src/share/vm/opto/cfgnode.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/systemDictionary.hpp"
aoqi@0 27 #include "memory/allocation.inline.hpp"
aoqi@0 28 #include "oops/objArrayKlass.hpp"
aoqi@0 29 #include "opto/addnode.hpp"
aoqi@0 30 #include "opto/cfgnode.hpp"
aoqi@0 31 #include "opto/connode.hpp"
aoqi@0 32 #include "opto/loopnode.hpp"
aoqi@0 33 #include "opto/machnode.hpp"
aoqi@0 34 #include "opto/mulnode.hpp"
aoqi@0 35 #include "opto/phaseX.hpp"
aoqi@0 36 #include "opto/regmask.hpp"
aoqi@0 37 #include "opto/runtime.hpp"
aoqi@0 38 #include "opto/subnode.hpp"
aoqi@0 39
aoqi@0 40 // Portions of code courtesy of Clifford Click
aoqi@0 41
aoqi@0 42 // Optimization - Graph Style
aoqi@0 43
aoqi@0 44 //=============================================================================
aoqi@0 45 //------------------------------Value------------------------------------------
aoqi@0 46 // Compute the type of the RegionNode.
aoqi@0 47 const Type *RegionNode::Value( PhaseTransform *phase ) const {
aoqi@0 48 for( uint i=1; i<req(); ++i ) { // For all paths in
aoqi@0 49 Node *n = in(i); // Get Control source
aoqi@0 50 if( !n ) continue; // Missing inputs are TOP
aoqi@0 51 if( phase->type(n) == Type::CONTROL )
aoqi@0 52 return Type::CONTROL;
aoqi@0 53 }
aoqi@0 54 return Type::TOP; // All paths dead? Then so are we
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 //------------------------------Identity---------------------------------------
aoqi@0 58 // Check for Region being Identity.
aoqi@0 59 Node *RegionNode::Identity( PhaseTransform *phase ) {
aoqi@0 60 // Cannot have Region be an identity, even if it has only 1 input.
aoqi@0 61 // Phi users cannot have their Region input folded away for them,
aoqi@0 62 // since they need to select the proper data input
aoqi@0 63 return this;
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 //------------------------------merge_region-----------------------------------
aoqi@0 67 // If a Region flows into a Region, merge into one big happy merge. This is
aoqi@0 68 // hard to do if there is stuff that has to happen
aoqi@0 69 static Node *merge_region(RegionNode *region, PhaseGVN *phase) {
aoqi@0 70 if( region->Opcode() != Op_Region ) // Do not do to LoopNodes
aoqi@0 71 return NULL;
aoqi@0 72 Node *progress = NULL; // Progress flag
aoqi@0 73 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 74
aoqi@0 75 uint rreq = region->req();
aoqi@0 76 for( uint i = 1; i < rreq; i++ ) {
aoqi@0 77 Node *r = region->in(i);
aoqi@0 78 if( r && r->Opcode() == Op_Region && // Found a region?
aoqi@0 79 r->in(0) == r && // Not already collapsed?
aoqi@0 80 r != region && // Avoid stupid situations
aoqi@0 81 r->outcnt() == 2 ) { // Self user and 'region' user only?
aoqi@0 82 assert(!r->as_Region()->has_phi(), "no phi users");
aoqi@0 83 if( !progress ) { // No progress
aoqi@0 84 if (region->has_phi()) {
aoqi@0 85 return NULL; // Only flatten if no Phi users
aoqi@0 86 // igvn->hash_delete( phi );
aoqi@0 87 }
aoqi@0 88 igvn->hash_delete( region );
aoqi@0 89 progress = region; // Making progress
aoqi@0 90 }
aoqi@0 91 igvn->hash_delete( r );
aoqi@0 92
aoqi@0 93 // Append inputs to 'r' onto 'region'
aoqi@0 94 for( uint j = 1; j < r->req(); j++ ) {
aoqi@0 95 // Move an input from 'r' to 'region'
aoqi@0 96 region->add_req(r->in(j));
aoqi@0 97 r->set_req(j, phase->C->top());
aoqi@0 98 // Update phis of 'region'
aoqi@0 99 //for( uint k = 0; k < max; k++ ) {
aoqi@0 100 // Node *phi = region->out(k);
aoqi@0 101 // if( phi->is_Phi() ) {
aoqi@0 102 // phi->add_req(phi->in(i));
aoqi@0 103 // }
aoqi@0 104 //}
aoqi@0 105
aoqi@0 106 rreq++; // One more input to Region
aoqi@0 107 } // Found a region to merge into Region
aoqi@0 108 // Clobber pointer to the now dead 'r'
aoqi@0 109 region->set_req(i, phase->C->top());
aoqi@0 110 }
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 return progress;
aoqi@0 114 }
aoqi@0 115
aoqi@0 116
aoqi@0 117
aoqi@0 118 //--------------------------------has_phi--------------------------------------
aoqi@0 119 // Helper function: Return any PhiNode that uses this region or NULL
aoqi@0 120 PhiNode* RegionNode::has_phi() const {
aoqi@0 121 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
aoqi@0 122 Node* phi = fast_out(i);
aoqi@0 123 if (phi->is_Phi()) { // Check for Phi users
aoqi@0 124 assert(phi->in(0) == (Node*)this, "phi uses region only via in(0)");
aoqi@0 125 return phi->as_Phi(); // this one is good enough
aoqi@0 126 }
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 return NULL;
aoqi@0 130 }
aoqi@0 131
aoqi@0 132
aoqi@0 133 //-----------------------------has_unique_phi----------------------------------
aoqi@0 134 // Helper function: Return the only PhiNode that uses this region or NULL
aoqi@0 135 PhiNode* RegionNode::has_unique_phi() const {
aoqi@0 136 // Check that only one use is a Phi
aoqi@0 137 PhiNode* only_phi = NULL;
aoqi@0 138 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
aoqi@0 139 Node* phi = fast_out(i);
aoqi@0 140 if (phi->is_Phi()) { // Check for Phi users
aoqi@0 141 assert(phi->in(0) == (Node*)this, "phi uses region only via in(0)");
aoqi@0 142 if (only_phi == NULL) {
aoqi@0 143 only_phi = phi->as_Phi();
aoqi@0 144 } else {
aoqi@0 145 return NULL; // multiple phis
aoqi@0 146 }
aoqi@0 147 }
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 return only_phi;
aoqi@0 151 }
aoqi@0 152
aoqi@0 153
aoqi@0 154 //------------------------------check_phi_clipping-----------------------------
aoqi@0 155 // Helper function for RegionNode's identification of FP clipping
aoqi@0 156 // Check inputs to the Phi
aoqi@0 157 static bool check_phi_clipping( PhiNode *phi, ConNode * &min, uint &min_idx, ConNode * &max, uint &max_idx, Node * &val, uint &val_idx ) {
aoqi@0 158 min = NULL;
aoqi@0 159 max = NULL;
aoqi@0 160 val = NULL;
aoqi@0 161 min_idx = 0;
aoqi@0 162 max_idx = 0;
aoqi@0 163 val_idx = 0;
aoqi@0 164 uint phi_max = phi->req();
aoqi@0 165 if( phi_max == 4 ) {
aoqi@0 166 for( uint j = 1; j < phi_max; ++j ) {
aoqi@0 167 Node *n = phi->in(j);
aoqi@0 168 int opcode = n->Opcode();
aoqi@0 169 switch( opcode ) {
aoqi@0 170 case Op_ConI:
aoqi@0 171 {
aoqi@0 172 if( min == NULL ) {
aoqi@0 173 min = n->Opcode() == Op_ConI ? (ConNode*)n : NULL;
aoqi@0 174 min_idx = j;
aoqi@0 175 } else {
aoqi@0 176 max = n->Opcode() == Op_ConI ? (ConNode*)n : NULL;
aoqi@0 177 max_idx = j;
aoqi@0 178 if( min->get_int() > max->get_int() ) {
aoqi@0 179 // Swap min and max
aoqi@0 180 ConNode *temp;
aoqi@0 181 uint temp_idx;
aoqi@0 182 temp = min; min = max; max = temp;
aoqi@0 183 temp_idx = min_idx; min_idx = max_idx; max_idx = temp_idx;
aoqi@0 184 }
aoqi@0 185 }
aoqi@0 186 }
aoqi@0 187 break;
aoqi@0 188 default:
aoqi@0 189 {
aoqi@0 190 val = n;
aoqi@0 191 val_idx = j;
aoqi@0 192 }
aoqi@0 193 break;
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196 }
aoqi@0 197 return ( min && max && val && (min->get_int() <= 0) && (max->get_int() >=0) );
aoqi@0 198 }
aoqi@0 199
aoqi@0 200
aoqi@0 201 //------------------------------check_if_clipping------------------------------
aoqi@0 202 // Helper function for RegionNode's identification of FP clipping
aoqi@0 203 // Check that inputs to Region come from two IfNodes,
aoqi@0 204 //
aoqi@0 205 // If
aoqi@0 206 // False True
aoqi@0 207 // If |
aoqi@0 208 // False True |
aoqi@0 209 // | | |
aoqi@0 210 // RegionNode_inputs
aoqi@0 211 //
aoqi@0 212 static bool check_if_clipping( const RegionNode *region, IfNode * &bot_if, IfNode * &top_if ) {
aoqi@0 213 top_if = NULL;
aoqi@0 214 bot_if = NULL;
aoqi@0 215
aoqi@0 216 // Check control structure above RegionNode for (if ( if ) )
aoqi@0 217 Node *in1 = region->in(1);
aoqi@0 218 Node *in2 = region->in(2);
aoqi@0 219 Node *in3 = region->in(3);
aoqi@0 220 // Check that all inputs are projections
aoqi@0 221 if( in1->is_Proj() && in2->is_Proj() && in3->is_Proj() ) {
aoqi@0 222 Node *in10 = in1->in(0);
aoqi@0 223 Node *in20 = in2->in(0);
aoqi@0 224 Node *in30 = in3->in(0);
aoqi@0 225 // Check that #1 and #2 are ifTrue and ifFalse from same If
aoqi@0 226 if( in10 != NULL && in10->is_If() &&
aoqi@0 227 in20 != NULL && in20->is_If() &&
aoqi@0 228 in30 != NULL && in30->is_If() && in10 == in20 &&
aoqi@0 229 (in1->Opcode() != in2->Opcode()) ) {
aoqi@0 230 Node *in100 = in10->in(0);
aoqi@0 231 Node *in1000 = (in100 != NULL && in100->is_Proj()) ? in100->in(0) : NULL;
aoqi@0 232 // Check that control for in10 comes from other branch of IF from in3
aoqi@0 233 if( in1000 != NULL && in1000->is_If() &&
aoqi@0 234 in30 == in1000 && (in3->Opcode() != in100->Opcode()) ) {
aoqi@0 235 // Control pattern checks
aoqi@0 236 top_if = (IfNode*)in1000;
aoqi@0 237 bot_if = (IfNode*)in10;
aoqi@0 238 }
aoqi@0 239 }
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 return (top_if != NULL);
aoqi@0 243 }
aoqi@0 244
aoqi@0 245
aoqi@0 246 //------------------------------check_convf2i_clipping-------------------------
aoqi@0 247 // Helper function for RegionNode's identification of FP clipping
aoqi@0 248 // Verify that the value input to the phi comes from "ConvF2I; LShift; RShift"
aoqi@0 249 static bool check_convf2i_clipping( PhiNode *phi, uint idx, ConvF2INode * &convf2i, Node *min, Node *max) {
aoqi@0 250 convf2i = NULL;
aoqi@0 251
aoqi@0 252 // Check for the RShiftNode
aoqi@0 253 Node *rshift = phi->in(idx);
aoqi@0 254 assert( rshift, "Previous checks ensure phi input is present");
aoqi@0 255 if( rshift->Opcode() != Op_RShiftI ) { return false; }
aoqi@0 256
aoqi@0 257 // Check for the LShiftNode
aoqi@0 258 Node *lshift = rshift->in(1);
aoqi@0 259 assert( lshift, "Previous checks ensure phi input is present");
aoqi@0 260 if( lshift->Opcode() != Op_LShiftI ) { return false; }
aoqi@0 261
aoqi@0 262 // Check for the ConvF2INode
aoqi@0 263 Node *conv = lshift->in(1);
aoqi@0 264 if( conv->Opcode() != Op_ConvF2I ) { return false; }
aoqi@0 265
aoqi@0 266 // Check that shift amounts are only to get sign bits set after F2I
aoqi@0 267 jint max_cutoff = max->get_int();
aoqi@0 268 jint min_cutoff = min->get_int();
aoqi@0 269 jint left_shift = lshift->in(2)->get_int();
aoqi@0 270 jint right_shift = rshift->in(2)->get_int();
aoqi@0 271 jint max_post_shift = nth_bit(BitsPerJavaInteger - left_shift - 1);
aoqi@0 272 if( left_shift != right_shift ||
aoqi@0 273 0 > left_shift || left_shift >= BitsPerJavaInteger ||
aoqi@0 274 max_post_shift < max_cutoff ||
aoqi@0 275 max_post_shift < -min_cutoff ) {
aoqi@0 276 // Shifts are necessary but current transformation eliminates them
aoqi@0 277 return false;
aoqi@0 278 }
aoqi@0 279
aoqi@0 280 // OK to return the result of ConvF2I without shifting
aoqi@0 281 convf2i = (ConvF2INode*)conv;
aoqi@0 282 return true;
aoqi@0 283 }
aoqi@0 284
aoqi@0 285
aoqi@0 286 //------------------------------check_compare_clipping-------------------------
aoqi@0 287 // Helper function for RegionNode's identification of FP clipping
aoqi@0 288 static bool check_compare_clipping( bool less_than, IfNode *iff, ConNode *limit, Node * & input ) {
aoqi@0 289 Node *i1 = iff->in(1);
aoqi@0 290 if ( !i1->is_Bool() ) { return false; }
aoqi@0 291 BoolNode *bool1 = i1->as_Bool();
aoqi@0 292 if( less_than && bool1->_test._test != BoolTest::le ) { return false; }
aoqi@0 293 else if( !less_than && bool1->_test._test != BoolTest::lt ) { return false; }
aoqi@0 294 const Node *cmpF = bool1->in(1);
aoqi@0 295 if( cmpF->Opcode() != Op_CmpF ) { return false; }
aoqi@0 296 // Test that the float value being compared against
aoqi@0 297 // is equivalent to the int value used as a limit
aoqi@0 298 Node *nodef = cmpF->in(2);
aoqi@0 299 if( nodef->Opcode() != Op_ConF ) { return false; }
aoqi@0 300 jfloat conf = nodef->getf();
aoqi@0 301 jint coni = limit->get_int();
aoqi@0 302 if( ((int)conf) != coni ) { return false; }
aoqi@0 303 input = cmpF->in(1);
aoqi@0 304 return true;
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 //------------------------------is_unreachable_region--------------------------
aoqi@0 308 // Find if the Region node is reachable from the root.
aoqi@0 309 bool RegionNode::is_unreachable_region(PhaseGVN *phase) const {
aoqi@0 310 assert(req() == 2, "");
aoqi@0 311
aoqi@0 312 // First, cut the simple case of fallthrough region when NONE of
aoqi@0 313 // region's phis references itself directly or through a data node.
aoqi@0 314 uint max = outcnt();
aoqi@0 315 uint i;
aoqi@0 316 for (i = 0; i < max; i++) {
aoqi@0 317 Node* phi = raw_out(i);
aoqi@0 318 if (phi != NULL && phi->is_Phi()) {
aoqi@0 319 assert(phase->eqv(phi->in(0), this) && phi->req() == 2, "");
aoqi@0 320 if (phi->outcnt() == 0)
aoqi@0 321 continue; // Safe case - no loops
aoqi@0 322 if (phi->outcnt() == 1) {
aoqi@0 323 Node* u = phi->raw_out(0);
aoqi@0 324 // Skip if only one use is an other Phi or Call or Uncommon trap.
aoqi@0 325 // It is safe to consider this case as fallthrough.
aoqi@0 326 if (u != NULL && (u->is_Phi() || u->is_CFG()))
aoqi@0 327 continue;
aoqi@0 328 }
aoqi@0 329 // Check when phi references itself directly or through an other node.
aoqi@0 330 if (phi->as_Phi()->simple_data_loop_check(phi->in(1)) >= PhiNode::Unsafe)
aoqi@0 331 break; // Found possible unsafe data loop.
aoqi@0 332 }
aoqi@0 333 }
aoqi@0 334 if (i >= max)
aoqi@0 335 return false; // An unsafe case was NOT found - don't need graph walk.
aoqi@0 336
aoqi@0 337 // Unsafe case - check if the Region node is reachable from root.
aoqi@0 338 ResourceMark rm;
aoqi@0 339
aoqi@0 340 Arena *a = Thread::current()->resource_area();
aoqi@0 341 Node_List nstack(a);
aoqi@0 342 VectorSet visited(a);
aoqi@0 343
aoqi@0 344 // Mark all control nodes reachable from root outputs
aoqi@0 345 Node *n = (Node*)phase->C->root();
aoqi@0 346 nstack.push(n);
aoqi@0 347 visited.set(n->_idx);
aoqi@0 348 while (nstack.size() != 0) {
aoqi@0 349 n = nstack.pop();
aoqi@0 350 uint max = n->outcnt();
aoqi@0 351 for (uint i = 0; i < max; i++) {
aoqi@0 352 Node* m = n->raw_out(i);
aoqi@0 353 if (m != NULL && m->is_CFG()) {
aoqi@0 354 if (phase->eqv(m, this)) {
aoqi@0 355 return false; // We reached the Region node - it is not dead.
aoqi@0 356 }
aoqi@0 357 if (!visited.test_set(m->_idx))
aoqi@0 358 nstack.push(m);
aoqi@0 359 }
aoqi@0 360 }
aoqi@0 361 }
aoqi@0 362
aoqi@0 363 return true; // The Region node is unreachable - it is dead.
aoqi@0 364 }
aoqi@0 365
aoqi@0 366 bool RegionNode::try_clean_mem_phi(PhaseGVN *phase) {
aoqi@0 367 // Incremental inlining + PhaseStringOpts sometimes produce:
aoqi@0 368 //
aoqi@0 369 // cmpP with 1 top input
aoqi@0 370 // |
aoqi@0 371 // If
aoqi@0 372 // / \
aoqi@0 373 // IfFalse IfTrue /- Some Node
aoqi@0 374 // \ / / /
aoqi@0 375 // Region / /-MergeMem
aoqi@0 376 // \---Phi
aoqi@0 377 //
aoqi@0 378 //
aoqi@0 379 // It's expected by PhaseStringOpts that the Region goes away and is
aoqi@0 380 // replaced by If's control input but because there's still a Phi,
aoqi@0 381 // the Region stays in the graph. The top input from the cmpP is
aoqi@0 382 // propagated forward and a subgraph that is useful goes away. The
aoqi@0 383 // code below replaces the Phi with the MergeMem so that the Region
aoqi@0 384 // is simplified.
aoqi@0 385
aoqi@0 386 PhiNode* phi = has_unique_phi();
aoqi@0 387 if (phi && phi->type() == Type::MEMORY && req() == 3 && phi->is_diamond_phi(true)) {
aoqi@0 388 MergeMemNode* m = NULL;
aoqi@0 389 assert(phi->req() == 3, "same as region");
aoqi@0 390 for (uint i = 1; i < 3; ++i) {
aoqi@0 391 Node *mem = phi->in(i);
aoqi@0 392 if (mem && mem->is_MergeMem() && in(i)->outcnt() == 1) {
aoqi@0 393 // Nothing is control-dependent on path #i except the region itself.
aoqi@0 394 m = mem->as_MergeMem();
aoqi@0 395 uint j = 3 - i;
aoqi@0 396 Node* other = phi->in(j);
aoqi@0 397 if (other && other == m->base_memory()) {
aoqi@0 398 // m is a successor memory to other, and is not pinned inside the diamond, so push it out.
aoqi@0 399 // This will allow the diamond to collapse completely.
aoqi@0 400 phase->is_IterGVN()->replace_node(phi, m);
aoqi@0 401 return true;
aoqi@0 402 }
aoqi@0 403 }
aoqi@0 404 }
aoqi@0 405 }
aoqi@0 406 return false;
aoqi@0 407 }
aoqi@0 408
aoqi@0 409 //------------------------------Ideal------------------------------------------
aoqi@0 410 // Return a node which is more "ideal" than the current node. Must preserve
aoqi@0 411 // the CFG, but we can still strip out dead paths.
aoqi@0 412 Node *RegionNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 413 if( !can_reshape && !in(0) ) return NULL; // Already degraded to a Copy
aoqi@0 414 assert(!in(0) || !in(0)->is_Root(), "not a specially hidden merge");
aoqi@0 415
aoqi@0 416 // Check for RegionNode with no Phi users and both inputs come from either
aoqi@0 417 // arm of the same IF. If found, then the control-flow split is useless.
aoqi@0 418 bool has_phis = false;
aoqi@0 419 if (can_reshape) { // Need DU info to check for Phi users
aoqi@0 420 has_phis = (has_phi() != NULL); // Cache result
aoqi@0 421 if (has_phis && try_clean_mem_phi(phase)) {
aoqi@0 422 has_phis = false;
aoqi@0 423 }
aoqi@0 424
aoqi@0 425 if (!has_phis) { // No Phi users? Nothing merging?
aoqi@0 426 for (uint i = 1; i < req()-1; i++) {
aoqi@0 427 Node *if1 = in(i);
aoqi@0 428 if( !if1 ) continue;
aoqi@0 429 Node *iff = if1->in(0);
aoqi@0 430 if( !iff || !iff->is_If() ) continue;
aoqi@0 431 for( uint j=i+1; j<req(); j++ ) {
aoqi@0 432 if( in(j) && in(j)->in(0) == iff &&
aoqi@0 433 if1->Opcode() != in(j)->Opcode() ) {
aoqi@0 434 // Add the IF Projections to the worklist. They (and the IF itself)
aoqi@0 435 // will be eliminated if dead.
aoqi@0 436 phase->is_IterGVN()->add_users_to_worklist(iff);
aoqi@0 437 set_req(i, iff->in(0));// Skip around the useless IF diamond
aoqi@0 438 set_req(j, NULL);
aoqi@0 439 return this; // Record progress
aoqi@0 440 }
aoqi@0 441 }
aoqi@0 442 }
aoqi@0 443 }
aoqi@0 444 }
aoqi@0 445
aoqi@0 446 // Remove TOP or NULL input paths. If only 1 input path remains, this Region
aoqi@0 447 // degrades to a copy.
aoqi@0 448 bool add_to_worklist = false;
aoqi@0 449 int cnt = 0; // Count of values merging
aoqi@0 450 DEBUG_ONLY( int cnt_orig = req(); ) // Save original inputs count
aoqi@0 451 int del_it = 0; // The last input path we delete
aoqi@0 452 // For all inputs...
aoqi@0 453 for( uint i=1; i<req(); ++i ){// For all paths in
aoqi@0 454 Node *n = in(i); // Get the input
aoqi@0 455 if( n != NULL ) {
aoqi@0 456 // Remove useless control copy inputs
aoqi@0 457 if( n->is_Region() && n->as_Region()->is_copy() ) {
aoqi@0 458 set_req(i, n->nonnull_req());
aoqi@0 459 i--;
aoqi@0 460 continue;
aoqi@0 461 }
aoqi@0 462 if( n->is_Proj() ) { // Remove useless rethrows
aoqi@0 463 Node *call = n->in(0);
aoqi@0 464 if (call->is_Call() && call->as_Call()->entry_point() == OptoRuntime::rethrow_stub()) {
aoqi@0 465 set_req(i, call->in(0));
aoqi@0 466 i--;
aoqi@0 467 continue;
aoqi@0 468 }
aoqi@0 469 }
aoqi@0 470 if( phase->type(n) == Type::TOP ) {
aoqi@0 471 set_req(i, NULL); // Ignore TOP inputs
aoqi@0 472 i--;
aoqi@0 473 continue;
aoqi@0 474 }
aoqi@0 475 cnt++; // One more value merging
aoqi@0 476
aoqi@0 477 } else if (can_reshape) { // Else found dead path with DU info
aoqi@0 478 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 479 del_req(i); // Yank path from self
aoqi@0 480 del_it = i;
aoqi@0 481 uint max = outcnt();
aoqi@0 482 DUIterator j;
aoqi@0 483 bool progress = true;
aoqi@0 484 while(progress) { // Need to establish property over all users
aoqi@0 485 progress = false;
aoqi@0 486 for (j = outs(); has_out(j); j++) {
aoqi@0 487 Node *n = out(j);
aoqi@0 488 if( n->req() != req() && n->is_Phi() ) {
aoqi@0 489 assert( n->in(0) == this, "" );
aoqi@0 490 igvn->hash_delete(n); // Yank from hash before hacking edges
aoqi@0 491 n->set_req_X(i,NULL,igvn);// Correct DU info
aoqi@0 492 n->del_req(i); // Yank path from Phis
aoqi@0 493 if( max != outcnt() ) {
aoqi@0 494 progress = true;
aoqi@0 495 j = refresh_out_pos(j);
aoqi@0 496 max = outcnt();
aoqi@0 497 }
aoqi@0 498 }
aoqi@0 499 }
aoqi@0 500 }
aoqi@0 501 add_to_worklist = true;
aoqi@0 502 i--;
aoqi@0 503 }
aoqi@0 504 }
aoqi@0 505
aoqi@0 506 if (can_reshape && cnt == 1) {
aoqi@0 507 // Is it dead loop?
aoqi@0 508 // If it is LoopNopde it had 2 (+1 itself) inputs and
aoqi@0 509 // one of them was cut. The loop is dead if it was EntryContol.
aoqi@0 510 // Loop node may have only one input because entry path
aoqi@0 511 // is removed in PhaseIdealLoop::Dominators().
aoqi@0 512 assert(!this->is_Loop() || cnt_orig <= 3, "Loop node should have 3 or less inputs");
aoqi@0 513 if (this->is_Loop() && (del_it == LoopNode::EntryControl ||
aoqi@0 514 del_it == 0 && is_unreachable_region(phase)) ||
aoqi@0 515 !this->is_Loop() && has_phis && is_unreachable_region(phase)) {
aoqi@0 516 // Yes, the region will be removed during the next step below.
aoqi@0 517 // Cut the backedge input and remove phis since no data paths left.
aoqi@0 518 // We don't cut outputs to other nodes here since we need to put them
aoqi@0 519 // on the worklist.
aoqi@0 520 del_req(1);
aoqi@0 521 cnt = 0;
aoqi@0 522 assert( req() == 1, "no more inputs expected" );
aoqi@0 523 uint max = outcnt();
aoqi@0 524 bool progress = true;
aoqi@0 525 Node *top = phase->C->top();
aoqi@0 526 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 527 DUIterator j;
aoqi@0 528 while(progress) {
aoqi@0 529 progress = false;
aoqi@0 530 for (j = outs(); has_out(j); j++) {
aoqi@0 531 Node *n = out(j);
aoqi@0 532 if( n->is_Phi() ) {
aoqi@0 533 assert( igvn->eqv(n->in(0), this), "" );
aoqi@0 534 assert( n->req() == 2 && n->in(1) != NULL, "Only one data input expected" );
aoqi@0 535 // Break dead loop data path.
aoqi@0 536 // Eagerly replace phis with top to avoid phis copies generation.
aoqi@0 537 igvn->replace_node(n, top);
aoqi@0 538 if( max != outcnt() ) {
aoqi@0 539 progress = true;
aoqi@0 540 j = refresh_out_pos(j);
aoqi@0 541 max = outcnt();
aoqi@0 542 }
aoqi@0 543 }
aoqi@0 544 }
aoqi@0 545 }
aoqi@0 546 add_to_worklist = true;
aoqi@0 547 }
aoqi@0 548 }
aoqi@0 549 if (add_to_worklist) {
aoqi@0 550 phase->is_IterGVN()->add_users_to_worklist(this); // Revisit collapsed Phis
aoqi@0 551 }
aoqi@0 552
aoqi@0 553 if( cnt <= 1 ) { // Only 1 path in?
aoqi@0 554 set_req(0, NULL); // Null control input for region copy
aoqi@0 555 if( cnt == 0 && !can_reshape) { // Parse phase - leave the node as it is.
aoqi@0 556 // No inputs or all inputs are NULL.
aoqi@0 557 return NULL;
aoqi@0 558 } else if (can_reshape) { // Optimization phase - remove the node
aoqi@0 559 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 560 Node *parent_ctrl;
aoqi@0 561 if( cnt == 0 ) {
aoqi@0 562 assert( req() == 1, "no inputs expected" );
aoqi@0 563 // During IGVN phase such region will be subsumed by TOP node
aoqi@0 564 // so region's phis will have TOP as control node.
aoqi@0 565 // Kill phis here to avoid it. PhiNode::is_copy() will be always false.
aoqi@0 566 // Also set other user's input to top.
aoqi@0 567 parent_ctrl = phase->C->top();
aoqi@0 568 } else {
aoqi@0 569 // The fallthrough case since we already checked dead loops above.
aoqi@0 570 parent_ctrl = in(1);
aoqi@0 571 assert(parent_ctrl != NULL, "Region is a copy of some non-null control");
aoqi@0 572 assert(!igvn->eqv(parent_ctrl, this), "Close dead loop");
aoqi@0 573 }
aoqi@0 574 if (!add_to_worklist)
aoqi@0 575 igvn->add_users_to_worklist(this); // Check for further allowed opts
aoqi@0 576 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
aoqi@0 577 Node* n = last_out(i);
aoqi@0 578 igvn->hash_delete(n); // Remove from worklist before modifying edges
aoqi@0 579 if( n->is_Phi() ) { // Collapse all Phis
aoqi@0 580 // Eagerly replace phis to avoid copies generation.
aoqi@0 581 Node* in;
aoqi@0 582 if( cnt == 0 ) {
aoqi@0 583 assert( n->req() == 1, "No data inputs expected" );
aoqi@0 584 in = parent_ctrl; // replaced by top
aoqi@0 585 } else {
aoqi@0 586 assert( n->req() == 2 && n->in(1) != NULL, "Only one data input expected" );
aoqi@0 587 in = n->in(1); // replaced by unique input
aoqi@0 588 if( n->as_Phi()->is_unsafe_data_reference(in) )
aoqi@0 589 in = phase->C->top(); // replaced by top
aoqi@0 590 }
aoqi@0 591 igvn->replace_node(n, in);
aoqi@0 592 }
aoqi@0 593 else if( n->is_Region() ) { // Update all incoming edges
aoqi@0 594 assert( !igvn->eqv(n, this), "Must be removed from DefUse edges");
aoqi@0 595 uint uses_found = 0;
aoqi@0 596 for( uint k=1; k < n->req(); k++ ) {
aoqi@0 597 if( n->in(k) == this ) {
aoqi@0 598 n->set_req(k, parent_ctrl);
aoqi@0 599 uses_found++;
aoqi@0 600 }
aoqi@0 601 }
aoqi@0 602 if( uses_found > 1 ) { // (--i) done at the end of the loop.
aoqi@0 603 i -= (uses_found - 1);
aoqi@0 604 }
aoqi@0 605 }
aoqi@0 606 else {
aoqi@0 607 assert( igvn->eqv(n->in(0), this), "Expect RegionNode to be control parent");
aoqi@0 608 n->set_req(0, parent_ctrl);
aoqi@0 609 }
aoqi@0 610 #ifdef ASSERT
aoqi@0 611 for( uint k=0; k < n->req(); k++ ) {
aoqi@0 612 assert( !igvn->eqv(n->in(k), this), "All uses of RegionNode should be gone");
aoqi@0 613 }
aoqi@0 614 #endif
aoqi@0 615 }
aoqi@0 616 // Remove the RegionNode itself from DefUse info
aoqi@0 617 igvn->remove_dead_node(this);
aoqi@0 618 return NULL;
aoqi@0 619 }
aoqi@0 620 return this; // Record progress
aoqi@0 621 }
aoqi@0 622
aoqi@0 623
aoqi@0 624 // If a Region flows into a Region, merge into one big happy merge.
aoqi@0 625 if (can_reshape) {
aoqi@0 626 Node *m = merge_region(this, phase);
aoqi@0 627 if (m != NULL) return m;
aoqi@0 628 }
aoqi@0 629
aoqi@0 630 // Check if this region is the root of a clipping idiom on floats
aoqi@0 631 if( ConvertFloat2IntClipping && can_reshape && req() == 4 ) {
aoqi@0 632 // Check that only one use is a Phi and that it simplifies to two constants +
aoqi@0 633 PhiNode* phi = has_unique_phi();
aoqi@0 634 if (phi != NULL) { // One Phi user
aoqi@0 635 // Check inputs to the Phi
aoqi@0 636 ConNode *min;
aoqi@0 637 ConNode *max;
aoqi@0 638 Node *val;
aoqi@0 639 uint min_idx;
aoqi@0 640 uint max_idx;
aoqi@0 641 uint val_idx;
aoqi@0 642 if( check_phi_clipping( phi, min, min_idx, max, max_idx, val, val_idx ) ) {
aoqi@0 643 IfNode *top_if;
aoqi@0 644 IfNode *bot_if;
aoqi@0 645 if( check_if_clipping( this, bot_if, top_if ) ) {
aoqi@0 646 // Control pattern checks, now verify compares
aoqi@0 647 Node *top_in = NULL; // value being compared against
aoqi@0 648 Node *bot_in = NULL;
aoqi@0 649 if( check_compare_clipping( true, bot_if, min, bot_in ) &&
aoqi@0 650 check_compare_clipping( false, top_if, max, top_in ) ) {
aoqi@0 651 if( bot_in == top_in ) {
aoqi@0 652 PhaseIterGVN *gvn = phase->is_IterGVN();
aoqi@0 653 assert( gvn != NULL, "Only had DefUse info in IterGVN");
aoqi@0 654 // Only remaining check is that bot_in == top_in == (Phi's val + mods)
aoqi@0 655
aoqi@0 656 // Check for the ConvF2INode
aoqi@0 657 ConvF2INode *convf2i;
aoqi@0 658 if( check_convf2i_clipping( phi, val_idx, convf2i, min, max ) &&
aoqi@0 659 convf2i->in(1) == bot_in ) {
aoqi@0 660 // Matched pattern, including LShiftI; RShiftI, replace with integer compares
aoqi@0 661 // max test
aoqi@0 662 Node *cmp = gvn->register_new_node_with_optimizer(new (phase->C) CmpINode( convf2i, min ));
aoqi@0 663 Node *boo = gvn->register_new_node_with_optimizer(new (phase->C) BoolNode( cmp, BoolTest::lt ));
aoqi@0 664 IfNode *iff = (IfNode*)gvn->register_new_node_with_optimizer(new (phase->C) IfNode( top_if->in(0), boo, PROB_UNLIKELY_MAG(5), top_if->_fcnt ));
aoqi@0 665 Node *if_min= gvn->register_new_node_with_optimizer(new (phase->C) IfTrueNode (iff));
aoqi@0 666 Node *ifF = gvn->register_new_node_with_optimizer(new (phase->C) IfFalseNode(iff));
aoqi@0 667 // min test
aoqi@0 668 cmp = gvn->register_new_node_with_optimizer(new (phase->C) CmpINode( convf2i, max ));
aoqi@0 669 boo = gvn->register_new_node_with_optimizer(new (phase->C) BoolNode( cmp, BoolTest::gt ));
aoqi@0 670 iff = (IfNode*)gvn->register_new_node_with_optimizer(new (phase->C) IfNode( ifF, boo, PROB_UNLIKELY_MAG(5), bot_if->_fcnt ));
aoqi@0 671 Node *if_max= gvn->register_new_node_with_optimizer(new (phase->C) IfTrueNode (iff));
aoqi@0 672 ifF = gvn->register_new_node_with_optimizer(new (phase->C) IfFalseNode(iff));
aoqi@0 673 // update input edges to region node
aoqi@0 674 set_req_X( min_idx, if_min, gvn );
aoqi@0 675 set_req_X( max_idx, if_max, gvn );
aoqi@0 676 set_req_X( val_idx, ifF, gvn );
aoqi@0 677 // remove unnecessary 'LShiftI; RShiftI' idiom
aoqi@0 678 gvn->hash_delete(phi);
aoqi@0 679 phi->set_req_X( val_idx, convf2i, gvn );
aoqi@0 680 gvn->hash_find_insert(phi);
aoqi@0 681 // Return transformed region node
aoqi@0 682 return this;
aoqi@0 683 }
aoqi@0 684 }
aoqi@0 685 }
aoqi@0 686 }
aoqi@0 687 }
aoqi@0 688 }
aoqi@0 689 }
aoqi@0 690
aoqi@0 691 return NULL;
aoqi@0 692 }
aoqi@0 693
aoqi@0 694
aoqi@0 695
aoqi@0 696 const RegMask &RegionNode::out_RegMask() const {
aoqi@0 697 return RegMask::Empty;
aoqi@0 698 }
aoqi@0 699
aoqi@0 700 // Find the one non-null required input. RegionNode only
aoqi@0 701 Node *Node::nonnull_req() const {
aoqi@0 702 assert( is_Region(), "" );
aoqi@0 703 for( uint i = 1; i < _cnt; i++ )
aoqi@0 704 if( in(i) )
aoqi@0 705 return in(i);
aoqi@0 706 ShouldNotReachHere();
aoqi@0 707 return NULL;
aoqi@0 708 }
aoqi@0 709
aoqi@0 710
aoqi@0 711 //=============================================================================
aoqi@0 712 // note that these functions assume that the _adr_type field is flattened
aoqi@0 713 uint PhiNode::hash() const {
aoqi@0 714 const Type* at = _adr_type;
aoqi@0 715 return TypeNode::hash() + (at ? at->hash() : 0);
aoqi@0 716 }
aoqi@0 717 uint PhiNode::cmp( const Node &n ) const {
aoqi@0 718 return TypeNode::cmp(n) && _adr_type == ((PhiNode&)n)._adr_type;
aoqi@0 719 }
aoqi@0 720 static inline
aoqi@0 721 const TypePtr* flatten_phi_adr_type(const TypePtr* at) {
aoqi@0 722 if (at == NULL || at == TypePtr::BOTTOM) return at;
aoqi@0 723 return Compile::current()->alias_type(at)->adr_type();
aoqi@0 724 }
aoqi@0 725
aoqi@0 726 //----------------------------make---------------------------------------------
aoqi@0 727 // create a new phi with edges matching r and set (initially) to x
aoqi@0 728 PhiNode* PhiNode::make(Node* r, Node* x, const Type *t, const TypePtr* at) {
aoqi@0 729 uint preds = r->req(); // Number of predecessor paths
aoqi@0 730 assert(t != Type::MEMORY || at == flatten_phi_adr_type(at), "flatten at");
aoqi@0 731 PhiNode* p = new (Compile::current()) PhiNode(r, t, at);
aoqi@0 732 for (uint j = 1; j < preds; j++) {
aoqi@0 733 // Fill in all inputs, except those which the region does not yet have
aoqi@0 734 if (r->in(j) != NULL)
aoqi@0 735 p->init_req(j, x);
aoqi@0 736 }
aoqi@0 737 return p;
aoqi@0 738 }
aoqi@0 739 PhiNode* PhiNode::make(Node* r, Node* x) {
aoqi@0 740 const Type* t = x->bottom_type();
aoqi@0 741 const TypePtr* at = NULL;
aoqi@0 742 if (t == Type::MEMORY) at = flatten_phi_adr_type(x->adr_type());
aoqi@0 743 return make(r, x, t, at);
aoqi@0 744 }
aoqi@0 745 PhiNode* PhiNode::make_blank(Node* r, Node* x) {
aoqi@0 746 const Type* t = x->bottom_type();
aoqi@0 747 const TypePtr* at = NULL;
aoqi@0 748 if (t == Type::MEMORY) at = flatten_phi_adr_type(x->adr_type());
aoqi@0 749 return new (Compile::current()) PhiNode(r, t, at);
aoqi@0 750 }
aoqi@0 751
aoqi@0 752
aoqi@0 753 //------------------------slice_memory-----------------------------------------
aoqi@0 754 // create a new phi with narrowed memory type
aoqi@0 755 PhiNode* PhiNode::slice_memory(const TypePtr* adr_type) const {
aoqi@0 756 PhiNode* mem = (PhiNode*) clone();
aoqi@0 757 *(const TypePtr**)&mem->_adr_type = adr_type;
aoqi@0 758 // convert self-loops, or else we get a bad graph
aoqi@0 759 for (uint i = 1; i < req(); i++) {
aoqi@0 760 if ((const Node*)in(i) == this) mem->set_req(i, mem);
aoqi@0 761 }
aoqi@0 762 mem->verify_adr_type();
aoqi@0 763 return mem;
aoqi@0 764 }
aoqi@0 765
aoqi@0 766 //------------------------split_out_instance-----------------------------------
aoqi@0 767 // Split out an instance type from a bottom phi.
aoqi@0 768 PhiNode* PhiNode::split_out_instance(const TypePtr* at, PhaseIterGVN *igvn) const {
aoqi@0 769 const TypeOopPtr *t_oop = at->isa_oopptr();
aoqi@0 770 assert(t_oop != NULL && t_oop->is_known_instance(), "expecting instance oopptr");
aoqi@0 771 const TypePtr *t = adr_type();
aoqi@0 772 assert(type() == Type::MEMORY &&
aoqi@0 773 (t == TypePtr::BOTTOM || t == TypeRawPtr::BOTTOM ||
aoqi@0 774 t->isa_oopptr() && !t->is_oopptr()->is_known_instance() &&
aoqi@0 775 t->is_oopptr()->cast_to_exactness(true)
aoqi@0 776 ->is_oopptr()->cast_to_ptr_type(t_oop->ptr())
aoqi@0 777 ->is_oopptr()->cast_to_instance_id(t_oop->instance_id()) == t_oop),
aoqi@0 778 "bottom or raw memory required");
aoqi@0 779
aoqi@0 780 // Check if an appropriate node already exists.
aoqi@0 781 Node *region = in(0);
aoqi@0 782 for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) {
aoqi@0 783 Node* use = region->fast_out(k);
aoqi@0 784 if( use->is_Phi()) {
aoqi@0 785 PhiNode *phi2 = use->as_Phi();
aoqi@0 786 if (phi2->type() == Type::MEMORY && phi2->adr_type() == at) {
aoqi@0 787 return phi2;
aoqi@0 788 }
aoqi@0 789 }
aoqi@0 790 }
aoqi@0 791 Compile *C = igvn->C;
aoqi@0 792 Arena *a = Thread::current()->resource_area();
aoqi@0 793 Node_Array node_map = new Node_Array(a);
aoqi@0 794 Node_Stack stack(a, C->unique() >> 4);
aoqi@0 795 PhiNode *nphi = slice_memory(at);
aoqi@0 796 igvn->register_new_node_with_optimizer( nphi );
aoqi@0 797 node_map.map(_idx, nphi);
aoqi@0 798 stack.push((Node *)this, 1);
aoqi@0 799 while(!stack.is_empty()) {
aoqi@0 800 PhiNode *ophi = stack.node()->as_Phi();
aoqi@0 801 uint i = stack.index();
aoqi@0 802 assert(i >= 1, "not control edge");
aoqi@0 803 stack.pop();
aoqi@0 804 nphi = node_map[ophi->_idx]->as_Phi();
aoqi@0 805 for (; i < ophi->req(); i++) {
aoqi@0 806 Node *in = ophi->in(i);
aoqi@0 807 if (in == NULL || igvn->type(in) == Type::TOP)
aoqi@0 808 continue;
aoqi@0 809 Node *opt = MemNode::optimize_simple_memory_chain(in, t_oop, NULL, igvn);
aoqi@0 810 PhiNode *optphi = opt->is_Phi() ? opt->as_Phi() : NULL;
aoqi@0 811 if (optphi != NULL && optphi->adr_type() == TypePtr::BOTTOM) {
aoqi@0 812 opt = node_map[optphi->_idx];
aoqi@0 813 if (opt == NULL) {
aoqi@0 814 stack.push(ophi, i);
aoqi@0 815 nphi = optphi->slice_memory(at);
aoqi@0 816 igvn->register_new_node_with_optimizer( nphi );
aoqi@0 817 node_map.map(optphi->_idx, nphi);
aoqi@0 818 ophi = optphi;
aoqi@0 819 i = 0; // will get incremented at top of loop
aoqi@0 820 continue;
aoqi@0 821 }
aoqi@0 822 }
aoqi@0 823 nphi->set_req(i, opt);
aoqi@0 824 }
aoqi@0 825 }
aoqi@0 826 return nphi;
aoqi@0 827 }
aoqi@0 828
aoqi@0 829 //------------------------verify_adr_type--------------------------------------
aoqi@0 830 #ifdef ASSERT
aoqi@0 831 void PhiNode::verify_adr_type(VectorSet& visited, const TypePtr* at) const {
aoqi@0 832 if (visited.test_set(_idx)) return; //already visited
aoqi@0 833
aoqi@0 834 // recheck constructor invariants:
aoqi@0 835 verify_adr_type(false);
aoqi@0 836
aoqi@0 837 // recheck local phi/phi consistency:
aoqi@0 838 assert(_adr_type == at || _adr_type == TypePtr::BOTTOM,
aoqi@0 839 "adr_type must be consistent across phi nest");
aoqi@0 840
aoqi@0 841 // walk around
aoqi@0 842 for (uint i = 1; i < req(); i++) {
aoqi@0 843 Node* n = in(i);
aoqi@0 844 if (n == NULL) continue;
aoqi@0 845 const Node* np = in(i);
aoqi@0 846 if (np->is_Phi()) {
aoqi@0 847 np->as_Phi()->verify_adr_type(visited, at);
aoqi@0 848 } else if (n->bottom_type() == Type::TOP
aoqi@0 849 || (n->is_Mem() && n->in(MemNode::Address)->bottom_type() == Type::TOP)) {
aoqi@0 850 // ignore top inputs
aoqi@0 851 } else {
aoqi@0 852 const TypePtr* nat = flatten_phi_adr_type(n->adr_type());
aoqi@0 853 // recheck phi/non-phi consistency at leaves:
aoqi@0 854 assert((nat != NULL) == (at != NULL), "");
aoqi@0 855 assert(nat == at || nat == TypePtr::BOTTOM,
aoqi@0 856 "adr_type must be consistent at leaves of phi nest");
aoqi@0 857 }
aoqi@0 858 }
aoqi@0 859 }
aoqi@0 860
aoqi@0 861 // Verify a whole nest of phis rooted at this one.
aoqi@0 862 void PhiNode::verify_adr_type(bool recursive) const {
aoqi@0 863 if (is_error_reported()) return; // muzzle asserts when debugging an error
aoqi@0 864 if (Node::in_dump()) return; // muzzle asserts when printing
aoqi@0 865
aoqi@0 866 assert((_type == Type::MEMORY) == (_adr_type != NULL), "adr_type for memory phis only");
aoqi@0 867
aoqi@0 868 if (!VerifyAliases) return; // verify thoroughly only if requested
aoqi@0 869
aoqi@0 870 assert(_adr_type == flatten_phi_adr_type(_adr_type),
aoqi@0 871 "Phi::adr_type must be pre-normalized");
aoqi@0 872
aoqi@0 873 if (recursive) {
aoqi@0 874 VectorSet visited(Thread::current()->resource_area());
aoqi@0 875 verify_adr_type(visited, _adr_type);
aoqi@0 876 }
aoqi@0 877 }
aoqi@0 878 #endif
aoqi@0 879
aoqi@0 880
aoqi@0 881 //------------------------------Value------------------------------------------
aoqi@0 882 // Compute the type of the PhiNode
aoqi@0 883 const Type *PhiNode::Value( PhaseTransform *phase ) const {
aoqi@0 884 Node *r = in(0); // RegionNode
aoqi@0 885 if( !r ) // Copy or dead
aoqi@0 886 return in(1) ? phase->type(in(1)) : Type::TOP;
aoqi@0 887
aoqi@0 888 // Note: During parsing, phis are often transformed before their regions.
aoqi@0 889 // This means we have to use type_or_null to defend against untyped regions.
aoqi@0 890 if( phase->type_or_null(r) == Type::TOP ) // Dead code?
aoqi@0 891 return Type::TOP;
aoqi@0 892
aoqi@0 893 // Check for trip-counted loop. If so, be smarter.
aoqi@0 894 CountedLoopNode *l = r->is_CountedLoop() ? r->as_CountedLoop() : NULL;
aoqi@0 895 if( l && l->can_be_counted_loop(phase) &&
aoqi@0 896 ((const Node*)l->phi() == this) ) { // Trip counted loop!
aoqi@0 897 // protect against init_trip() or limit() returning NULL
aoqi@0 898 const Node *init = l->init_trip();
aoqi@0 899 const Node *limit = l->limit();
aoqi@0 900 if( init != NULL && limit != NULL && l->stride_is_con() ) {
aoqi@0 901 const TypeInt *lo = init ->bottom_type()->isa_int();
aoqi@0 902 const TypeInt *hi = limit->bottom_type()->isa_int();
aoqi@0 903 if( lo && hi ) { // Dying loops might have TOP here
aoqi@0 904 int stride = l->stride_con();
aoqi@0 905 if( stride < 0 ) { // Down-counter loop
aoqi@0 906 const TypeInt *tmp = lo; lo = hi; hi = tmp;
aoqi@0 907 stride = -stride;
aoqi@0 908 }
aoqi@0 909 if( lo->_hi < hi->_lo ) // Reversed endpoints are well defined :-(
aoqi@0 910 return TypeInt::make(lo->_lo,hi->_hi,3);
aoqi@0 911 }
aoqi@0 912 }
aoqi@0 913 }
aoqi@0 914
aoqi@0 915 // Until we have harmony between classes and interfaces in the type
aoqi@0 916 // lattice, we must tread carefully around phis which implicitly
aoqi@0 917 // convert the one to the other.
aoqi@0 918 const TypePtr* ttp = _type->make_ptr();
aoqi@0 919 const TypeInstPtr* ttip = (ttp != NULL) ? ttp->isa_instptr() : NULL;
aoqi@0 920 const TypeKlassPtr* ttkp = (ttp != NULL) ? ttp->isa_klassptr() : NULL;
aoqi@0 921 bool is_intf = false;
aoqi@0 922 if (ttip != NULL) {
aoqi@0 923 ciKlass* k = ttip->klass();
aoqi@0 924 if (k->is_loaded() && k->is_interface())
aoqi@0 925 is_intf = true;
aoqi@0 926 }
aoqi@0 927 if (ttkp != NULL) {
aoqi@0 928 ciKlass* k = ttkp->klass();
aoqi@0 929 if (k->is_loaded() && k->is_interface())
aoqi@0 930 is_intf = true;
aoqi@0 931 }
aoqi@0 932
aoqi@0 933 // Default case: merge all inputs
aoqi@0 934 const Type *t = Type::TOP; // Merged type starting value
aoqi@0 935 for (uint i = 1; i < req(); ++i) {// For all paths in
aoqi@0 936 // Reachable control path?
aoqi@0 937 if (r->in(i) && phase->type(r->in(i)) == Type::CONTROL) {
aoqi@0 938 const Type* ti = phase->type(in(i));
aoqi@0 939 // We assume that each input of an interface-valued Phi is a true
aoqi@0 940 // subtype of that interface. This might not be true of the meet
aoqi@0 941 // of all the input types. The lattice is not distributive in
aoqi@0 942 // such cases. Ward off asserts in type.cpp by refusing to do
aoqi@0 943 // meets between interfaces and proper classes.
aoqi@0 944 const TypePtr* tip = ti->make_ptr();
aoqi@0 945 const TypeInstPtr* tiip = (tip != NULL) ? tip->isa_instptr() : NULL;
aoqi@0 946 if (tiip) {
aoqi@0 947 bool ti_is_intf = false;
aoqi@0 948 ciKlass* k = tiip->klass();
aoqi@0 949 if (k->is_loaded() && k->is_interface())
aoqi@0 950 ti_is_intf = true;
aoqi@0 951 if (is_intf != ti_is_intf)
aoqi@0 952 { t = _type; break; }
aoqi@0 953 }
aoqi@0 954 t = t->meet_speculative(ti);
aoqi@0 955 }
aoqi@0 956 }
aoqi@0 957
aoqi@0 958 // The worst-case type (from ciTypeFlow) should be consistent with "t".
aoqi@0 959 // That is, we expect that "t->higher_equal(_type)" holds true.
aoqi@0 960 // There are various exceptions:
aoqi@0 961 // - Inputs which are phis might in fact be widened unnecessarily.
aoqi@0 962 // For example, an input might be a widened int while the phi is a short.
aoqi@0 963 // - Inputs might be BotPtrs but this phi is dependent on a null check,
aoqi@0 964 // and postCCP has removed the cast which encodes the result of the check.
aoqi@0 965 // - The type of this phi is an interface, and the inputs are classes.
aoqi@0 966 // - Value calls on inputs might produce fuzzy results.
aoqi@0 967 // (Occurrences of this case suggest improvements to Value methods.)
aoqi@0 968 //
aoqi@0 969 // It is not possible to see Type::BOTTOM values as phi inputs,
aoqi@0 970 // because the ciTypeFlow pre-pass produces verifier-quality types.
aoqi@0 971 const Type* ft = t->filter_speculative(_type); // Worst case type
aoqi@0 972
aoqi@0 973 #ifdef ASSERT
aoqi@0 974 // The following logic has been moved into TypeOopPtr::filter.
aoqi@0 975 const Type* jt = t->join_speculative(_type);
aoqi@0 976 if( jt->empty() ) { // Emptied out???
aoqi@0 977
aoqi@0 978 // Check for evil case of 't' being a class and '_type' expecting an
aoqi@0 979 // interface. This can happen because the bytecodes do not contain
aoqi@0 980 // enough type info to distinguish a Java-level interface variable
aoqi@0 981 // from a Java-level object variable. If we meet 2 classes which
aoqi@0 982 // both implement interface I, but their meet is at 'j/l/O' which
aoqi@0 983 // doesn't implement I, we have no way to tell if the result should
aoqi@0 984 // be 'I' or 'j/l/O'. Thus we'll pick 'j/l/O'. If this then flows
aoqi@0 985 // into a Phi which "knows" it's an Interface type we'll have to
aoqi@0 986 // uplift the type.
aoqi@0 987 if( !t->empty() && ttip && ttip->is_loaded() && ttip->klass()->is_interface() )
aoqi@0 988 { assert(ft == _type, ""); } // Uplift to interface
aoqi@0 989 else if( !t->empty() && ttkp && ttkp->is_loaded() && ttkp->klass()->is_interface() )
aoqi@0 990 { assert(ft == _type, ""); } // Uplift to interface
aoqi@0 991 // Otherwise it's something stupid like non-overlapping int ranges
aoqi@0 992 // found on dying counted loops.
aoqi@0 993 else
aoqi@0 994 { assert(ft == Type::TOP, ""); } // Canonical empty value
aoqi@0 995 }
aoqi@0 996
aoqi@0 997 else {
aoqi@0 998
aoqi@0 999 // If we have an interface-typed Phi and we narrow to a class type, the join
aoqi@0 1000 // should report back the class. However, if we have a J/L/Object
aoqi@0 1001 // class-typed Phi and an interface flows in, it's possible that the meet &
aoqi@0 1002 // join report an interface back out. This isn't possible but happens
aoqi@0 1003 // because the type system doesn't interact well with interfaces.
aoqi@0 1004 const TypePtr *jtp = jt->make_ptr();
aoqi@0 1005 const TypeInstPtr *jtip = (jtp != NULL) ? jtp->isa_instptr() : NULL;
aoqi@0 1006 const TypeKlassPtr *jtkp = (jtp != NULL) ? jtp->isa_klassptr() : NULL;
aoqi@0 1007 if( jtip && ttip ) {
aoqi@0 1008 if( jtip->is_loaded() && jtip->klass()->is_interface() &&
aoqi@0 1009 ttip->is_loaded() && !ttip->klass()->is_interface() ) {
aoqi@0 1010 // Happens in a CTW of rt.jar, 320-341, no extra flags
aoqi@0 1011 assert(ft == ttip->cast_to_ptr_type(jtip->ptr()) ||
aoqi@0 1012 ft->isa_narrowoop() && ft->make_ptr() == ttip->cast_to_ptr_type(jtip->ptr()), "");
aoqi@0 1013 jt = ft;
aoqi@0 1014 }
aoqi@0 1015 }
aoqi@0 1016 if( jtkp && ttkp ) {
aoqi@0 1017 if( jtkp->is_loaded() && jtkp->klass()->is_interface() &&
aoqi@0 1018 !jtkp->klass_is_exact() && // Keep exact interface klass (6894807)
aoqi@0 1019 ttkp->is_loaded() && !ttkp->klass()->is_interface() ) {
aoqi@0 1020 assert(ft == ttkp->cast_to_ptr_type(jtkp->ptr()) ||
aoqi@0 1021 ft->isa_narrowklass() && ft->make_ptr() == ttkp->cast_to_ptr_type(jtkp->ptr()), "");
aoqi@0 1022 jt = ft;
aoqi@0 1023 }
aoqi@0 1024 }
aoqi@0 1025 if (jt != ft && jt->base() == ft->base()) {
aoqi@0 1026 if (jt->isa_int() &&
aoqi@0 1027 jt->is_int()->_lo == ft->is_int()->_lo &&
aoqi@0 1028 jt->is_int()->_hi == ft->is_int()->_hi)
aoqi@0 1029 jt = ft;
aoqi@0 1030 if (jt->isa_long() &&
aoqi@0 1031 jt->is_long()->_lo == ft->is_long()->_lo &&
aoqi@0 1032 jt->is_long()->_hi == ft->is_long()->_hi)
aoqi@0 1033 jt = ft;
aoqi@0 1034 }
aoqi@0 1035 if (jt != ft) {
aoqi@0 1036 tty->print("merge type: "); t->dump(); tty->cr();
aoqi@0 1037 tty->print("kill type: "); _type->dump(); tty->cr();
aoqi@0 1038 tty->print("join type: "); jt->dump(); tty->cr();
aoqi@0 1039 tty->print("filter type: "); ft->dump(); tty->cr();
aoqi@0 1040 }
aoqi@0 1041 assert(jt == ft, "");
aoqi@0 1042 }
aoqi@0 1043 #endif //ASSERT
aoqi@0 1044
aoqi@0 1045 // Deal with conversion problems found in data loops.
aoqi@0 1046 ft = phase->saturate(ft, phase->type_or_null(this), _type);
aoqi@0 1047
aoqi@0 1048 return ft;
aoqi@0 1049 }
aoqi@0 1050
aoqi@0 1051
aoqi@0 1052 //------------------------------is_diamond_phi---------------------------------
aoqi@0 1053 // Does this Phi represent a simple well-shaped diamond merge? Return the
aoqi@0 1054 // index of the true path or 0 otherwise.
aoqi@0 1055 // If check_control_only is true, do not inspect the If node at the
aoqi@0 1056 // top, and return -1 (not an edge number) on success.
aoqi@0 1057 int PhiNode::is_diamond_phi(bool check_control_only) const {
aoqi@0 1058 // Check for a 2-path merge
aoqi@0 1059 Node *region = in(0);
aoqi@0 1060 if( !region ) return 0;
aoqi@0 1061 if( region->req() != 3 ) return 0;
aoqi@0 1062 if( req() != 3 ) return 0;
aoqi@0 1063 // Check that both paths come from the same If
aoqi@0 1064 Node *ifp1 = region->in(1);
aoqi@0 1065 Node *ifp2 = region->in(2);
aoqi@0 1066 if( !ifp1 || !ifp2 ) return 0;
aoqi@0 1067 Node *iff = ifp1->in(0);
aoqi@0 1068 if( !iff || !iff->is_If() ) return 0;
aoqi@0 1069 if( iff != ifp2->in(0) ) return 0;
aoqi@0 1070 if (check_control_only) return -1;
aoqi@0 1071 // Check for a proper bool/cmp
aoqi@0 1072 const Node *b = iff->in(1);
aoqi@0 1073 if( !b->is_Bool() ) return 0;
aoqi@0 1074 const Node *cmp = b->in(1);
aoqi@0 1075 if( !cmp->is_Cmp() ) return 0;
aoqi@0 1076
aoqi@0 1077 // Check for branching opposite expected
aoqi@0 1078 if( ifp2->Opcode() == Op_IfTrue ) {
aoqi@0 1079 assert( ifp1->Opcode() == Op_IfFalse, "" );
aoqi@0 1080 return 2;
aoqi@0 1081 } else {
aoqi@0 1082 assert( ifp1->Opcode() == Op_IfTrue, "" );
aoqi@0 1083 return 1;
aoqi@0 1084 }
aoqi@0 1085 }
aoqi@0 1086
aoqi@0 1087 //----------------------------check_cmove_id-----------------------------------
aoqi@0 1088 // Check for CMove'ing a constant after comparing against the constant.
aoqi@0 1089 // Happens all the time now, since if we compare equality vs a constant in
aoqi@0 1090 // the parser, we "know" the variable is constant on one path and we force
aoqi@0 1091 // it. Thus code like "if( x==0 ) {/*EMPTY*/}" ends up inserting a
aoqi@0 1092 // conditional move: "x = (x==0)?0:x;". Yucko. This fix is slightly more
aoqi@0 1093 // general in that we don't need constants. Since CMove's are only inserted
aoqi@0 1094 // in very special circumstances, we do it here on generic Phi's.
aoqi@0 1095 Node* PhiNode::is_cmove_id(PhaseTransform* phase, int true_path) {
aoqi@0 1096 assert(true_path !=0, "only diamond shape graph expected");
aoqi@0 1097
aoqi@0 1098 // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
aoqi@0 1099 // phi->region->if_proj->ifnode->bool->cmp
aoqi@0 1100 Node* region = in(0);
aoqi@0 1101 Node* iff = region->in(1)->in(0);
aoqi@0 1102 BoolNode* b = iff->in(1)->as_Bool();
aoqi@0 1103 Node* cmp = b->in(1);
aoqi@0 1104 Node* tval = in(true_path);
aoqi@0 1105 Node* fval = in(3-true_path);
aoqi@0 1106 Node* id = CMoveNode::is_cmove_id(phase, cmp, tval, fval, b);
aoqi@0 1107 if (id == NULL)
aoqi@0 1108 return NULL;
aoqi@0 1109
aoqi@0 1110 // Either value might be a cast that depends on a branch of 'iff'.
aoqi@0 1111 // Since the 'id' value will float free of the diamond, either
aoqi@0 1112 // decast or return failure.
aoqi@0 1113 Node* ctl = id->in(0);
aoqi@0 1114 if (ctl != NULL && ctl->in(0) == iff) {
aoqi@0 1115 if (id->is_ConstraintCast()) {
aoqi@0 1116 return id->in(1);
aoqi@0 1117 } else {
aoqi@0 1118 // Don't know how to disentangle this value.
aoqi@0 1119 return NULL;
aoqi@0 1120 }
aoqi@0 1121 }
aoqi@0 1122
aoqi@0 1123 return id;
aoqi@0 1124 }
aoqi@0 1125
aoqi@0 1126 //------------------------------Identity---------------------------------------
aoqi@0 1127 // Check for Region being Identity.
aoqi@0 1128 Node *PhiNode::Identity( PhaseTransform *phase ) {
aoqi@0 1129 // Check for no merging going on
aoqi@0 1130 // (There used to be special-case code here when this->region->is_Loop.
aoqi@0 1131 // It would check for a tributary phi on the backedge that the main phi
aoqi@0 1132 // trivially, perhaps with a single cast. The unique_input method
aoqi@0 1133 // does all this and more, by reducing such tributaries to 'this'.)
aoqi@0 1134 Node* uin = unique_input(phase);
aoqi@0 1135 if (uin != NULL) {
aoqi@0 1136 return uin;
aoqi@0 1137 }
aoqi@0 1138
aoqi@0 1139 int true_path = is_diamond_phi();
aoqi@0 1140 if (true_path != 0) {
aoqi@0 1141 Node* id = is_cmove_id(phase, true_path);
aoqi@0 1142 if (id != NULL) return id;
aoqi@0 1143 }
aoqi@0 1144
aoqi@0 1145 return this; // No identity
aoqi@0 1146 }
aoqi@0 1147
aoqi@0 1148 //-----------------------------unique_input------------------------------------
aoqi@0 1149 // Find the unique value, discounting top, self-loops, and casts.
aoqi@0 1150 // Return top if there are no inputs, and self if there are multiple.
aoqi@0 1151 Node* PhiNode::unique_input(PhaseTransform* phase) {
aoqi@0 1152 // 1) One unique direct input, or
aoqi@0 1153 // 2) some of the inputs have an intervening ConstraintCast and
aoqi@0 1154 // the type of input is the same or sharper (more specific)
aoqi@0 1155 // than the phi's type.
aoqi@0 1156 // 3) an input is a self loop
aoqi@0 1157 //
aoqi@0 1158 // 1) input or 2) input or 3) input __
aoqi@0 1159 // / \ / \ \ / \
aoqi@0 1160 // \ / | cast phi cast
aoqi@0 1161 // phi \ / / \ /
aoqi@0 1162 // phi / --
aoqi@0 1163
aoqi@0 1164 Node* r = in(0); // RegionNode
aoqi@0 1165 if (r == NULL) return in(1); // Already degraded to a Copy
aoqi@0 1166 Node* uncasted_input = NULL; // The unique uncasted input (ConstraintCasts removed)
aoqi@0 1167 Node* direct_input = NULL; // The unique direct input
aoqi@0 1168
aoqi@0 1169 for (uint i = 1, cnt = req(); i < cnt; ++i) {
aoqi@0 1170 Node* rc = r->in(i);
aoqi@0 1171 if (rc == NULL || phase->type(rc) == Type::TOP)
aoqi@0 1172 continue; // ignore unreachable control path
aoqi@0 1173 Node* n = in(i);
aoqi@0 1174 if (n == NULL)
aoqi@0 1175 continue;
aoqi@0 1176 Node* un = n->uncast();
aoqi@0 1177 if (un == NULL || un == this || phase->type(un) == Type::TOP) {
aoqi@0 1178 continue; // ignore if top, or in(i) and "this" are in a data cycle
aoqi@0 1179 }
aoqi@0 1180 // Check for a unique uncasted input
aoqi@0 1181 if (uncasted_input == NULL) {
aoqi@0 1182 uncasted_input = un;
aoqi@0 1183 } else if (uncasted_input != un) {
aoqi@0 1184 uncasted_input = NodeSentinel; // no unique uncasted input
aoqi@0 1185 }
aoqi@0 1186 // Check for a unique direct input
aoqi@0 1187 if (direct_input == NULL) {
aoqi@0 1188 direct_input = n;
aoqi@0 1189 } else if (direct_input != n) {
aoqi@0 1190 direct_input = NodeSentinel; // no unique direct input
aoqi@0 1191 }
aoqi@0 1192 }
aoqi@0 1193 if (direct_input == NULL) {
aoqi@0 1194 return phase->C->top(); // no inputs
aoqi@0 1195 }
aoqi@0 1196 assert(uncasted_input != NULL,"");
aoqi@0 1197
aoqi@0 1198 if (direct_input != NodeSentinel) {
aoqi@0 1199 return direct_input; // one unique direct input
aoqi@0 1200 }
aoqi@0 1201 if (uncasted_input != NodeSentinel &&
aoqi@0 1202 phase->type(uncasted_input)->higher_equal(type())) {
aoqi@0 1203 return uncasted_input; // one unique uncasted input
aoqi@0 1204 }
aoqi@0 1205
aoqi@0 1206 // Nothing.
aoqi@0 1207 return NULL;
aoqi@0 1208 }
aoqi@0 1209
aoqi@0 1210 //------------------------------is_x2logic-------------------------------------
aoqi@0 1211 // Check for simple convert-to-boolean pattern
aoqi@0 1212 // If:(C Bool) Region:(IfF IfT) Phi:(Region 0 1)
aoqi@0 1213 // Convert Phi to an ConvIB.
aoqi@0 1214 static Node *is_x2logic( PhaseGVN *phase, PhiNode *phi, int true_path ) {
aoqi@0 1215 assert(true_path !=0, "only diamond shape graph expected");
aoqi@0 1216 // Convert the true/false index into an expected 0/1 return.
aoqi@0 1217 // Map 2->0 and 1->1.
aoqi@0 1218 int flipped = 2-true_path;
aoqi@0 1219
aoqi@0 1220 // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
aoqi@0 1221 // phi->region->if_proj->ifnode->bool->cmp
aoqi@0 1222 Node *region = phi->in(0);
aoqi@0 1223 Node *iff = region->in(1)->in(0);
aoqi@0 1224 BoolNode *b = (BoolNode*)iff->in(1);
aoqi@0 1225 const CmpNode *cmp = (CmpNode*)b->in(1);
aoqi@0 1226
aoqi@0 1227 Node *zero = phi->in(1);
aoqi@0 1228 Node *one = phi->in(2);
aoqi@0 1229 const Type *tzero = phase->type( zero );
aoqi@0 1230 const Type *tone = phase->type( one );
aoqi@0 1231
aoqi@0 1232 // Check for compare vs 0
aoqi@0 1233 const Type *tcmp = phase->type(cmp->in(2));
aoqi@0 1234 if( tcmp != TypeInt::ZERO && tcmp != TypePtr::NULL_PTR ) {
aoqi@0 1235 // Allow cmp-vs-1 if the other input is bounded by 0-1
aoqi@0 1236 if( !(tcmp == TypeInt::ONE && phase->type(cmp->in(1)) == TypeInt::BOOL) )
aoqi@0 1237 return NULL;
aoqi@0 1238 flipped = 1-flipped; // Test is vs 1 instead of 0!
aoqi@0 1239 }
aoqi@0 1240
aoqi@0 1241 // Check for setting zero/one opposite expected
aoqi@0 1242 if( tzero == TypeInt::ZERO ) {
aoqi@0 1243 if( tone == TypeInt::ONE ) {
aoqi@0 1244 } else return NULL;
aoqi@0 1245 } else if( tzero == TypeInt::ONE ) {
aoqi@0 1246 if( tone == TypeInt::ZERO ) {
aoqi@0 1247 flipped = 1-flipped;
aoqi@0 1248 } else return NULL;
aoqi@0 1249 } else return NULL;
aoqi@0 1250
aoqi@0 1251 // Check for boolean test backwards
aoqi@0 1252 if( b->_test._test == BoolTest::ne ) {
aoqi@0 1253 } else if( b->_test._test == BoolTest::eq ) {
aoqi@0 1254 flipped = 1-flipped;
aoqi@0 1255 } else return NULL;
aoqi@0 1256
aoqi@0 1257 // Build int->bool conversion
aoqi@0 1258 Node *n = new (phase->C) Conv2BNode( cmp->in(1) );
aoqi@0 1259 if( flipped )
aoqi@0 1260 n = new (phase->C) XorINode( phase->transform(n), phase->intcon(1) );
aoqi@0 1261
aoqi@0 1262 return n;
aoqi@0 1263 }
aoqi@0 1264
aoqi@0 1265 //------------------------------is_cond_add------------------------------------
aoqi@0 1266 // Check for simple conditional add pattern: "(P < Q) ? X+Y : X;"
aoqi@0 1267 // To be profitable the control flow has to disappear; there can be no other
aoqi@0 1268 // values merging here. We replace the test-and-branch with:
aoqi@0 1269 // "(sgn(P-Q))&Y) + X". Basically, convert "(P < Q)" into 0 or -1 by
aoqi@0 1270 // moving the carry bit from (P-Q) into a register with 'sbb EAX,EAX'.
aoqi@0 1271 // Then convert Y to 0-or-Y and finally add.
aoqi@0 1272 // This is a key transform for SpecJava _201_compress.
aoqi@0 1273 static Node* is_cond_add(PhaseGVN *phase, PhiNode *phi, int true_path) {
aoqi@0 1274 assert(true_path !=0, "only diamond shape graph expected");
aoqi@0 1275
aoqi@0 1276 // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
aoqi@0 1277 // phi->region->if_proj->ifnode->bool->cmp
aoqi@0 1278 RegionNode *region = (RegionNode*)phi->in(0);
aoqi@0 1279 Node *iff = region->in(1)->in(0);
aoqi@0 1280 BoolNode* b = iff->in(1)->as_Bool();
aoqi@0 1281 const CmpNode *cmp = (CmpNode*)b->in(1);
aoqi@0 1282
aoqi@0 1283 // Make sure only merging this one phi here
aoqi@0 1284 if (region->has_unique_phi() != phi) return NULL;
aoqi@0 1285
aoqi@0 1286 // Make sure each arm of the diamond has exactly one output, which we assume
aoqi@0 1287 // is the region. Otherwise, the control flow won't disappear.
aoqi@0 1288 if (region->in(1)->outcnt() != 1) return NULL;
aoqi@0 1289 if (region->in(2)->outcnt() != 1) return NULL;
aoqi@0 1290
aoqi@0 1291 // Check for "(P < Q)" of type signed int
aoqi@0 1292 if (b->_test._test != BoolTest::lt) return NULL;
aoqi@0 1293 if (cmp->Opcode() != Op_CmpI) return NULL;
aoqi@0 1294
aoqi@0 1295 Node *p = cmp->in(1);
aoqi@0 1296 Node *q = cmp->in(2);
aoqi@0 1297 Node *n1 = phi->in( true_path);
aoqi@0 1298 Node *n2 = phi->in(3-true_path);
aoqi@0 1299
aoqi@0 1300 int op = n1->Opcode();
aoqi@0 1301 if( op != Op_AddI // Need zero as additive identity
aoqi@0 1302 /*&&op != Op_SubI &&
aoqi@0 1303 op != Op_AddP &&
aoqi@0 1304 op != Op_XorI &&
aoqi@0 1305 op != Op_OrI*/ )
aoqi@0 1306 return NULL;
aoqi@0 1307
aoqi@0 1308 Node *x = n2;
aoqi@0 1309 Node *y = NULL;
aoqi@0 1310 if( x == n1->in(1) ) {
aoqi@0 1311 y = n1->in(2);
aoqi@0 1312 } else if( x == n1->in(2) ) {
aoqi@0 1313 y = n1->in(1);
aoqi@0 1314 } else return NULL;
aoqi@0 1315
aoqi@0 1316 // Not so profitable if compare and add are constants
aoqi@0 1317 if( q->is_Con() && phase->type(q) != TypeInt::ZERO && y->is_Con() )
aoqi@0 1318 return NULL;
aoqi@0 1319
aoqi@0 1320 Node *cmplt = phase->transform( new (phase->C) CmpLTMaskNode(p,q) );
aoqi@0 1321 Node *j_and = phase->transform( new (phase->C) AndINode(cmplt,y) );
aoqi@0 1322 return new (phase->C) AddINode(j_and,x);
aoqi@0 1323 }
aoqi@0 1324
aoqi@0 1325 //------------------------------is_absolute------------------------------------
aoqi@0 1326 // Check for absolute value.
aoqi@0 1327 static Node* is_absolute( PhaseGVN *phase, PhiNode *phi_root, int true_path) {
aoqi@0 1328 assert(true_path !=0, "only diamond shape graph expected");
aoqi@0 1329
aoqi@0 1330 int cmp_zero_idx = 0; // Index of compare input where to look for zero
aoqi@0 1331 int phi_x_idx = 0; // Index of phi input where to find naked x
aoqi@0 1332
aoqi@0 1333 // ABS ends with the merge of 2 control flow paths.
aoqi@0 1334 // Find the false path from the true path. With only 2 inputs, 3 - x works nicely.
aoqi@0 1335 int false_path = 3 - true_path;
aoqi@0 1336
aoqi@0 1337 // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
aoqi@0 1338 // phi->region->if_proj->ifnode->bool->cmp
aoqi@0 1339 BoolNode *bol = phi_root->in(0)->in(1)->in(0)->in(1)->as_Bool();
aoqi@0 1340
aoqi@0 1341 // Check bool sense
aoqi@0 1342 switch( bol->_test._test ) {
aoqi@0 1343 case BoolTest::lt: cmp_zero_idx = 1; phi_x_idx = true_path; break;
aoqi@0 1344 case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = false_path; break;
aoqi@0 1345 case BoolTest::gt: cmp_zero_idx = 2; phi_x_idx = true_path; break;
aoqi@0 1346 case BoolTest::ge: cmp_zero_idx = 1; phi_x_idx = false_path; break;
aoqi@0 1347 default: return NULL; break;
aoqi@0 1348 }
aoqi@0 1349
aoqi@0 1350 // Test is next
aoqi@0 1351 Node *cmp = bol->in(1);
aoqi@0 1352 const Type *tzero = NULL;
aoqi@0 1353 switch( cmp->Opcode() ) {
aoqi@0 1354 case Op_CmpF: tzero = TypeF::ZERO; break; // Float ABS
aoqi@0 1355 case Op_CmpD: tzero = TypeD::ZERO; break; // Double ABS
aoqi@0 1356 default: return NULL;
aoqi@0 1357 }
aoqi@0 1358
aoqi@0 1359 // Find zero input of compare; the other input is being abs'd
aoqi@0 1360 Node *x = NULL;
aoqi@0 1361 bool flip = false;
aoqi@0 1362 if( phase->type(cmp->in(cmp_zero_idx)) == tzero ) {
aoqi@0 1363 x = cmp->in(3 - cmp_zero_idx);
aoqi@0 1364 } else if( phase->type(cmp->in(3 - cmp_zero_idx)) == tzero ) {
aoqi@0 1365 // The test is inverted, we should invert the result...
aoqi@0 1366 x = cmp->in(cmp_zero_idx);
aoqi@0 1367 flip = true;
aoqi@0 1368 } else {
aoqi@0 1369 return NULL;
aoqi@0 1370 }
aoqi@0 1371
aoqi@0 1372 // Next get the 2 pieces being selected, one is the original value
aoqi@0 1373 // and the other is the negated value.
aoqi@0 1374 if( phi_root->in(phi_x_idx) != x ) return NULL;
aoqi@0 1375
aoqi@0 1376 // Check other phi input for subtract node
aoqi@0 1377 Node *sub = phi_root->in(3 - phi_x_idx);
aoqi@0 1378
aoqi@0 1379 // Allow only Sub(0,X) and fail out for all others; Neg is not OK
aoqi@0 1380 if( tzero == TypeF::ZERO ) {
aoqi@0 1381 if( sub->Opcode() != Op_SubF ||
aoqi@0 1382 sub->in(2) != x ||
aoqi@0 1383 phase->type(sub->in(1)) != tzero ) return NULL;
aoqi@0 1384 x = new (phase->C) AbsFNode(x);
aoqi@0 1385 if (flip) {
aoqi@0 1386 x = new (phase->C) SubFNode(sub->in(1), phase->transform(x));
aoqi@0 1387 }
aoqi@0 1388 } else {
aoqi@0 1389 if( sub->Opcode() != Op_SubD ||
aoqi@0 1390 sub->in(2) != x ||
aoqi@0 1391 phase->type(sub->in(1)) != tzero ) return NULL;
aoqi@0 1392 x = new (phase->C) AbsDNode(x);
aoqi@0 1393 if (flip) {
aoqi@0 1394 x = new (phase->C) SubDNode(sub->in(1), phase->transform(x));
aoqi@0 1395 }
aoqi@0 1396 }
aoqi@0 1397
aoqi@0 1398 return x;
aoqi@0 1399 }
aoqi@0 1400
aoqi@0 1401 //------------------------------split_once-------------------------------------
aoqi@0 1402 // Helper for split_flow_path
aoqi@0 1403 static void split_once(PhaseIterGVN *igvn, Node *phi, Node *val, Node *n, Node *newn) {
aoqi@0 1404 igvn->hash_delete(n); // Remove from hash before hacking edges
aoqi@0 1405
aoqi@0 1406 uint j = 1;
aoqi@0 1407 for (uint i = phi->req()-1; i > 0; i--) {
aoqi@0 1408 if (phi->in(i) == val) { // Found a path with val?
aoqi@0 1409 // Add to NEW Region/Phi, no DU info
aoqi@0 1410 newn->set_req( j++, n->in(i) );
aoqi@0 1411 // Remove from OLD Region/Phi
aoqi@0 1412 n->del_req(i);
aoqi@0 1413 }
aoqi@0 1414 }
aoqi@0 1415
aoqi@0 1416 // Register the new node but do not transform it. Cannot transform until the
aoqi@0 1417 // entire Region/Phi conglomerate has been hacked as a single huge transform.
aoqi@0 1418 igvn->register_new_node_with_optimizer( newn );
aoqi@0 1419
aoqi@0 1420 // Now I can point to the new node.
aoqi@0 1421 n->add_req(newn);
aoqi@0 1422 igvn->_worklist.push(n);
aoqi@0 1423 }
aoqi@0 1424
aoqi@0 1425 //------------------------------split_flow_path--------------------------------
aoqi@0 1426 // Check for merging identical values and split flow paths
aoqi@0 1427 static Node* split_flow_path(PhaseGVN *phase, PhiNode *phi) {
aoqi@0 1428 BasicType bt = phi->type()->basic_type();
aoqi@0 1429 if( bt == T_ILLEGAL || type2size[bt] <= 0 )
aoqi@0 1430 return NULL; // Bail out on funny non-value stuff
aoqi@0 1431 if( phi->req() <= 3 ) // Need at least 2 matched inputs and a
aoqi@0 1432 return NULL; // third unequal input to be worth doing
aoqi@0 1433
aoqi@0 1434 // Scan for a constant
aoqi@0 1435 uint i;
aoqi@0 1436 for( i = 1; i < phi->req()-1; i++ ) {
aoqi@0 1437 Node *n = phi->in(i);
aoqi@0 1438 if( !n ) return NULL;
aoqi@0 1439 if( phase->type(n) == Type::TOP ) return NULL;
aoqi@0 1440 if( n->Opcode() == Op_ConP || n->Opcode() == Op_ConN || n->Opcode() == Op_ConNKlass )
aoqi@0 1441 break;
aoqi@0 1442 }
aoqi@0 1443 if( i >= phi->req() ) // Only split for constants
aoqi@0 1444 return NULL;
aoqi@0 1445
aoqi@0 1446 Node *val = phi->in(i); // Constant to split for
aoqi@0 1447 uint hit = 0; // Number of times it occurs
aoqi@0 1448 Node *r = phi->region();
aoqi@0 1449
aoqi@0 1450 for( ; i < phi->req(); i++ ){ // Count occurrences of constant
aoqi@0 1451 Node *n = phi->in(i);
aoqi@0 1452 if( !n ) return NULL;
aoqi@0 1453 if( phase->type(n) == Type::TOP ) return NULL;
aoqi@0 1454 if( phi->in(i) == val ) {
aoqi@0 1455 hit++;
aoqi@0 1456 if (PhaseIdealLoop::find_predicate(r->in(i)) != NULL) {
aoqi@0 1457 return NULL; // don't split loop entry path
aoqi@0 1458 }
aoqi@0 1459 }
aoqi@0 1460 }
aoqi@0 1461
aoqi@0 1462 if( hit <= 1 || // Make sure we find 2 or more
aoqi@0 1463 hit == phi->req()-1 ) // and not ALL the same value
aoqi@0 1464 return NULL;
aoqi@0 1465
aoqi@0 1466 // Now start splitting out the flow paths that merge the same value.
aoqi@0 1467 // Split first the RegionNode.
aoqi@0 1468 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 1469 RegionNode *newr = new (phase->C) RegionNode(hit+1);
aoqi@0 1470 split_once(igvn, phi, val, r, newr);
aoqi@0 1471
aoqi@0 1472 // Now split all other Phis than this one
aoqi@0 1473 for (DUIterator_Fast kmax, k = r->fast_outs(kmax); k < kmax; k++) {
aoqi@0 1474 Node* phi2 = r->fast_out(k);
aoqi@0 1475 if( phi2->is_Phi() && phi2->as_Phi() != phi ) {
aoqi@0 1476 PhiNode *newphi = PhiNode::make_blank(newr, phi2);
aoqi@0 1477 split_once(igvn, phi, val, phi2, newphi);
aoqi@0 1478 }
aoqi@0 1479 }
aoqi@0 1480
aoqi@0 1481 // Clean up this guy
aoqi@0 1482 igvn->hash_delete(phi);
aoqi@0 1483 for( i = phi->req()-1; i > 0; i-- ) {
aoqi@0 1484 if( phi->in(i) == val ) {
aoqi@0 1485 phi->del_req(i);
aoqi@0 1486 }
aoqi@0 1487 }
aoqi@0 1488 phi->add_req(val);
aoqi@0 1489
aoqi@0 1490 return phi;
aoqi@0 1491 }
aoqi@0 1492
aoqi@0 1493 //=============================================================================
aoqi@0 1494 //------------------------------simple_data_loop_check-------------------------
aoqi@0 1495 // Try to determining if the phi node in a simple safe/unsafe data loop.
aoqi@0 1496 // Returns:
aoqi@0 1497 // enum LoopSafety { Safe = 0, Unsafe, UnsafeLoop };
aoqi@0 1498 // Safe - safe case when the phi and it's inputs reference only safe data
aoqi@0 1499 // nodes;
aoqi@0 1500 // Unsafe - the phi and it's inputs reference unsafe data nodes but there
aoqi@0 1501 // is no reference back to the phi - need a graph walk
aoqi@0 1502 // to determine if it is in a loop;
aoqi@0 1503 // UnsafeLoop - unsafe case when the phi references itself directly or through
aoqi@0 1504 // unsafe data node.
aoqi@0 1505 // Note: a safe data node is a node which could/never reference itself during
aoqi@0 1506 // GVN transformations. For now it is Con, Proj, Phi, CastPP, CheckCastPP.
aoqi@0 1507 // I mark Phi nodes as safe node not only because they can reference itself
aoqi@0 1508 // but also to prevent mistaking the fallthrough case inside an outer loop
aoqi@0 1509 // as dead loop when the phi references itselfs through an other phi.
aoqi@0 1510 PhiNode::LoopSafety PhiNode::simple_data_loop_check(Node *in) const {
aoqi@0 1511 // It is unsafe loop if the phi node references itself directly.
aoqi@0 1512 if (in == (Node*)this)
aoqi@0 1513 return UnsafeLoop; // Unsafe loop
aoqi@0 1514 // Unsafe loop if the phi node references itself through an unsafe data node.
aoqi@0 1515 // Exclude cases with null inputs or data nodes which could reference
aoqi@0 1516 // itself (safe for dead loops).
aoqi@0 1517 if (in != NULL && !in->is_dead_loop_safe()) {
aoqi@0 1518 // Check inputs of phi's inputs also.
aoqi@0 1519 // It is much less expensive then full graph walk.
aoqi@0 1520 uint cnt = in->req();
aoqi@0 1521 uint i = (in->is_Proj() && !in->is_CFG()) ? 0 : 1;
aoqi@0 1522 for (; i < cnt; ++i) {
aoqi@0 1523 Node* m = in->in(i);
aoqi@0 1524 if (m == (Node*)this)
aoqi@0 1525 return UnsafeLoop; // Unsafe loop
aoqi@0 1526 if (m != NULL && !m->is_dead_loop_safe()) {
aoqi@0 1527 // Check the most common case (about 30% of all cases):
aoqi@0 1528 // phi->Load/Store->AddP->(ConP ConP Con)/(Parm Parm Con).
aoqi@0 1529 Node *m1 = (m->is_AddP() && m->req() > 3) ? m->in(1) : NULL;
aoqi@0 1530 if (m1 == (Node*)this)
aoqi@0 1531 return UnsafeLoop; // Unsafe loop
aoqi@0 1532 if (m1 != NULL && m1 == m->in(2) &&
aoqi@0 1533 m1->is_dead_loop_safe() && m->in(3)->is_Con()) {
aoqi@0 1534 continue; // Safe case
aoqi@0 1535 }
aoqi@0 1536 // The phi references an unsafe node - need full analysis.
aoqi@0 1537 return Unsafe;
aoqi@0 1538 }
aoqi@0 1539 }
aoqi@0 1540 }
aoqi@0 1541 return Safe; // Safe case - we can optimize the phi node.
aoqi@0 1542 }
aoqi@0 1543
aoqi@0 1544 //------------------------------is_unsafe_data_reference-----------------------
aoqi@0 1545 // If phi can be reached through the data input - it is data loop.
aoqi@0 1546 bool PhiNode::is_unsafe_data_reference(Node *in) const {
aoqi@0 1547 assert(req() > 1, "");
aoqi@0 1548 // First, check simple cases when phi references itself directly or
aoqi@0 1549 // through an other node.
aoqi@0 1550 LoopSafety safety = simple_data_loop_check(in);
aoqi@0 1551 if (safety == UnsafeLoop)
aoqi@0 1552 return true; // phi references itself - unsafe loop
aoqi@0 1553 else if (safety == Safe)
aoqi@0 1554 return false; // Safe case - phi could be replaced with the unique input.
aoqi@0 1555
aoqi@0 1556 // Unsafe case when we should go through data graph to determine
aoqi@0 1557 // if the phi references itself.
aoqi@0 1558
aoqi@0 1559 ResourceMark rm;
aoqi@0 1560
aoqi@0 1561 Arena *a = Thread::current()->resource_area();
aoqi@0 1562 Node_List nstack(a);
aoqi@0 1563 VectorSet visited(a);
aoqi@0 1564
aoqi@0 1565 nstack.push(in); // Start with unique input.
aoqi@0 1566 visited.set(in->_idx);
aoqi@0 1567 while (nstack.size() != 0) {
aoqi@0 1568 Node* n = nstack.pop();
aoqi@0 1569 uint cnt = n->req();
aoqi@0 1570 uint i = (n->is_Proj() && !n->is_CFG()) ? 0 : 1;
aoqi@0 1571 for (; i < cnt; i++) {
aoqi@0 1572 Node* m = n->in(i);
aoqi@0 1573 if (m == (Node*)this) {
aoqi@0 1574 return true; // Data loop
aoqi@0 1575 }
aoqi@0 1576 if (m != NULL && !m->is_dead_loop_safe()) { // Only look for unsafe cases.
aoqi@0 1577 if (!visited.test_set(m->_idx))
aoqi@0 1578 nstack.push(m);
aoqi@0 1579 }
aoqi@0 1580 }
aoqi@0 1581 }
aoqi@0 1582 return false; // The phi is not reachable from its inputs
aoqi@0 1583 }
aoqi@0 1584
aoqi@0 1585
aoqi@0 1586 //------------------------------Ideal------------------------------------------
aoqi@0 1587 // Return a node which is more "ideal" than the current node. Must preserve
aoqi@0 1588 // the CFG, but we can still strip out dead paths.
aoqi@0 1589 Node *PhiNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 1590 // The next should never happen after 6297035 fix.
aoqi@0 1591 if( is_copy() ) // Already degraded to a Copy ?
aoqi@0 1592 return NULL; // No change
aoqi@0 1593
aoqi@0 1594 Node *r = in(0); // RegionNode
aoqi@0 1595 assert(r->in(0) == NULL || !r->in(0)->is_Root(), "not a specially hidden merge");
aoqi@0 1596
aoqi@0 1597 // Note: During parsing, phis are often transformed before their regions.
aoqi@0 1598 // This means we have to use type_or_null to defend against untyped regions.
aoqi@0 1599 if( phase->type_or_null(r) == Type::TOP ) // Dead code?
aoqi@0 1600 return NULL; // No change
aoqi@0 1601
aoqi@0 1602 Node *top = phase->C->top();
aoqi@0 1603 bool new_phi = (outcnt() == 0); // transforming new Phi
aoqi@0 1604 // No change for igvn if new phi is not hooked
aoqi@0 1605 if (new_phi && can_reshape)
aoqi@0 1606 return NULL;
aoqi@0 1607
aoqi@0 1608 // The are 2 situations when only one valid phi's input is left
aoqi@0 1609 // (in addition to Region input).
aoqi@0 1610 // One: region is not loop - replace phi with this input.
aoqi@0 1611 // Two: region is loop - replace phi with top since this data path is dead
aoqi@0 1612 // and we need to break the dead data loop.
aoqi@0 1613 Node* progress = NULL; // Record if any progress made
aoqi@0 1614 for( uint j = 1; j < req(); ++j ){ // For all paths in
aoqi@0 1615 // Check unreachable control paths
aoqi@0 1616 Node* rc = r->in(j);
aoqi@0 1617 Node* n = in(j); // Get the input
aoqi@0 1618 if (rc == NULL || phase->type(rc) == Type::TOP) {
aoqi@0 1619 if (n != top) { // Not already top?
aoqi@0 1620 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 1621 if (can_reshape && igvn != NULL) {
aoqi@0 1622 igvn->_worklist.push(r);
aoqi@0 1623 }
aoqi@0 1624 set_req(j, top); // Nuke it down
aoqi@0 1625 progress = this; // Record progress
aoqi@0 1626 }
aoqi@0 1627 }
aoqi@0 1628 }
aoqi@0 1629
aoqi@0 1630 if (can_reshape && outcnt() == 0) {
aoqi@0 1631 // set_req() above may kill outputs if Phi is referenced
aoqi@0 1632 // only by itself on the dead (top) control path.
aoqi@0 1633 return top;
aoqi@0 1634 }
aoqi@0 1635
aoqi@0 1636 Node* uin = unique_input(phase);
aoqi@0 1637 if (uin == top) { // Simplest case: no alive inputs.
aoqi@0 1638 if (can_reshape) // IGVN transformation
aoqi@0 1639 return top;
aoqi@0 1640 else
aoqi@0 1641 return NULL; // Identity will return TOP
aoqi@0 1642 } else if (uin != NULL) {
aoqi@0 1643 // Only one not-NULL unique input path is left.
aoqi@0 1644 // Determine if this input is backedge of a loop.
aoqi@0 1645 // (Skip new phis which have no uses and dead regions).
aoqi@0 1646 if (outcnt() > 0 && r->in(0) != NULL) {
aoqi@0 1647 // First, take the short cut when we know it is a loop and
aoqi@0 1648 // the EntryControl data path is dead.
aoqi@0 1649 // Loop node may have only one input because entry path
aoqi@0 1650 // is removed in PhaseIdealLoop::Dominators().
aoqi@0 1651 assert(!r->is_Loop() || r->req() <= 3, "Loop node should have 3 or less inputs");
aoqi@0 1652 bool is_loop = (r->is_Loop() && r->req() == 3);
aoqi@0 1653 // Then, check if there is a data loop when phi references itself directly
aoqi@0 1654 // or through other data nodes.
aoqi@0 1655 if (is_loop && !uin->eqv_uncast(in(LoopNode::EntryControl)) ||
aoqi@0 1656 !is_loop && is_unsafe_data_reference(uin)) {
aoqi@0 1657 // Break this data loop to avoid creation of a dead loop.
aoqi@0 1658 if (can_reshape) {
aoqi@0 1659 return top;
aoqi@0 1660 } else {
aoqi@0 1661 // We can't return top if we are in Parse phase - cut inputs only
aoqi@0 1662 // let Identity to handle the case.
aoqi@0 1663 replace_edge(uin, top);
aoqi@0 1664 return NULL;
aoqi@0 1665 }
aoqi@0 1666 }
aoqi@0 1667 }
aoqi@0 1668
aoqi@0 1669 // One unique input.
aoqi@0 1670 debug_only(Node* ident = Identity(phase));
aoqi@0 1671 // The unique input must eventually be detected by the Identity call.
aoqi@0 1672 #ifdef ASSERT
aoqi@0 1673 if (ident != uin && !ident->is_top()) {
aoqi@0 1674 // print this output before failing assert
aoqi@0 1675 r->dump(3);
aoqi@0 1676 this->dump(3);
aoqi@0 1677 ident->dump();
aoqi@0 1678 uin->dump();
aoqi@0 1679 }
aoqi@0 1680 #endif
aoqi@0 1681 assert(ident == uin || ident->is_top(), "Identity must clean this up");
aoqi@0 1682 return NULL;
aoqi@0 1683 }
aoqi@0 1684
aoqi@0 1685
aoqi@0 1686 Node* opt = NULL;
aoqi@0 1687 int true_path = is_diamond_phi();
aoqi@0 1688 if( true_path != 0 ) {
aoqi@0 1689 // Check for CMove'ing identity. If it would be unsafe,
aoqi@0 1690 // handle it here. In the safe case, let Identity handle it.
aoqi@0 1691 Node* unsafe_id = is_cmove_id(phase, true_path);
aoqi@0 1692 if( unsafe_id != NULL && is_unsafe_data_reference(unsafe_id) )
aoqi@0 1693 opt = unsafe_id;
aoqi@0 1694
aoqi@0 1695 // Check for simple convert-to-boolean pattern
aoqi@0 1696 if( opt == NULL )
aoqi@0 1697 opt = is_x2logic(phase, this, true_path);
aoqi@0 1698
aoqi@0 1699 // Check for absolute value
aoqi@0 1700 if( opt == NULL )
aoqi@0 1701 opt = is_absolute(phase, this, true_path);
aoqi@0 1702
aoqi@0 1703 // Check for conditional add
aoqi@0 1704 if( opt == NULL && can_reshape )
aoqi@0 1705 opt = is_cond_add(phase, this, true_path);
aoqi@0 1706
aoqi@0 1707 // These 4 optimizations could subsume the phi:
aoqi@0 1708 // have to check for a dead data loop creation.
aoqi@0 1709 if( opt != NULL ) {
aoqi@0 1710 if( opt == unsafe_id || is_unsafe_data_reference(opt) ) {
aoqi@0 1711 // Found dead loop.
aoqi@0 1712 if( can_reshape )
aoqi@0 1713 return top;
aoqi@0 1714 // We can't return top if we are in Parse phase - cut inputs only
aoqi@0 1715 // to stop further optimizations for this phi. Identity will return TOP.
aoqi@0 1716 assert(req() == 3, "only diamond merge phi here");
aoqi@0 1717 set_req(1, top);
aoqi@0 1718 set_req(2, top);
aoqi@0 1719 return NULL;
aoqi@0 1720 } else {
aoqi@0 1721 return opt;
aoqi@0 1722 }
aoqi@0 1723 }
aoqi@0 1724 }
aoqi@0 1725
aoqi@0 1726 // Check for merging identical values and split flow paths
aoqi@0 1727 if (can_reshape) {
aoqi@0 1728 opt = split_flow_path(phase, this);
aoqi@0 1729 // This optimization only modifies phi - don't need to check for dead loop.
aoqi@0 1730 assert(opt == NULL || phase->eqv(opt, this), "do not elide phi");
aoqi@0 1731 if (opt != NULL) return opt;
aoqi@0 1732 }
aoqi@0 1733
aoqi@0 1734 if (in(1) != NULL && in(1)->Opcode() == Op_AddP && can_reshape) {
aoqi@0 1735 // Try to undo Phi of AddP:
aoqi@0 1736 // (Phi (AddP base base y) (AddP base2 base2 y))
aoqi@0 1737 // becomes:
aoqi@0 1738 // newbase := (Phi base base2)
aoqi@0 1739 // (AddP newbase newbase y)
aoqi@0 1740 //
aoqi@0 1741 // This occurs as a result of unsuccessful split_thru_phi and
aoqi@0 1742 // interferes with taking advantage of addressing modes. See the
aoqi@0 1743 // clone_shift_expressions code in matcher.cpp
aoqi@0 1744 Node* addp = in(1);
aoqi@0 1745 const Type* type = addp->in(AddPNode::Base)->bottom_type();
aoqi@0 1746 Node* y = addp->in(AddPNode::Offset);
aoqi@0 1747 if (y != NULL && addp->in(AddPNode::Base) == addp->in(AddPNode::Address)) {
aoqi@0 1748 // make sure that all the inputs are similar to the first one,
aoqi@0 1749 // i.e. AddP with base == address and same offset as first AddP
aoqi@0 1750 bool doit = true;
aoqi@0 1751 for (uint i = 2; i < req(); i++) {
aoqi@0 1752 if (in(i) == NULL ||
aoqi@0 1753 in(i)->Opcode() != Op_AddP ||
aoqi@0 1754 in(i)->in(AddPNode::Base) != in(i)->in(AddPNode::Address) ||
aoqi@0 1755 in(i)->in(AddPNode::Offset) != y) {
aoqi@0 1756 doit = false;
aoqi@0 1757 break;
aoqi@0 1758 }
aoqi@0 1759 // Accumulate type for resulting Phi
aoqi@0 1760 type = type->meet_speculative(in(i)->in(AddPNode::Base)->bottom_type());
aoqi@0 1761 }
aoqi@0 1762 Node* base = NULL;
aoqi@0 1763 if (doit) {
aoqi@0 1764 // Check for neighboring AddP nodes in a tree.
aoqi@0 1765 // If they have a base, use that it.
aoqi@0 1766 for (DUIterator_Fast kmax, k = this->fast_outs(kmax); k < kmax; k++) {
aoqi@0 1767 Node* u = this->fast_out(k);
aoqi@0 1768 if (u->is_AddP()) {
aoqi@0 1769 Node* base2 = u->in(AddPNode::Base);
aoqi@0 1770 if (base2 != NULL && !base2->is_top()) {
aoqi@0 1771 if (base == NULL)
aoqi@0 1772 base = base2;
aoqi@0 1773 else if (base != base2)
aoqi@0 1774 { doit = false; break; }
aoqi@0 1775 }
aoqi@0 1776 }
aoqi@0 1777 }
aoqi@0 1778 }
aoqi@0 1779 if (doit) {
aoqi@0 1780 if (base == NULL) {
aoqi@0 1781 base = new (phase->C) PhiNode(in(0), type, NULL);
aoqi@0 1782 for (uint i = 1; i < req(); i++) {
aoqi@0 1783 base->init_req(i, in(i)->in(AddPNode::Base));
aoqi@0 1784 }
aoqi@0 1785 phase->is_IterGVN()->register_new_node_with_optimizer(base);
aoqi@0 1786 }
aoqi@0 1787 return new (phase->C) AddPNode(base, base, y);
aoqi@0 1788 }
aoqi@0 1789 }
aoqi@0 1790 }
aoqi@0 1791
aoqi@0 1792 // Split phis through memory merges, so that the memory merges will go away.
aoqi@0 1793 // Piggy-back this transformation on the search for a unique input....
aoqi@0 1794 // It will be as if the merged memory is the unique value of the phi.
aoqi@0 1795 // (Do not attempt this optimization unless parsing is complete.
aoqi@0 1796 // It would make the parser's memory-merge logic sick.)
aoqi@0 1797 // (MergeMemNode is not dead_loop_safe - need to check for dead loop.)
aoqi@0 1798 if (progress == NULL && can_reshape && type() == Type::MEMORY) {
aoqi@0 1799 // see if this phi should be sliced
aoqi@0 1800 uint merge_width = 0;
aoqi@0 1801 bool saw_self = false;
aoqi@0 1802 for( uint i=1; i<req(); ++i ) {// For all paths in
aoqi@0 1803 Node *ii = in(i);
aoqi@0 1804 if (ii->is_MergeMem()) {
aoqi@0 1805 MergeMemNode* n = ii->as_MergeMem();
aoqi@0 1806 merge_width = MAX2(merge_width, n->req());
aoqi@0 1807 saw_self = saw_self || phase->eqv(n->base_memory(), this);
aoqi@0 1808 }
aoqi@0 1809 }
aoqi@0 1810
aoqi@0 1811 // This restriction is temporarily necessary to ensure termination:
aoqi@0 1812 if (!saw_self && adr_type() == TypePtr::BOTTOM) merge_width = 0;
aoqi@0 1813
aoqi@0 1814 if (merge_width > Compile::AliasIdxRaw) {
aoqi@0 1815 // found at least one non-empty MergeMem
aoqi@0 1816 const TypePtr* at = adr_type();
aoqi@0 1817 if (at != TypePtr::BOTTOM) {
aoqi@0 1818 // Patch the existing phi to select an input from the merge:
aoqi@0 1819 // Phi:AT1(...MergeMem(m0, m1, m2)...) into
aoqi@0 1820 // Phi:AT1(...m1...)
aoqi@0 1821 int alias_idx = phase->C->get_alias_index(at);
aoqi@0 1822 for (uint i=1; i<req(); ++i) {
aoqi@0 1823 Node *ii = in(i);
aoqi@0 1824 if (ii->is_MergeMem()) {
aoqi@0 1825 MergeMemNode* n = ii->as_MergeMem();
aoqi@0 1826 // compress paths and change unreachable cycles to TOP
aoqi@0 1827 // If not, we can update the input infinitely along a MergeMem cycle
aoqi@0 1828 // Equivalent code is in MemNode::Ideal_common
aoqi@0 1829 Node *m = phase->transform(n);
aoqi@0 1830 if (outcnt() == 0) { // Above transform() may kill us!
aoqi@0 1831 return top;
aoqi@0 1832 }
aoqi@0 1833 // If transformed to a MergeMem, get the desired slice
aoqi@0 1834 // Otherwise the returned node represents memory for every slice
aoqi@0 1835 Node *new_mem = (m->is_MergeMem()) ?
aoqi@0 1836 m->as_MergeMem()->memory_at(alias_idx) : m;
aoqi@0 1837 // Update input if it is progress over what we have now
aoqi@0 1838 if (new_mem != ii) {
aoqi@0 1839 set_req(i, new_mem);
aoqi@0 1840 progress = this;
aoqi@0 1841 }
aoqi@0 1842 }
aoqi@0 1843 }
aoqi@0 1844 } else {
aoqi@0 1845 // We know that at least one MergeMem->base_memory() == this
aoqi@0 1846 // (saw_self == true). If all other inputs also references this phi
aoqi@0 1847 // (directly or through data nodes) - it is dead loop.
aoqi@0 1848 bool saw_safe_input = false;
aoqi@0 1849 for (uint j = 1; j < req(); ++j) {
aoqi@0 1850 Node *n = in(j);
aoqi@0 1851 if (n->is_MergeMem() && n->as_MergeMem()->base_memory() == this)
aoqi@0 1852 continue; // skip known cases
aoqi@0 1853 if (!is_unsafe_data_reference(n)) {
aoqi@0 1854 saw_safe_input = true; // found safe input
aoqi@0 1855 break;
aoqi@0 1856 }
aoqi@0 1857 }
aoqi@0 1858 if (!saw_safe_input)
aoqi@0 1859 return top; // all inputs reference back to this phi - dead loop
aoqi@0 1860
aoqi@0 1861 // Phi(...MergeMem(m0, m1:AT1, m2:AT2)...) into
aoqi@0 1862 // MergeMem(Phi(...m0...), Phi:AT1(...m1...), Phi:AT2(...m2...))
aoqi@0 1863 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 1864 Node* hook = new (phase->C) Node(1);
aoqi@0 1865 PhiNode* new_base = (PhiNode*) clone();
aoqi@0 1866 // Must eagerly register phis, since they participate in loops.
aoqi@0 1867 if (igvn) {
aoqi@0 1868 igvn->register_new_node_with_optimizer(new_base);
aoqi@0 1869 hook->add_req(new_base);
aoqi@0 1870 }
aoqi@0 1871 MergeMemNode* result = MergeMemNode::make(phase->C, new_base);
aoqi@0 1872 for (uint i = 1; i < req(); ++i) {
aoqi@0 1873 Node *ii = in(i);
aoqi@0 1874 if (ii->is_MergeMem()) {
aoqi@0 1875 MergeMemNode* n = ii->as_MergeMem();
aoqi@0 1876 for (MergeMemStream mms(result, n); mms.next_non_empty2(); ) {
aoqi@0 1877 // If we have not seen this slice yet, make a phi for it.
aoqi@0 1878 bool made_new_phi = false;
aoqi@0 1879 if (mms.is_empty()) {
aoqi@0 1880 Node* new_phi = new_base->slice_memory(mms.adr_type(phase->C));
aoqi@0 1881 made_new_phi = true;
aoqi@0 1882 if (igvn) {
aoqi@0 1883 igvn->register_new_node_with_optimizer(new_phi);
aoqi@0 1884 hook->add_req(new_phi);
aoqi@0 1885 }
aoqi@0 1886 mms.set_memory(new_phi);
aoqi@0 1887 }
aoqi@0 1888 Node* phi = mms.memory();
aoqi@0 1889 assert(made_new_phi || phi->in(i) == n, "replace the i-th merge by a slice");
aoqi@0 1890 phi->set_req(i, mms.memory2());
aoqi@0 1891 }
aoqi@0 1892 }
aoqi@0 1893 }
aoqi@0 1894 // Distribute all self-loops.
aoqi@0 1895 { // (Extra braces to hide mms.)
aoqi@0 1896 for (MergeMemStream mms(result); mms.next_non_empty(); ) {
aoqi@0 1897 Node* phi = mms.memory();
aoqi@0 1898 for (uint i = 1; i < req(); ++i) {
aoqi@0 1899 if (phi->in(i) == this) phi->set_req(i, phi);
aoqi@0 1900 }
aoqi@0 1901 }
aoqi@0 1902 }
aoqi@0 1903 // now transform the new nodes, and return the mergemem
aoqi@0 1904 for (MergeMemStream mms(result); mms.next_non_empty(); ) {
aoqi@0 1905 Node* phi = mms.memory();
aoqi@0 1906 mms.set_memory(phase->transform(phi));
aoqi@0 1907 }
aoqi@0 1908 if (igvn) { // Unhook.
aoqi@0 1909 igvn->hash_delete(hook);
aoqi@0 1910 for (uint i = 1; i < hook->req(); i++) {
aoqi@0 1911 hook->set_req(i, NULL);
aoqi@0 1912 }
aoqi@0 1913 }
aoqi@0 1914 // Replace self with the result.
aoqi@0 1915 return result;
aoqi@0 1916 }
aoqi@0 1917 }
aoqi@0 1918 //
aoqi@0 1919 // Other optimizations on the memory chain
aoqi@0 1920 //
aoqi@0 1921 const TypePtr* at = adr_type();
aoqi@0 1922 for( uint i=1; i<req(); ++i ) {// For all paths in
aoqi@0 1923 Node *ii = in(i);
aoqi@0 1924 Node *new_in = MemNode::optimize_memory_chain(ii, at, NULL, phase);
aoqi@0 1925 if (ii != new_in ) {
aoqi@0 1926 set_req(i, new_in);
aoqi@0 1927 progress = this;
aoqi@0 1928 }
aoqi@0 1929 }
aoqi@0 1930 }
aoqi@0 1931
aoqi@0 1932 #ifdef _LP64
aoqi@0 1933 // Push DecodeN/DecodeNKlass down through phi.
aoqi@0 1934 // The rest of phi graph will transform by split EncodeP node though phis up.
aoqi@0 1935 if ((UseCompressedOops || UseCompressedClassPointers) && can_reshape && progress == NULL) {
aoqi@0 1936 bool may_push = true;
aoqi@0 1937 bool has_decodeN = false;
aoqi@0 1938 bool is_decodeN = false;
aoqi@0 1939 for (uint i=1; i<req(); ++i) {// For all paths in
aoqi@0 1940 Node *ii = in(i);
aoqi@0 1941 if (ii->is_DecodeNarrowPtr() && ii->bottom_type() == bottom_type()) {
aoqi@0 1942 // Do optimization if a non dead path exist.
aoqi@0 1943 if (ii->in(1)->bottom_type() != Type::TOP) {
aoqi@0 1944 has_decodeN = true;
aoqi@0 1945 is_decodeN = ii->is_DecodeN();
aoqi@0 1946 }
aoqi@0 1947 } else if (!ii->is_Phi()) {
aoqi@0 1948 may_push = false;
aoqi@0 1949 }
aoqi@0 1950 }
aoqi@0 1951
aoqi@0 1952 if (has_decodeN && may_push) {
aoqi@0 1953 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 1954 // Make narrow type for new phi.
aoqi@0 1955 const Type* narrow_t;
aoqi@0 1956 if (is_decodeN) {
aoqi@0 1957 narrow_t = TypeNarrowOop::make(this->bottom_type()->is_ptr());
aoqi@0 1958 } else {
aoqi@0 1959 narrow_t = TypeNarrowKlass::make(this->bottom_type()->is_ptr());
aoqi@0 1960 }
aoqi@0 1961 PhiNode* new_phi = new (phase->C) PhiNode(r, narrow_t);
aoqi@0 1962 uint orig_cnt = req();
aoqi@0 1963 for (uint i=1; i<req(); ++i) {// For all paths in
aoqi@0 1964 Node *ii = in(i);
aoqi@0 1965 Node* new_ii = NULL;
aoqi@0 1966 if (ii->is_DecodeNarrowPtr()) {
aoqi@0 1967 assert(ii->bottom_type() == bottom_type(), "sanity");
aoqi@0 1968 new_ii = ii->in(1);
aoqi@0 1969 } else {
aoqi@0 1970 assert(ii->is_Phi(), "sanity");
aoqi@0 1971 if (ii->as_Phi() == this) {
aoqi@0 1972 new_ii = new_phi;
aoqi@0 1973 } else {
aoqi@0 1974 if (is_decodeN) {
aoqi@0 1975 new_ii = new (phase->C) EncodePNode(ii, narrow_t);
aoqi@0 1976 } else {
aoqi@0 1977 new_ii = new (phase->C) EncodePKlassNode(ii, narrow_t);
aoqi@0 1978 }
aoqi@0 1979 igvn->register_new_node_with_optimizer(new_ii);
aoqi@0 1980 }
aoqi@0 1981 }
aoqi@0 1982 new_phi->set_req(i, new_ii);
aoqi@0 1983 }
aoqi@0 1984 igvn->register_new_node_with_optimizer(new_phi, this);
aoqi@0 1985 if (is_decodeN) {
aoqi@0 1986 progress = new (phase->C) DecodeNNode(new_phi, bottom_type());
aoqi@0 1987 } else {
aoqi@0 1988 progress = new (phase->C) DecodeNKlassNode(new_phi, bottom_type());
aoqi@0 1989 }
aoqi@0 1990 }
aoqi@0 1991 }
aoqi@0 1992 #endif
aoqi@0 1993
aoqi@0 1994 return progress; // Return any progress
aoqi@0 1995 }
aoqi@0 1996
aoqi@0 1997 //------------------------------is_tripcount-----------------------------------
aoqi@0 1998 bool PhiNode::is_tripcount() const {
aoqi@0 1999 return (in(0) != NULL && in(0)->is_CountedLoop() &&
aoqi@0 2000 in(0)->as_CountedLoop()->phi() == this);
aoqi@0 2001 }
aoqi@0 2002
aoqi@0 2003 //------------------------------out_RegMask------------------------------------
aoqi@0 2004 const RegMask &PhiNode::in_RegMask(uint i) const {
aoqi@0 2005 return i ? out_RegMask() : RegMask::Empty;
aoqi@0 2006 }
aoqi@0 2007
aoqi@0 2008 const RegMask &PhiNode::out_RegMask() const {
aoqi@0 2009 uint ideal_reg = _type->ideal_reg();
aoqi@0 2010 assert( ideal_reg != Node::NotAMachineReg, "invalid type at Phi" );
aoqi@0 2011 if( ideal_reg == 0 ) return RegMask::Empty;
aoqi@0 2012 return *(Compile::current()->matcher()->idealreg2spillmask[ideal_reg]);
aoqi@0 2013 }
aoqi@0 2014
aoqi@0 2015 #ifndef PRODUCT
aoqi@0 2016 void PhiNode::dump_spec(outputStream *st) const {
aoqi@0 2017 TypeNode::dump_spec(st);
aoqi@0 2018 if (is_tripcount()) {
aoqi@0 2019 st->print(" #tripcount");
aoqi@0 2020 }
aoqi@0 2021 }
aoqi@0 2022 #endif
aoqi@0 2023
aoqi@0 2024
aoqi@0 2025 //=============================================================================
aoqi@0 2026 const Type *GotoNode::Value( PhaseTransform *phase ) const {
aoqi@0 2027 // If the input is reachable, then we are executed.
aoqi@0 2028 // If the input is not reachable, then we are not executed.
aoqi@0 2029 return phase->type(in(0));
aoqi@0 2030 }
aoqi@0 2031
aoqi@0 2032 Node *GotoNode::Identity( PhaseTransform *phase ) {
aoqi@0 2033 return in(0); // Simple copy of incoming control
aoqi@0 2034 }
aoqi@0 2035
aoqi@0 2036 const RegMask &GotoNode::out_RegMask() const {
aoqi@0 2037 return RegMask::Empty;
aoqi@0 2038 }
aoqi@0 2039
aoqi@0 2040 //=============================================================================
aoqi@0 2041 const RegMask &JumpNode::out_RegMask() const {
aoqi@0 2042 return RegMask::Empty;
aoqi@0 2043 }
aoqi@0 2044
aoqi@0 2045 //=============================================================================
aoqi@0 2046 const RegMask &JProjNode::out_RegMask() const {
aoqi@0 2047 return RegMask::Empty;
aoqi@0 2048 }
aoqi@0 2049
aoqi@0 2050 //=============================================================================
aoqi@0 2051 const RegMask &CProjNode::out_RegMask() const {
aoqi@0 2052 return RegMask::Empty;
aoqi@0 2053 }
aoqi@0 2054
aoqi@0 2055
aoqi@0 2056
aoqi@0 2057 //=============================================================================
aoqi@0 2058
aoqi@0 2059 uint PCTableNode::hash() const { return Node::hash() + _size; }
aoqi@0 2060 uint PCTableNode::cmp( const Node &n ) const
aoqi@0 2061 { return _size == ((PCTableNode&)n)._size; }
aoqi@0 2062
aoqi@0 2063 const Type *PCTableNode::bottom_type() const {
aoqi@0 2064 const Type** f = TypeTuple::fields(_size);
aoqi@0 2065 for( uint i = 0; i < _size; i++ ) f[i] = Type::CONTROL;
aoqi@0 2066 return TypeTuple::make(_size, f);
aoqi@0 2067 }
aoqi@0 2068
aoqi@0 2069 //------------------------------Value------------------------------------------
aoqi@0 2070 // Compute the type of the PCTableNode. If reachable it is a tuple of
aoqi@0 2071 // Control, otherwise the table targets are not reachable
aoqi@0 2072 const Type *PCTableNode::Value( PhaseTransform *phase ) const {
aoqi@0 2073 if( phase->type(in(0)) == Type::CONTROL )
aoqi@0 2074 return bottom_type();
aoqi@0 2075 return Type::TOP; // All paths dead? Then so are we
aoqi@0 2076 }
aoqi@0 2077
aoqi@0 2078 //------------------------------Ideal------------------------------------------
aoqi@0 2079 // Return a node which is more "ideal" than the current node. Strip out
aoqi@0 2080 // control copies
aoqi@0 2081 Node *PCTableNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 2082 return remove_dead_region(phase, can_reshape) ? this : NULL;
aoqi@0 2083 }
aoqi@0 2084
aoqi@0 2085 //=============================================================================
aoqi@0 2086 uint JumpProjNode::hash() const {
aoqi@0 2087 return Node::hash() + _dest_bci;
aoqi@0 2088 }
aoqi@0 2089
aoqi@0 2090 uint JumpProjNode::cmp( const Node &n ) const {
aoqi@0 2091 return ProjNode::cmp(n) &&
aoqi@0 2092 _dest_bci == ((JumpProjNode&)n)._dest_bci;
aoqi@0 2093 }
aoqi@0 2094
aoqi@0 2095 #ifndef PRODUCT
aoqi@0 2096 void JumpProjNode::dump_spec(outputStream *st) const {
aoqi@0 2097 ProjNode::dump_spec(st);
aoqi@0 2098 st->print("@bci %d ",_dest_bci);
aoqi@0 2099 }
aoqi@0 2100 #endif
aoqi@0 2101
aoqi@0 2102 //=============================================================================
aoqi@0 2103 //------------------------------Value------------------------------------------
aoqi@0 2104 // Check for being unreachable, or for coming from a Rethrow. Rethrow's cannot
aoqi@0 2105 // have the default "fall_through_index" path.
aoqi@0 2106 const Type *CatchNode::Value( PhaseTransform *phase ) const {
aoqi@0 2107 // Unreachable? Then so are all paths from here.
aoqi@0 2108 if( phase->type(in(0)) == Type::TOP ) return Type::TOP;
aoqi@0 2109 // First assume all paths are reachable
aoqi@0 2110 const Type** f = TypeTuple::fields(_size);
aoqi@0 2111 for( uint i = 0; i < _size; i++ ) f[i] = Type::CONTROL;
aoqi@0 2112 // Identify cases that will always throw an exception
aoqi@0 2113 // () rethrow call
aoqi@0 2114 // () virtual or interface call with NULL receiver
aoqi@0 2115 // () call is a check cast with incompatible arguments
aoqi@0 2116 if( in(1)->is_Proj() ) {
aoqi@0 2117 Node *i10 = in(1)->in(0);
aoqi@0 2118 if( i10->is_Call() ) {
aoqi@0 2119 CallNode *call = i10->as_Call();
aoqi@0 2120 // Rethrows always throw exceptions, never return
aoqi@0 2121 if (call->entry_point() == OptoRuntime::rethrow_stub()) {
aoqi@0 2122 f[CatchProjNode::fall_through_index] = Type::TOP;
aoqi@0 2123 } else if( call->req() > TypeFunc::Parms ) {
aoqi@0 2124 const Type *arg0 = phase->type( call->in(TypeFunc::Parms) );
aoqi@0 2125 // Check for null receiver to virtual or interface calls
aoqi@0 2126 if( call->is_CallDynamicJava() &&
aoqi@0 2127 arg0->higher_equal(TypePtr::NULL_PTR) ) {
aoqi@0 2128 f[CatchProjNode::fall_through_index] = Type::TOP;
aoqi@0 2129 }
aoqi@0 2130 } // End of if not a runtime stub
aoqi@0 2131 } // End of if have call above me
aoqi@0 2132 } // End of slot 1 is not a projection
aoqi@0 2133 return TypeTuple::make(_size, f);
aoqi@0 2134 }
aoqi@0 2135
aoqi@0 2136 //=============================================================================
aoqi@0 2137 uint CatchProjNode::hash() const {
aoqi@0 2138 return Node::hash() + _handler_bci;
aoqi@0 2139 }
aoqi@0 2140
aoqi@0 2141
aoqi@0 2142 uint CatchProjNode::cmp( const Node &n ) const {
aoqi@0 2143 return ProjNode::cmp(n) &&
aoqi@0 2144 _handler_bci == ((CatchProjNode&)n)._handler_bci;
aoqi@0 2145 }
aoqi@0 2146
aoqi@0 2147
aoqi@0 2148 //------------------------------Identity---------------------------------------
aoqi@0 2149 // If only 1 target is possible, choose it if it is the main control
aoqi@0 2150 Node *CatchProjNode::Identity( PhaseTransform *phase ) {
aoqi@0 2151 // If my value is control and no other value is, then treat as ID
aoqi@0 2152 const TypeTuple *t = phase->type(in(0))->is_tuple();
aoqi@0 2153 if (t->field_at(_con) != Type::CONTROL) return this;
aoqi@0 2154 // If we remove the last CatchProj and elide the Catch/CatchProj, then we
aoqi@0 2155 // also remove any exception table entry. Thus we must know the call
aoqi@0 2156 // feeding the Catch will not really throw an exception. This is ok for
aoqi@0 2157 // the main fall-thru control (happens when we know a call can never throw
aoqi@0 2158 // an exception) or for "rethrow", because a further optimization will
aoqi@0 2159 // yank the rethrow (happens when we inline a function that can throw an
aoqi@0 2160 // exception and the caller has no handler). Not legal, e.g., for passing
aoqi@0 2161 // a NULL receiver to a v-call, or passing bad types to a slow-check-cast.
aoqi@0 2162 // These cases MUST throw an exception via the runtime system, so the VM
aoqi@0 2163 // will be looking for a table entry.
aoqi@0 2164 Node *proj = in(0)->in(1); // Expect a proj feeding CatchNode
aoqi@0 2165 CallNode *call;
aoqi@0 2166 if (_con != TypeFunc::Control && // Bail out if not the main control.
aoqi@0 2167 !(proj->is_Proj() && // AND NOT a rethrow
aoqi@0 2168 proj->in(0)->is_Call() &&
aoqi@0 2169 (call = proj->in(0)->as_Call()) &&
aoqi@0 2170 call->entry_point() == OptoRuntime::rethrow_stub()))
aoqi@0 2171 return this;
aoqi@0 2172
aoqi@0 2173 // Search for any other path being control
aoqi@0 2174 for (uint i = 0; i < t->cnt(); i++) {
aoqi@0 2175 if (i != _con && t->field_at(i) == Type::CONTROL)
aoqi@0 2176 return this;
aoqi@0 2177 }
aoqi@0 2178 // Only my path is possible; I am identity on control to the jump
aoqi@0 2179 return in(0)->in(0);
aoqi@0 2180 }
aoqi@0 2181
aoqi@0 2182
aoqi@0 2183 #ifndef PRODUCT
aoqi@0 2184 void CatchProjNode::dump_spec(outputStream *st) const {
aoqi@0 2185 ProjNode::dump_spec(st);
aoqi@0 2186 st->print("@bci %d ",_handler_bci);
aoqi@0 2187 }
aoqi@0 2188 #endif
aoqi@0 2189
aoqi@0 2190 //=============================================================================
aoqi@0 2191 //------------------------------Identity---------------------------------------
aoqi@0 2192 // Check for CreateEx being Identity.
aoqi@0 2193 Node *CreateExNode::Identity( PhaseTransform *phase ) {
aoqi@0 2194 if( phase->type(in(1)) == Type::TOP ) return in(1);
aoqi@0 2195 if( phase->type(in(0)) == Type::TOP ) return in(0);
aoqi@0 2196 // We only come from CatchProj, unless the CatchProj goes away.
aoqi@0 2197 // If the CatchProj is optimized away, then we just carry the
aoqi@0 2198 // exception oop through.
aoqi@0 2199 CallNode *call = in(1)->in(0)->as_Call();
aoqi@0 2200
aoqi@0 2201 return ( in(0)->is_CatchProj() && in(0)->in(0)->in(1) == in(1) )
aoqi@0 2202 ? this
aoqi@0 2203 : call->in(TypeFunc::Parms);
aoqi@0 2204 }
aoqi@0 2205
aoqi@0 2206 //=============================================================================
aoqi@0 2207 //------------------------------Value------------------------------------------
aoqi@0 2208 // Check for being unreachable.
aoqi@0 2209 const Type *NeverBranchNode::Value( PhaseTransform *phase ) const {
aoqi@0 2210 if (!in(0) || in(0)->is_top()) return Type::TOP;
aoqi@0 2211 return bottom_type();
aoqi@0 2212 }
aoqi@0 2213
aoqi@0 2214 //------------------------------Ideal------------------------------------------
aoqi@0 2215 // Check for no longer being part of a loop
aoqi@0 2216 Node *NeverBranchNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 2217 if (can_reshape && !in(0)->is_Loop()) {
aoqi@0 2218 // Dead code elimination can sometimes delete this projection so
aoqi@0 2219 // if it's not there, there's nothing to do.
aoqi@0 2220 Node* fallthru = proj_out(0);
aoqi@0 2221 if (fallthru != NULL) {
aoqi@0 2222 phase->is_IterGVN()->replace_node(fallthru, in(0));
aoqi@0 2223 }
aoqi@0 2224 return phase->C->top();
aoqi@0 2225 }
aoqi@0 2226 return NULL;
aoqi@0 2227 }
aoqi@0 2228
aoqi@0 2229 #ifndef PRODUCT
aoqi@0 2230 void NeverBranchNode::format( PhaseRegAlloc *ra_, outputStream *st) const {
aoqi@0 2231 st->print("%s", Name());
aoqi@0 2232 }
aoqi@0 2233 #endif

mercurial