1 /* 2 * Copyright (c) 2015, 2021, Red Hat, Inc. All rights reserved. 3 * Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 28 #include "classfile/javaClasses.hpp" 29 #include "gc/shenandoah/c2/shenandoahSupport.hpp" 30 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp" 31 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp" 32 #include "gc/shenandoah/shenandoahForwarding.hpp" 33 #include "gc/shenandoah/shenandoahHeap.hpp" 34 #include "gc/shenandoah/shenandoahHeapRegion.hpp" 35 #include "gc/shenandoah/shenandoahRuntime.hpp" 36 #include "gc/shenandoah/shenandoahThreadLocalData.hpp" 37 #include "opto/arraycopynode.hpp" 38 #include "opto/block.hpp" 39 #include "opto/callnode.hpp" 40 #include "opto/castnode.hpp" 41 #include "opto/movenode.hpp" 42 #include "opto/phaseX.hpp" 43 #include "opto/rootnode.hpp" 44 #include "opto/runtime.hpp" 45 #include "opto/subnode.hpp" 46 47 bool ShenandoahBarrierC2Support::expand(Compile* C, PhaseIterGVN& igvn) { 48 ShenandoahBarrierSetC2State* state = ShenandoahBarrierSetC2::bsc2()->state(); 49 if ((state->iu_barriers_count() + 50 state->load_reference_barriers_count()) > 0) { 51 assert(C->post_loop_opts_phase(), "no loop opts allowed"); 52 C->reset_post_loop_opts_phase(); // ... but we know what we are doing 53 C->clear_major_progress(); 54 PhaseIdealLoop::optimize(igvn, LoopOptsShenandoahExpand); 55 if (C->failing()) return false; 56 57 C->set_major_progress(); 58 if (!C->optimize_loops(igvn, LoopOptsShenandoahPostExpand)) { 59 return false; 60 } 61 C->clear_major_progress(); 62 C->process_for_post_loop_opts_igvn(igvn); 63 if (C->failing()) return false; 64 65 C->set_post_loop_opts_phase(); // now for real! 66 } 67 return true; 68 } 69 70 bool ShenandoahBarrierC2Support::is_gc_state_test(Node* iff, int mask) { 71 if (!UseShenandoahGC) { 72 return false; 73 } 74 assert(iff->is_If(), "bad input"); 75 if (iff->Opcode() != Op_If) { 76 return false; 77 } 78 Node* bol = iff->in(1); 79 if (!bol->is_Bool() || bol->as_Bool()->_test._test != BoolTest::ne) { 80 return false; 81 } 82 Node* cmp = bol->in(1); 83 if (cmp->Opcode() != Op_CmpI) { 84 return false; 85 } 86 Node* in1 = cmp->in(1); 87 Node* in2 = cmp->in(2); 88 if (in2->find_int_con(-1) != 0) { 89 return false; 90 } 91 if (in1->Opcode() != Op_AndI) { 92 return false; 93 } 94 in2 = in1->in(2); 95 if (in2->find_int_con(-1) != mask) { 96 return false; 97 } 98 in1 = in1->in(1); 99 100 return is_gc_state_load(in1); 101 } 102 103 bool ShenandoahBarrierC2Support::is_heap_stable_test(Node* iff) { 104 return is_gc_state_test(iff, ShenandoahHeap::HAS_FORWARDED); 105 } 106 107 bool ShenandoahBarrierC2Support::is_gc_state_load(Node *n) { 108 if (!UseShenandoahGC) { 109 return false; 110 } 111 if (n->Opcode() != Op_LoadB && n->Opcode() != Op_LoadUB) { 112 return false; 113 } 114 Node* addp = n->in(MemNode::Address); 115 if (!addp->is_AddP()) { 116 return false; 117 } 118 Node* base = addp->in(AddPNode::Address); 119 Node* off = addp->in(AddPNode::Offset); 120 if (base->Opcode() != Op_ThreadLocal) { 121 return false; 122 } 123 if (off->find_intptr_t_con(-1) != in_bytes(ShenandoahThreadLocalData::gc_state_offset())) { 124 return false; 125 } 126 return true; 127 } 128 129 bool ShenandoahBarrierC2Support::has_safepoint_between(Node* start, Node* stop, PhaseIdealLoop *phase) { 130 assert(phase->is_dominator(stop, start), "bad inputs"); 131 ResourceMark rm; 132 Unique_Node_List wq; 133 wq.push(start); 134 for (uint next = 0; next < wq.size(); next++) { 135 Node *m = wq.at(next); 136 if (m == stop) { 137 continue; 138 } 139 if (m->is_SafePoint() && !m->is_CallLeaf()) { 140 return true; 141 } 142 if (m->is_Region()) { 143 for (uint i = 1; i < m->req(); i++) { 144 wq.push(m->in(i)); 145 } 146 } else { 147 wq.push(m->in(0)); 148 } 149 } 150 return false; 151 } 152 153 #ifdef ASSERT 154 bool ShenandoahBarrierC2Support::verify_helper(Node* in, Node_Stack& phis, VectorSet& visited, verify_type t, bool trace, Unique_Node_List& barriers_used) { 155 assert(phis.size() == 0, ""); 156 157 while (true) { 158 if (in->bottom_type() == TypePtr::NULL_PTR) { 159 if (trace) {tty->print_cr("null");} 160 } else if (!in->bottom_type()->make_ptr()->make_oopptr()) { 161 if (trace) {tty->print_cr("Non oop");} 162 } else { 163 if (in->is_ConstraintCast()) { 164 in = in->in(1); 165 continue; 166 } else if (in->is_AddP()) { 167 assert(!in->in(AddPNode::Address)->is_top(), "no raw memory access"); 168 in = in->in(AddPNode::Address); 169 continue; 170 } else if (in->is_Con()) { 171 if (trace) { 172 tty->print("Found constant"); 173 in->dump(); 174 } 175 } else if (in->Opcode() == Op_Parm) { 176 if (trace) { 177 tty->print("Found argument"); 178 } 179 } else if (in->Opcode() == Op_CreateEx) { 180 if (trace) { 181 tty->print("Found create-exception"); 182 } 183 } else if (in->Opcode() == Op_LoadP && in->adr_type() == TypeRawPtr::BOTTOM) { 184 if (trace) { 185 tty->print("Found raw LoadP (OSR argument?)"); 186 } 187 } else if (in->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 188 if (t == ShenandoahOopStore) { 189 uint i = 0; 190 for (; i < phis.size(); i++) { 191 Node* n = phis.node_at(i); 192 if (n->Opcode() == Op_ShenandoahIUBarrier) { 193 break; 194 } 195 } 196 if (i == phis.size()) { 197 return false; 198 } 199 } 200 barriers_used.push(in); 201 if (trace) {tty->print("Found barrier"); in->dump();} 202 } else if (in->Opcode() == Op_ShenandoahIUBarrier) { 203 if (t != ShenandoahOopStore) { 204 in = in->in(1); 205 continue; 206 } 207 if (trace) {tty->print("Found enqueue barrier"); in->dump();} 208 phis.push(in, in->req()); 209 in = in->in(1); 210 continue; 211 } else if (in->is_Proj() && in->in(0)->is_Allocate()) { 212 if (trace) { 213 tty->print("Found alloc"); 214 in->in(0)->dump(); 215 } 216 } else if (in->is_Proj() && (in->in(0)->Opcode() == Op_CallStaticJava || in->in(0)->Opcode() == Op_CallDynamicJava)) { 217 if (trace) { 218 tty->print("Found Java call"); 219 } 220 } else if (in->is_Phi()) { 221 if (!visited.test_set(in->_idx)) { 222 if (trace) {tty->print("Pushed phi:"); in->dump();} 223 phis.push(in, 2); 224 in = in->in(1); 225 continue; 226 } 227 if (trace) {tty->print("Already seen phi:"); in->dump();} 228 } else if (in->Opcode() == Op_CMoveP || in->Opcode() == Op_CMoveN) { 229 if (!visited.test_set(in->_idx)) { 230 if (trace) {tty->print("Pushed cmovep:"); in->dump();} 231 phis.push(in, CMoveNode::IfTrue); 232 in = in->in(CMoveNode::IfFalse); 233 continue; 234 } 235 if (trace) {tty->print("Already seen cmovep:"); in->dump();} 236 } else if (in->Opcode() == Op_EncodeP || in->Opcode() == Op_DecodeN) { 237 in = in->in(1); 238 continue; 239 } else { 240 return false; 241 } 242 } 243 bool cont = false; 244 while (phis.is_nonempty()) { 245 uint idx = phis.index(); 246 Node* phi = phis.node(); 247 if (idx >= phi->req()) { 248 if (trace) {tty->print("Popped phi:"); phi->dump();} 249 phis.pop(); 250 continue; 251 } 252 if (trace) {tty->print("Next entry(%d) for phi:", idx); phi->dump();} 253 in = phi->in(idx); 254 phis.set_index(idx+1); 255 cont = true; 256 break; 257 } 258 if (!cont) { 259 break; 260 } 261 } 262 return true; 263 } 264 265 void ShenandoahBarrierC2Support::report_verify_failure(const char* msg, Node* n1, Node* n2) { 266 if (n1 != nullptr) { 267 n1->dump(+10); 268 } 269 if (n2 != nullptr) { 270 n2->dump(+10); 271 } 272 fatal("%s", msg); 273 } 274 275 void ShenandoahBarrierC2Support::verify(RootNode* root) { 276 ResourceMark rm; 277 Unique_Node_List wq; 278 GrowableArray<Node*> barriers; 279 Unique_Node_List barriers_used; 280 Node_Stack phis(0); 281 VectorSet visited; 282 const bool trace = false; 283 const bool verify_no_useless_barrier = false; 284 285 wq.push(root); 286 for (uint next = 0; next < wq.size(); next++) { 287 Node *n = wq.at(next); 288 if (n->is_Load()) { 289 const bool trace = false; 290 if (trace) {tty->print("Verifying"); n->dump();} 291 if (n->Opcode() == Op_LoadRange || n->Opcode() == Op_LoadKlass || n->Opcode() == Op_LoadNKlass) { 292 if (trace) {tty->print_cr("Load range/klass");} 293 } else { 294 const TypePtr* adr_type = n->as_Load()->adr_type(); 295 296 if (adr_type->isa_oopptr() && adr_type->is_oopptr()->offset() == oopDesc::mark_offset_in_bytes()) { 297 if (trace) {tty->print_cr("Mark load");} 298 } else if (adr_type->isa_instptr() && 299 adr_type->is_instptr()->instance_klass()->is_subtype_of(Compile::current()->env()->Reference_klass()) && 300 adr_type->is_instptr()->offset() == java_lang_ref_Reference::referent_offset()) { 301 if (trace) {tty->print_cr("Reference.get()");} 302 } else if (!verify_helper(n->in(MemNode::Address), phis, visited, ShenandoahLoad, trace, barriers_used)) { 303 report_verify_failure("Shenandoah verification: Load should have barriers", n); 304 } 305 } 306 } else if (n->is_Store()) { 307 const bool trace = false; 308 309 if (trace) {tty->print("Verifying"); n->dump();} 310 if (n->in(MemNode::ValueIn)->bottom_type()->make_oopptr()) { 311 Node* adr = n->in(MemNode::Address); 312 bool verify = true; 313 314 if (adr->is_AddP() && adr->in(AddPNode::Base)->is_top()) { 315 adr = adr->in(AddPNode::Address); 316 if (adr->is_AddP()) { 317 assert(adr->in(AddPNode::Base)->is_top(), ""); 318 adr = adr->in(AddPNode::Address); 319 if (adr->Opcode() == Op_LoadP && 320 adr->in(MemNode::Address)->in(AddPNode::Base)->is_top() && 321 adr->in(MemNode::Address)->in(AddPNode::Address)->Opcode() == Op_ThreadLocal && 322 adr->in(MemNode::Address)->in(AddPNode::Offset)->find_intptr_t_con(-1) == in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset())) { 323 if (trace) {tty->print_cr("SATB prebarrier");} 324 verify = false; 325 } 326 } 327 } 328 329 if (verify && !verify_helper(n->in(MemNode::ValueIn), phis, visited, ShenandoahIUBarrier ? ShenandoahOopStore : ShenandoahValue, trace, barriers_used)) { 330 report_verify_failure("Shenandoah verification: Store should have barriers", n); 331 } 332 } 333 if (!verify_helper(n->in(MemNode::Address), phis, visited, ShenandoahStore, trace, barriers_used)) { 334 report_verify_failure("Shenandoah verification: Store (address) should have barriers", n); 335 } 336 } else if (n->Opcode() == Op_CmpP) { 337 const bool trace = false; 338 339 Node* in1 = n->in(1); 340 Node* in2 = n->in(2); 341 if (in1->bottom_type()->isa_oopptr()) { 342 if (trace) {tty->print("Verifying"); n->dump();} 343 344 bool mark_inputs = false; 345 if (in1->bottom_type() == TypePtr::NULL_PTR || in2->bottom_type() == TypePtr::NULL_PTR || 346 (in1->is_Con() || in2->is_Con())) { 347 if (trace) {tty->print_cr("Comparison against a constant");} 348 mark_inputs = true; 349 } else if ((in1->is_CheckCastPP() && in1->in(1)->is_Proj() && in1->in(1)->in(0)->is_Allocate()) || 350 (in2->is_CheckCastPP() && in2->in(1)->is_Proj() && in2->in(1)->in(0)->is_Allocate())) { 351 if (trace) {tty->print_cr("Comparison with newly alloc'ed object");} 352 mark_inputs = true; 353 } else { 354 assert(in2->bottom_type()->isa_oopptr(), ""); 355 356 if (!verify_helper(in1, phis, visited, ShenandoahStore, trace, barriers_used) || 357 !verify_helper(in2, phis, visited, ShenandoahStore, trace, barriers_used)) { 358 report_verify_failure("Shenandoah verification: Cmp should have barriers", n); 359 } 360 } 361 if (verify_no_useless_barrier && 362 mark_inputs && 363 (!verify_helper(in1, phis, visited, ShenandoahValue, trace, barriers_used) || 364 !verify_helper(in2, phis, visited, ShenandoahValue, trace, barriers_used))) { 365 phis.clear(); 366 visited.reset(); 367 } 368 } 369 } else if (n->is_LoadStore()) { 370 if (n->in(MemNode::ValueIn)->bottom_type()->make_ptr() && 371 !verify_helper(n->in(MemNode::ValueIn), phis, visited, ShenandoahIUBarrier ? ShenandoahOopStore : ShenandoahValue, trace, barriers_used)) { 372 report_verify_failure("Shenandoah verification: LoadStore (value) should have barriers", n); 373 } 374 375 if (n->in(MemNode::Address)->bottom_type()->make_oopptr() && !verify_helper(n->in(MemNode::Address), phis, visited, ShenandoahStore, trace, barriers_used)) { 376 report_verify_failure("Shenandoah verification: LoadStore (address) should have barriers", n); 377 } 378 } else if (n->Opcode() == Op_CallLeafNoFP || n->Opcode() == Op_CallLeaf) { 379 CallNode* call = n->as_Call(); 380 381 static struct { 382 const char* name; 383 struct { 384 int pos; 385 verify_type t; 386 } args[6]; 387 } calls[] = { 388 "aescrypt_encryptBlock", 389 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { TypeFunc::Parms+2, ShenandoahLoad }, 390 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 391 "aescrypt_decryptBlock", 392 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { TypeFunc::Parms+2, ShenandoahLoad }, 393 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 394 "multiplyToLen", 395 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+2, ShenandoahLoad }, { TypeFunc::Parms+4, ShenandoahStore }, 396 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 397 "squareToLen", 398 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+2, ShenandoahLoad }, { -1, ShenandoahNone}, 399 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 400 "montgomery_multiply", 401 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahLoad }, { TypeFunc::Parms+2, ShenandoahLoad }, 402 { TypeFunc::Parms+6, ShenandoahStore }, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 403 "montgomery_square", 404 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahLoad }, { TypeFunc::Parms+5, ShenandoahStore }, 405 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 406 "mulAdd", 407 { { TypeFunc::Parms, ShenandoahStore }, { TypeFunc::Parms+1, ShenandoahLoad }, { -1, ShenandoahNone}, 408 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 409 "vectorizedMismatch", 410 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahLoad }, { -1, ShenandoahNone}, 411 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 412 "updateBytesCRC32", 413 { { TypeFunc::Parms+1, ShenandoahLoad }, { -1, ShenandoahNone}, { -1, ShenandoahNone}, 414 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 415 "updateBytesAdler32", 416 { { TypeFunc::Parms+1, ShenandoahLoad }, { -1, ShenandoahNone}, { -1, ShenandoahNone}, 417 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 418 "updateBytesCRC32C", 419 { { TypeFunc::Parms+1, ShenandoahLoad }, { TypeFunc::Parms+3, ShenandoahLoad}, { -1, ShenandoahNone}, 420 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 421 "counterMode_AESCrypt", 422 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { TypeFunc::Parms+2, ShenandoahLoad }, 423 { TypeFunc::Parms+3, ShenandoahStore }, { TypeFunc::Parms+5, ShenandoahStore }, { TypeFunc::Parms+6, ShenandoahStore } }, 424 "cipherBlockChaining_encryptAESCrypt", 425 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { TypeFunc::Parms+2, ShenandoahLoad }, 426 { TypeFunc::Parms+3, ShenandoahLoad }, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 427 "cipherBlockChaining_decryptAESCrypt", 428 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { TypeFunc::Parms+2, ShenandoahLoad }, 429 { TypeFunc::Parms+3, ShenandoahLoad }, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 430 "shenandoah_clone_barrier", 431 { { TypeFunc::Parms, ShenandoahLoad }, { -1, ShenandoahNone}, { -1, ShenandoahNone}, 432 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 433 "ghash_processBlocks", 434 { { TypeFunc::Parms, ShenandoahStore }, { TypeFunc::Parms+1, ShenandoahLoad }, { TypeFunc::Parms+2, ShenandoahLoad }, 435 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 436 "sha1_implCompress", 437 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { -1, ShenandoahNone }, 438 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 439 "sha256_implCompress", 440 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { -1, ShenandoahNone }, 441 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 442 "sha512_implCompress", 443 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { -1, ShenandoahNone }, 444 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 445 "sha1_implCompressMB", 446 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { -1, ShenandoahNone }, 447 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 448 "sha256_implCompressMB", 449 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { -1, ShenandoahNone }, 450 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 451 "sha512_implCompressMB", 452 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+1, ShenandoahStore }, { -1, ShenandoahNone }, 453 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 454 "encodeBlock", 455 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+3, ShenandoahStore }, { -1, ShenandoahNone }, 456 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 457 "decodeBlock", 458 { { TypeFunc::Parms, ShenandoahLoad }, { TypeFunc::Parms+3, ShenandoahStore }, { -1, ShenandoahNone }, 459 { -1, ShenandoahNone}, { -1, ShenandoahNone}, { -1, ShenandoahNone} }, 460 }; 461 462 if (call->is_call_to_arraycopystub()) { 463 Node* dest = nullptr; 464 const TypeTuple* args = n->as_Call()->_tf->domain(); 465 for (uint i = TypeFunc::Parms, j = 0; i < args->cnt(); i++) { 466 if (args->field_at(i)->isa_ptr()) { 467 j++; 468 if (j == 2) { 469 dest = n->in(i); 470 break; 471 } 472 } 473 } 474 if (!verify_helper(n->in(TypeFunc::Parms), phis, visited, ShenandoahLoad, trace, barriers_used) || 475 !verify_helper(dest, phis, visited, ShenandoahStore, trace, barriers_used)) { 476 report_verify_failure("Shenandoah verification: ArrayCopy should have barriers", n); 477 } 478 } else if (strlen(call->_name) > 5 && 479 !strcmp(call->_name + strlen(call->_name) - 5, "_fill")) { 480 if (!verify_helper(n->in(TypeFunc::Parms), phis, visited, ShenandoahStore, trace, barriers_used)) { 481 report_verify_failure("Shenandoah verification: _fill should have barriers", n); 482 } 483 } else if (!strcmp(call->_name, "shenandoah_wb_pre")) { 484 // skip 485 } else { 486 const int calls_len = sizeof(calls) / sizeof(calls[0]); 487 int i = 0; 488 for (; i < calls_len; i++) { 489 if (!strcmp(calls[i].name, call->_name)) { 490 break; 491 } 492 } 493 if (i != calls_len) { 494 const uint args_len = sizeof(calls[0].args) / sizeof(calls[0].args[0]); 495 for (uint j = 0; j < args_len; j++) { 496 int pos = calls[i].args[j].pos; 497 if (pos == -1) { 498 break; 499 } 500 if (!verify_helper(call->in(pos), phis, visited, calls[i].args[j].t, trace, barriers_used)) { 501 report_verify_failure("Shenandoah verification: intrinsic calls should have barriers", n); 502 } 503 } 504 for (uint j = TypeFunc::Parms; j < call->req(); j++) { 505 if (call->in(j)->bottom_type()->make_ptr() && 506 call->in(j)->bottom_type()->make_ptr()->isa_oopptr()) { 507 uint k = 0; 508 for (; k < args_len && calls[i].args[k].pos != (int)j; k++); 509 if (k == args_len) { 510 fatal("arg %d for call %s not covered", j, call->_name); 511 } 512 } 513 } 514 } else { 515 for (uint j = TypeFunc::Parms; j < call->req(); j++) { 516 if (call->in(j)->bottom_type()->make_ptr() && 517 call->in(j)->bottom_type()->make_ptr()->isa_oopptr()) { 518 fatal("%s not covered", call->_name); 519 } 520 } 521 } 522 } 523 } else if (n->Opcode() == Op_ShenandoahIUBarrier || n->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 524 // skip 525 } else if (n->is_AddP() 526 || n->is_Phi() 527 || n->is_ConstraintCast() 528 || n->Opcode() == Op_Return 529 || n->Opcode() == Op_CMoveP 530 || n->Opcode() == Op_CMoveN 531 || n->Opcode() == Op_Rethrow 532 || n->is_MemBar() 533 || n->Opcode() == Op_Conv2B 534 || n->Opcode() == Op_SafePoint 535 || n->is_CallJava() 536 || n->Opcode() == Op_Unlock 537 || n->Opcode() == Op_EncodeP 538 || n->Opcode() == Op_DecodeN) { 539 // nothing to do 540 } else { 541 static struct { 542 int opcode; 543 struct { 544 int pos; 545 verify_type t; 546 } inputs[2]; 547 } others[] = { 548 Op_FastLock, 549 { { 1, ShenandoahLoad }, { -1, ShenandoahNone} }, 550 Op_Lock, 551 { { TypeFunc::Parms, ShenandoahLoad }, { -1, ShenandoahNone} }, 552 Op_ArrayCopy, 553 { { ArrayCopyNode::Src, ShenandoahLoad }, { ArrayCopyNode::Dest, ShenandoahStore } }, 554 Op_StrCompressedCopy, 555 { { 2, ShenandoahLoad }, { 3, ShenandoahStore } }, 556 Op_StrInflatedCopy, 557 { { 2, ShenandoahLoad }, { 3, ShenandoahStore } }, 558 Op_AryEq, 559 { { 2, ShenandoahLoad }, { 3, ShenandoahLoad } }, 560 Op_StrIndexOf, 561 { { 2, ShenandoahLoad }, { 4, ShenandoahLoad } }, 562 Op_StrComp, 563 { { 2, ShenandoahLoad }, { 4, ShenandoahLoad } }, 564 Op_StrEquals, 565 { { 2, ShenandoahLoad }, { 3, ShenandoahLoad } }, 566 Op_VectorizedHashCode, 567 { { 2, ShenandoahLoad }, { -1, ShenandoahNone } }, 568 Op_EncodeISOArray, 569 { { 2, ShenandoahLoad }, { 3, ShenandoahStore } }, 570 Op_CountPositives, 571 { { 2, ShenandoahLoad }, { -1, ShenandoahNone} }, 572 Op_CastP2X, 573 { { 1, ShenandoahLoad }, { -1, ShenandoahNone} }, 574 Op_StrIndexOfChar, 575 { { 2, ShenandoahLoad }, { -1, ShenandoahNone } }, 576 }; 577 578 const int others_len = sizeof(others) / sizeof(others[0]); 579 int i = 0; 580 for (; i < others_len; i++) { 581 if (others[i].opcode == n->Opcode()) { 582 break; 583 } 584 } 585 uint stop = n->is_Call() ? n->as_Call()->tf()->domain()->cnt() : n->req(); 586 if (i != others_len) { 587 const uint inputs_len = sizeof(others[0].inputs) / sizeof(others[0].inputs[0]); 588 for (uint j = 0; j < inputs_len; j++) { 589 int pos = others[i].inputs[j].pos; 590 if (pos == -1) { 591 break; 592 } 593 if (!verify_helper(n->in(pos), phis, visited, others[i].inputs[j].t, trace, barriers_used)) { 594 report_verify_failure("Shenandoah verification: intrinsic calls should have barriers", n); 595 } 596 } 597 for (uint j = 1; j < stop; j++) { 598 if (n->in(j) != nullptr && n->in(j)->bottom_type()->make_ptr() && 599 n->in(j)->bottom_type()->make_ptr()->make_oopptr()) { 600 uint k = 0; 601 for (; k < inputs_len && others[i].inputs[k].pos != (int)j; k++); 602 if (k == inputs_len) { 603 fatal("arg %d for node %s not covered", j, n->Name()); 604 } 605 } 606 } 607 } else { 608 for (uint j = 1; j < stop; j++) { 609 if (n->in(j) != nullptr && n->in(j)->bottom_type()->make_ptr() && 610 n->in(j)->bottom_type()->make_ptr()->make_oopptr()) { 611 fatal("%s not covered", n->Name()); 612 } 613 } 614 } 615 } 616 617 if (n->is_SafePoint()) { 618 SafePointNode* sfpt = n->as_SafePoint(); 619 if (verify_no_useless_barrier && sfpt->jvms() != nullptr) { 620 for (uint i = sfpt->jvms()->scloff(); i < sfpt->jvms()->endoff(); i++) { 621 if (!verify_helper(sfpt->in(i), phis, visited, ShenandoahLoad, trace, barriers_used)) { 622 phis.clear(); 623 visited.reset(); 624 } 625 } 626 } 627 } 628 } 629 630 if (verify_no_useless_barrier) { 631 for (int i = 0; i < barriers.length(); i++) { 632 Node* n = barriers.at(i); 633 if (!barriers_used.member(n)) { 634 tty->print("XXX useless barrier"); n->dump(-2); 635 ShouldNotReachHere(); 636 } 637 } 638 } 639 } 640 #endif 641 642 bool ShenandoahBarrierC2Support::is_dominator_same_ctrl(Node* c, Node* d, Node* n, PhaseIdealLoop* phase) { 643 // That both nodes have the same control is not sufficient to prove 644 // domination, verify that there's no path from d to n 645 ResourceMark rm; 646 Unique_Node_List wq; 647 wq.push(d); 648 for (uint next = 0; next < wq.size(); next++) { 649 Node *m = wq.at(next); 650 if (m == n) { 651 return false; 652 } 653 if (m->is_Phi() && m->in(0)->is_Loop()) { 654 assert(phase->ctrl_or_self(m->in(LoopNode::EntryControl)) != c, "following loop entry should lead to new control"); 655 } else { 656 if (m->is_Store() || m->is_LoadStore()) { 657 // Take anti-dependencies into account 658 Node* mem = m->in(MemNode::Memory); 659 for (DUIterator_Fast imax, i = mem->fast_outs(imax); i < imax; i++) { 660 Node* u = mem->fast_out(i); 661 if (u->is_Load() && phase->C->can_alias(m->adr_type(), phase->C->get_alias_index(u->adr_type())) && 662 phase->ctrl_or_self(u) == c) { 663 wq.push(u); 664 } 665 } 666 } 667 for (uint i = 0; i < m->req(); i++) { 668 if (m->in(i) != nullptr && phase->ctrl_or_self(m->in(i)) == c) { 669 wq.push(m->in(i)); 670 } 671 } 672 } 673 } 674 return true; 675 } 676 677 bool ShenandoahBarrierC2Support::is_dominator(Node* d_c, Node* n_c, Node* d, Node* n, PhaseIdealLoop* phase) { 678 if (d_c != n_c) { 679 return phase->is_dominator(d_c, n_c); 680 } 681 return is_dominator_same_ctrl(d_c, d, n, phase); 682 } 683 684 Node* next_mem(Node* mem, int alias) { 685 Node* res = nullptr; 686 if (mem->is_Proj()) { 687 res = mem->in(0); 688 } else if (mem->is_SafePoint() || mem->is_MemBar()) { 689 res = mem->in(TypeFunc::Memory); 690 } else if (mem->is_Phi()) { 691 res = mem->in(1); 692 } else if (mem->is_MergeMem()) { 693 res = mem->as_MergeMem()->memory_at(alias); 694 } else if (mem->is_Store() || mem->is_LoadStore() || mem->is_ClearArray()) { 695 assert(alias == Compile::AliasIdxRaw, "following raw memory can't lead to a barrier"); 696 res = mem->in(MemNode::Memory); 697 } else { 698 #ifdef ASSERT 699 mem->dump(); 700 #endif 701 ShouldNotReachHere(); 702 } 703 return res; 704 } 705 706 Node* ShenandoahBarrierC2Support::no_branches(Node* c, Node* dom, bool allow_one_proj, PhaseIdealLoop* phase) { 707 Node* iffproj = nullptr; 708 while (c != dom) { 709 Node* next = phase->idom(c); 710 assert(next->unique_ctrl_out_or_null() == c || c->is_Proj() || c->is_Region(), "multiple control flow out but no proj or region?"); 711 if (c->is_Region()) { 712 ResourceMark rm; 713 Unique_Node_List wq; 714 wq.push(c); 715 for (uint i = 0; i < wq.size(); i++) { 716 Node *n = wq.at(i); 717 if (n == next) { 718 continue; 719 } 720 if (n->is_Region()) { 721 for (uint j = 1; j < n->req(); j++) { 722 wq.push(n->in(j)); 723 } 724 } else { 725 wq.push(n->in(0)); 726 } 727 } 728 for (uint i = 0; i < wq.size(); i++) { 729 Node *n = wq.at(i); 730 assert(n->is_CFG(), ""); 731 if (n->is_Multi()) { 732 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { 733 Node* u = n->fast_out(j); 734 if (u->is_CFG()) { 735 if (!wq.member(u) && !u->as_Proj()->is_uncommon_trap_proj(Deoptimization::Reason_none)) { 736 return NodeSentinel; 737 } 738 } 739 } 740 } 741 } 742 } else if (c->is_Proj()) { 743 if (c->is_IfProj()) { 744 if (c->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none) != nullptr) { 745 // continue; 746 } else { 747 if (!allow_one_proj) { 748 return NodeSentinel; 749 } 750 if (iffproj == nullptr) { 751 iffproj = c; 752 } else { 753 return NodeSentinel; 754 } 755 } 756 } else if (c->Opcode() == Op_JumpProj) { 757 return NodeSentinel; // unsupported 758 } else if (c->Opcode() == Op_CatchProj) { 759 return NodeSentinel; // unsupported 760 } else if (c->Opcode() == Op_CProj && next->is_NeverBranch()) { 761 return NodeSentinel; // unsupported 762 } else { 763 assert(next->unique_ctrl_out() == c, "unsupported branch pattern"); 764 } 765 } 766 c = next; 767 } 768 return iffproj; 769 } 770 771 Node* ShenandoahBarrierC2Support::dom_mem(Node* mem, Node* ctrl, int alias, Node*& mem_ctrl, PhaseIdealLoop* phase) { 772 ResourceMark rm; 773 VectorSet wq; 774 wq.set(mem->_idx); 775 mem_ctrl = phase->ctrl_or_self(mem); 776 while (!phase->is_dominator(mem_ctrl, ctrl) || mem_ctrl == ctrl) { 777 mem = next_mem(mem, alias); 778 if (wq.test_set(mem->_idx)) { 779 return nullptr; 780 } 781 mem_ctrl = phase->ctrl_or_self(mem); 782 } 783 if (mem->is_MergeMem()) { 784 mem = mem->as_MergeMem()->memory_at(alias); 785 mem_ctrl = phase->ctrl_or_self(mem); 786 } 787 return mem; 788 } 789 790 Node* ShenandoahBarrierC2Support::find_bottom_mem(Node* ctrl, PhaseIdealLoop* phase) { 791 Node* mem = nullptr; 792 Node* c = ctrl; 793 do { 794 if (c->is_Region()) { 795 for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax && mem == nullptr; i++) { 796 Node* u = c->fast_out(i); 797 if (u->is_Phi() && u->bottom_type() == Type::MEMORY) { 798 if (u->adr_type() == TypePtr::BOTTOM) { 799 mem = u; 800 } 801 } 802 } 803 } else { 804 if (c->is_Call() && c->as_Call()->adr_type() != nullptr) { 805 CallProjections projs; 806 c->as_Call()->extract_projections(&projs, true, false); 807 if (projs.fallthrough_memproj != nullptr) { 808 if (projs.fallthrough_memproj->adr_type() == TypePtr::BOTTOM) { 809 if (projs.catchall_memproj == nullptr) { 810 mem = projs.fallthrough_memproj; 811 } else { 812 if (phase->is_dominator(projs.fallthrough_catchproj, ctrl)) { 813 mem = projs.fallthrough_memproj; 814 } else { 815 assert(phase->is_dominator(projs.catchall_catchproj, ctrl), "one proj must dominate barrier"); 816 mem = projs.catchall_memproj; 817 } 818 } 819 } 820 } else { 821 Node* proj = c->as_Call()->proj_out(TypeFunc::Memory); 822 if (proj != nullptr && 823 proj->adr_type() == TypePtr::BOTTOM) { 824 mem = proj; 825 } 826 } 827 } else { 828 for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax; i++) { 829 Node* u = c->fast_out(i); 830 if (u->is_Proj() && 831 u->bottom_type() == Type::MEMORY && 832 u->adr_type() == TypePtr::BOTTOM) { 833 assert(c->is_SafePoint() || c->is_MemBar() || c->is_Start(), ""); 834 assert(mem == nullptr, "only one proj"); 835 mem = u; 836 } 837 } 838 assert(!c->is_Call() || c->as_Call()->adr_type() != nullptr || mem == nullptr, "no mem projection expected"); 839 } 840 } 841 c = phase->idom(c); 842 } while (mem == nullptr); 843 return mem; 844 } 845 846 void ShenandoahBarrierC2Support::follow_barrier_uses(Node* n, Node* ctrl, Unique_Node_List& uses, PhaseIdealLoop* phase) { 847 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 848 Node* u = n->fast_out(i); 849 if (!u->is_CFG() && phase->get_ctrl(u) == ctrl && (!u->is_Phi() || !u->in(0)->is_Loop() || u->in(LoopNode::LoopBackControl) != n)) { 850 uses.push(u); 851 } 852 } 853 } 854 855 static void hide_strip_mined_loop(OuterStripMinedLoopNode* outer, CountedLoopNode* inner, PhaseIdealLoop* phase) { 856 OuterStripMinedLoopEndNode* le = inner->outer_loop_end(); 857 Node* new_outer = new LoopNode(outer->in(LoopNode::EntryControl), outer->in(LoopNode::LoopBackControl)); 858 phase->register_control(new_outer, phase->get_loop(outer), outer->in(LoopNode::EntryControl)); 859 Node* new_le = new IfNode(le->in(0), le->in(1), le->_prob, le->_fcnt); 860 phase->register_control(new_le, phase->get_loop(le), le->in(0)); 861 phase->lazy_replace(outer, new_outer); 862 phase->lazy_replace(le, new_le); 863 inner->clear_strip_mined(); 864 } 865 866 void ShenandoahBarrierC2Support::test_gc_state(Node*& ctrl, Node* raw_mem, Node*& test_fail_ctrl, 867 PhaseIdealLoop* phase, int flags) { 868 PhaseIterGVN& igvn = phase->igvn(); 869 Node* old_ctrl = ctrl; 870 871 Node* thread = new ThreadLocalNode(); 872 Node* gc_state_offset = igvn.MakeConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 873 Node* gc_state_addr = new AddPNode(phase->C->top(), thread, gc_state_offset); 874 Node* gc_state = new LoadBNode(old_ctrl, raw_mem, gc_state_addr, 875 DEBUG_ONLY(phase->C->get_adr_type(Compile::AliasIdxRaw)) NOT_DEBUG(nullptr), 876 TypeInt::BYTE, MemNode::unordered); 877 Node* gc_state_and = new AndINode(gc_state, igvn.intcon(flags)); 878 Node* gc_state_cmp = new CmpINode(gc_state_and, igvn.zerocon(T_INT)); 879 Node* gc_state_bool = new BoolNode(gc_state_cmp, BoolTest::ne); 880 881 IfNode* gc_state_iff = new IfNode(old_ctrl, gc_state_bool, PROB_UNLIKELY(0.999), COUNT_UNKNOWN); 882 ctrl = new IfTrueNode(gc_state_iff); 883 test_fail_ctrl = new IfFalseNode(gc_state_iff); 884 885 IdealLoopTree* loop = phase->get_loop(old_ctrl); 886 phase->register_control(gc_state_iff, loop, old_ctrl); 887 phase->register_control(ctrl, loop, gc_state_iff); 888 phase->register_control(test_fail_ctrl, loop, gc_state_iff); 889 890 phase->register_new_node(thread, old_ctrl); 891 phase->register_new_node(gc_state_addr, old_ctrl); 892 phase->register_new_node(gc_state, old_ctrl); 893 phase->register_new_node(gc_state_and, old_ctrl); 894 phase->register_new_node(gc_state_cmp, old_ctrl); 895 phase->register_new_node(gc_state_bool, old_ctrl); 896 897 phase->set_ctrl(gc_state_offset, phase->C->root()); 898 899 assert(is_gc_state_test(gc_state_iff, flags), "Should match the shape"); 900 } 901 902 void ShenandoahBarrierC2Support::test_null(Node*& ctrl, Node* val, Node*& null_ctrl, PhaseIdealLoop* phase) { 903 Node* old_ctrl = ctrl; 904 PhaseIterGVN& igvn = phase->igvn(); 905 906 const Type* val_t = igvn.type(val); 907 if (val_t->meet(TypePtr::NULL_PTR) == val_t) { 908 Node* null_cmp = new CmpPNode(val, igvn.zerocon(T_OBJECT)); 909 Node* null_test = new BoolNode(null_cmp, BoolTest::ne); 910 911 IfNode* null_iff = new IfNode(old_ctrl, null_test, PROB_LIKELY(0.999), COUNT_UNKNOWN); 912 ctrl = new IfTrueNode(null_iff); 913 null_ctrl = new IfFalseNode(null_iff); 914 915 IdealLoopTree* loop = phase->get_loop(old_ctrl); 916 phase->register_control(null_iff, loop, old_ctrl); 917 phase->register_control(ctrl, loop, null_iff); 918 phase->register_control(null_ctrl, loop, null_iff); 919 920 phase->register_new_node(null_cmp, old_ctrl); 921 phase->register_new_node(null_test, old_ctrl); 922 } 923 } 924 925 void ShenandoahBarrierC2Support::test_in_cset(Node*& ctrl, Node*& not_cset_ctrl, Node* val, Node* raw_mem, PhaseIdealLoop* phase) { 926 Node* old_ctrl = ctrl; 927 PhaseIterGVN& igvn = phase->igvn(); 928 929 Node* raw_val = new CastP2XNode(old_ctrl, val); 930 Node* cset_idx = new URShiftXNode(raw_val, igvn.intcon(ShenandoahHeapRegion::region_size_bytes_shift_jint())); 931 932 // Figure out the target cset address with raw pointer math. 933 // This avoids matching AddP+LoadB that would emit inefficient code. 934 // See JDK-8245465. 935 Node* cset_addr_ptr = igvn.makecon(TypeRawPtr::make(ShenandoahHeap::in_cset_fast_test_addr())); 936 Node* cset_addr = new CastP2XNode(old_ctrl, cset_addr_ptr); 937 Node* cset_load_addr = new AddXNode(cset_addr, cset_idx); 938 Node* cset_load_ptr = new CastX2PNode(cset_load_addr); 939 940 Node* cset_load = new LoadBNode(old_ctrl, raw_mem, cset_load_ptr, 941 DEBUG_ONLY(phase->C->get_adr_type(Compile::AliasIdxRaw)) NOT_DEBUG(nullptr), 942 TypeInt::BYTE, MemNode::unordered); 943 Node* cset_cmp = new CmpINode(cset_load, igvn.zerocon(T_INT)); 944 Node* cset_bool = new BoolNode(cset_cmp, BoolTest::ne); 945 946 IfNode* cset_iff = new IfNode(old_ctrl, cset_bool, PROB_UNLIKELY(0.999), COUNT_UNKNOWN); 947 ctrl = new IfTrueNode(cset_iff); 948 not_cset_ctrl = new IfFalseNode(cset_iff); 949 950 IdealLoopTree *loop = phase->get_loop(old_ctrl); 951 phase->register_control(cset_iff, loop, old_ctrl); 952 phase->register_control(ctrl, loop, cset_iff); 953 phase->register_control(not_cset_ctrl, loop, cset_iff); 954 955 phase->set_ctrl(cset_addr_ptr, phase->C->root()); 956 957 phase->register_new_node(raw_val, old_ctrl); 958 phase->register_new_node(cset_idx, old_ctrl); 959 phase->register_new_node(cset_addr, old_ctrl); 960 phase->register_new_node(cset_load_addr, old_ctrl); 961 phase->register_new_node(cset_load_ptr, old_ctrl); 962 phase->register_new_node(cset_load, old_ctrl); 963 phase->register_new_node(cset_cmp, old_ctrl); 964 phase->register_new_node(cset_bool, old_ctrl); 965 } 966 967 void ShenandoahBarrierC2Support::call_lrb_stub(Node*& ctrl, Node*& val, Node* load_addr, 968 DecoratorSet decorators, PhaseIdealLoop* phase) { 969 IdealLoopTree*loop = phase->get_loop(ctrl); 970 const TypePtr* obj_type = phase->igvn().type(val)->is_oopptr(); 971 972 address calladdr = nullptr; 973 const char* name = nullptr; 974 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators); 975 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators); 976 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators); 977 bool is_native = ShenandoahBarrierSet::is_native_access(decorators); 978 bool is_narrow = UseCompressedOops && !is_native; 979 if (is_strong) { 980 if (is_narrow) { 981 calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow); 982 name = "load_reference_barrier_strong_narrow"; 983 } else { 984 calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong); 985 name = "load_reference_barrier_strong"; 986 } 987 } else if (is_weak) { 988 if (is_narrow) { 989 calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow); 990 name = "load_reference_barrier_weak_narrow"; 991 } else { 992 calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak); 993 name = "load_reference_barrier_weak"; 994 } 995 } else { 996 assert(is_phantom, "only remaining strength"); 997 if (is_narrow) { 998 calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom_narrow); 999 name = "load_reference_barrier_phantom_narrow"; 1000 } else { 1001 calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom); 1002 name = "load_reference_barrier_phantom"; 1003 } 1004 } 1005 Node* call = new CallLeafNode(ShenandoahBarrierSetC2::shenandoah_load_reference_barrier_Type(), calladdr, name, TypeRawPtr::BOTTOM); 1006 1007 call->init_req(TypeFunc::Control, ctrl); 1008 call->init_req(TypeFunc::I_O, phase->C->top()); 1009 call->init_req(TypeFunc::Memory, phase->C->top()); 1010 call->init_req(TypeFunc::FramePtr, phase->C->top()); 1011 call->init_req(TypeFunc::ReturnAdr, phase->C->top()); 1012 call->init_req(TypeFunc::Parms, val); 1013 call->init_req(TypeFunc::Parms+1, load_addr); 1014 phase->register_control(call, loop, ctrl); 1015 ctrl = new ProjNode(call, TypeFunc::Control); 1016 phase->register_control(ctrl, loop, call); 1017 val = new ProjNode(call, TypeFunc::Parms); 1018 phase->register_new_node(val, call); 1019 val = new CheckCastPPNode(ctrl, val, obj_type); 1020 phase->register_new_node(val, ctrl); 1021 } 1022 1023 void ShenandoahBarrierC2Support::fix_ctrl(Node* barrier, Node* region, const MemoryGraphFixer& fixer, Unique_Node_List& uses, Unique_Node_List& uses_to_ignore, uint last, PhaseIdealLoop* phase) { 1024 Node* ctrl = phase->get_ctrl(barrier); 1025 Node* init_raw_mem = fixer.find_mem(ctrl, barrier); 1026 1027 // Update the control of all nodes that should be after the 1028 // barrier control flow 1029 uses.clear(); 1030 // Every node that is control dependent on the barrier's input 1031 // control will be after the expanded barrier. The raw memory (if 1032 // its memory is control dependent on the barrier's input control) 1033 // must stay above the barrier. 1034 uses_to_ignore.clear(); 1035 if (phase->has_ctrl(init_raw_mem) && phase->get_ctrl(init_raw_mem) == ctrl && !init_raw_mem->is_Phi()) { 1036 uses_to_ignore.push(init_raw_mem); 1037 } 1038 for (uint next = 0; next < uses_to_ignore.size(); next++) { 1039 Node *n = uses_to_ignore.at(next); 1040 for (uint i = 0; i < n->req(); i++) { 1041 Node* in = n->in(i); 1042 if (in != nullptr && phase->has_ctrl(in) && phase->get_ctrl(in) == ctrl) { 1043 uses_to_ignore.push(in); 1044 } 1045 } 1046 } 1047 for (DUIterator_Fast imax, i = ctrl->fast_outs(imax); i < imax; i++) { 1048 Node* u = ctrl->fast_out(i); 1049 if (u->_idx < last && 1050 u != barrier && 1051 !u->depends_only_on_test() && // preserve dependency on test 1052 !uses_to_ignore.member(u) && 1053 (u->in(0) != ctrl || (!u->is_Region() && !u->is_Phi())) && 1054 (ctrl->Opcode() != Op_CatchProj || u->Opcode() != Op_CreateEx)) { 1055 Node* old_c = phase->ctrl_or_self(u); 1056 Node* c = old_c; 1057 if (c != ctrl || 1058 is_dominator_same_ctrl(old_c, barrier, u, phase) || 1059 ShenandoahBarrierSetC2::is_shenandoah_state_load(u)) { 1060 phase->igvn().rehash_node_delayed(u); 1061 int nb = u->replace_edge(ctrl, region, &phase->igvn()); 1062 if (u->is_CFG()) { 1063 if (phase->idom(u) == ctrl) { 1064 phase->set_idom(u, region, phase->dom_depth(region)); 1065 } 1066 } else if (phase->get_ctrl(u) == ctrl) { 1067 assert(u != init_raw_mem, "should leave input raw mem above the barrier"); 1068 uses.push(u); 1069 } 1070 assert(nb == 1, "more than 1 ctrl input?"); 1071 --i, imax -= nb; 1072 } 1073 } 1074 } 1075 } 1076 1077 static Node* create_phis_on_call_return(Node* ctrl, Node* c, Node* n, Node* n_clone, const CallProjections& projs, PhaseIdealLoop* phase) { 1078 Node* region = nullptr; 1079 while (c != ctrl) { 1080 if (c->is_Region()) { 1081 region = c; 1082 } 1083 c = phase->idom(c); 1084 } 1085 assert(region != nullptr, ""); 1086 Node* phi = new PhiNode(region, n->bottom_type()); 1087 for (uint j = 1; j < region->req(); j++) { 1088 Node* in = region->in(j); 1089 if (phase->is_dominator(projs.fallthrough_catchproj, in)) { 1090 phi->init_req(j, n); 1091 } else if (phase->is_dominator(projs.catchall_catchproj, in)) { 1092 phi->init_req(j, n_clone); 1093 } else { 1094 phi->init_req(j, create_phis_on_call_return(ctrl, in, n, n_clone, projs, phase)); 1095 } 1096 } 1097 phase->register_new_node(phi, region); 1098 return phi; 1099 } 1100 1101 void ShenandoahBarrierC2Support::pin_and_expand(PhaseIdealLoop* phase) { 1102 ShenandoahBarrierSetC2State* state = ShenandoahBarrierSetC2::bsc2()->state(); 1103 1104 Unique_Node_List uses; 1105 for (int i = 0; i < state->iu_barriers_count(); i++) { 1106 Node* barrier = state->iu_barrier(i); 1107 Node* ctrl = phase->get_ctrl(barrier); 1108 IdealLoopTree* loop = phase->get_loop(ctrl); 1109 Node* head = loop->head(); 1110 if (head->is_OuterStripMinedLoop()) { 1111 // Expanding a barrier here will break loop strip mining 1112 // verification. Transform the loop so the loop nest doesn't 1113 // appear as strip mined. 1114 OuterStripMinedLoopNode* outer = head->as_OuterStripMinedLoop(); 1115 hide_strip_mined_loop(outer, outer->unique_ctrl_out()->as_CountedLoop(), phase); 1116 } 1117 } 1118 1119 Node_Stack stack(0); 1120 Node_List clones; 1121 for (int i = state->load_reference_barriers_count() - 1; i >= 0; i--) { 1122 ShenandoahLoadReferenceBarrierNode* lrb = state->load_reference_barrier(i); 1123 1124 Node* ctrl = phase->get_ctrl(lrb); 1125 Node* val = lrb->in(ShenandoahLoadReferenceBarrierNode::ValueIn); 1126 1127 CallStaticJavaNode* unc = nullptr; 1128 Node* unc_ctrl = nullptr; 1129 Node* uncasted_val = val; 1130 1131 for (DUIterator_Fast imax, i = lrb->fast_outs(imax); i < imax; i++) { 1132 Node* u = lrb->fast_out(i); 1133 if (u->Opcode() == Op_CastPP && 1134 u->in(0) != nullptr && 1135 phase->is_dominator(u->in(0), ctrl)) { 1136 const Type* u_t = phase->igvn().type(u); 1137 1138 if (u_t->meet(TypePtr::NULL_PTR) != u_t && 1139 u->in(0)->Opcode() == Op_IfTrue && 1140 u->in(0)->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none) && 1141 u->in(0)->in(0)->is_If() && 1142 u->in(0)->in(0)->in(1)->Opcode() == Op_Bool && 1143 u->in(0)->in(0)->in(1)->as_Bool()->_test._test == BoolTest::ne && 1144 u->in(0)->in(0)->in(1)->in(1)->Opcode() == Op_CmpP && 1145 u->in(0)->in(0)->in(1)->in(1)->in(1) == val && 1146 u->in(0)->in(0)->in(1)->in(1)->in(2)->bottom_type() == TypePtr::NULL_PTR) { 1147 IdealLoopTree* loop = phase->get_loop(ctrl); 1148 IdealLoopTree* unc_loop = phase->get_loop(u->in(0)); 1149 1150 if (!unc_loop->is_member(loop)) { 1151 continue; 1152 } 1153 1154 Node* branch = no_branches(ctrl, u->in(0), false, phase); 1155 assert(branch == nullptr || branch == NodeSentinel, "was not looking for a branch"); 1156 if (branch == NodeSentinel) { 1157 continue; 1158 } 1159 1160 Node* iff = u->in(0)->in(0); 1161 Node* bol = iff->in(1)->clone(); 1162 Node* cmp = bol->in(1)->clone(); 1163 cmp->set_req(1, lrb); 1164 bol->set_req(1, cmp); 1165 phase->igvn().replace_input_of(iff, 1, bol); 1166 phase->set_ctrl(lrb, iff->in(0)); 1167 phase->register_new_node(cmp, iff->in(0)); 1168 phase->register_new_node(bol, iff->in(0)); 1169 break; 1170 } 1171 } 1172 } 1173 if ((ctrl->is_Proj() && ctrl->in(0)->is_CallJava()) || ctrl->is_CallJava()) { 1174 CallNode* call = ctrl->is_Proj() ? ctrl->in(0)->as_CallJava() : ctrl->as_CallJava(); 1175 if (call->entry_point() == OptoRuntime::rethrow_stub()) { 1176 // The rethrow call may have too many projections to be 1177 // properly handled here. Given there's no reason for a 1178 // barrier to depend on the call, move it above the call 1179 stack.push(lrb, 0); 1180 do { 1181 Node* n = stack.node(); 1182 uint idx = stack.index(); 1183 if (idx < n->req()) { 1184 Node* in = n->in(idx); 1185 stack.set_index(idx+1); 1186 if (in != nullptr) { 1187 if (phase->has_ctrl(in)) { 1188 if (phase->is_dominator(call, phase->get_ctrl(in))) { 1189 #ifdef ASSERT 1190 for (uint i = 0; i < stack.size(); i++) { 1191 assert(stack.node_at(i) != in, "node shouldn't have been seen yet"); 1192 } 1193 #endif 1194 stack.push(in, 0); 1195 } 1196 } else { 1197 assert(phase->is_dominator(in, call->in(0)), "no dependency on the call"); 1198 } 1199 } 1200 } else { 1201 phase->set_ctrl(n, call->in(0)); 1202 stack.pop(); 1203 } 1204 } while(stack.size() > 0); 1205 continue; 1206 } 1207 CallProjections projs; 1208 call->extract_projections(&projs, false, false); 1209 1210 #ifdef ASSERT 1211 VectorSet cloned; 1212 #endif 1213 Node* lrb_clone = lrb->clone(); 1214 phase->register_new_node(lrb_clone, projs.catchall_catchproj); 1215 phase->set_ctrl(lrb, projs.fallthrough_catchproj); 1216 1217 stack.push(lrb, 0); 1218 clones.push(lrb_clone); 1219 1220 do { 1221 assert(stack.size() == clones.size(), ""); 1222 Node* n = stack.node(); 1223 #ifdef ASSERT 1224 if (n->is_Load()) { 1225 Node* mem = n->in(MemNode::Memory); 1226 for (DUIterator_Fast jmax, j = mem->fast_outs(jmax); j < jmax; j++) { 1227 Node* u = mem->fast_out(j); 1228 assert(!u->is_Store() || !u->is_LoadStore() || phase->get_ctrl(u) != ctrl, "anti dependent store?"); 1229 } 1230 } 1231 #endif 1232 uint idx = stack.index(); 1233 Node* n_clone = clones.at(clones.size()-1); 1234 if (idx < n->outcnt()) { 1235 Node* u = n->raw_out(idx); 1236 Node* c = phase->ctrl_or_self(u); 1237 if (phase->is_dominator(call, c) && phase->is_dominator(c, projs.fallthrough_proj)) { 1238 stack.set_index(idx+1); 1239 assert(!u->is_CFG(), ""); 1240 stack.push(u, 0); 1241 assert(!cloned.test_set(u->_idx), "only one clone"); 1242 Node* u_clone = u->clone(); 1243 int nb = u_clone->replace_edge(n, n_clone, &phase->igvn()); 1244 assert(nb > 0, "should have replaced some uses"); 1245 phase->register_new_node(u_clone, projs.catchall_catchproj); 1246 clones.push(u_clone); 1247 phase->set_ctrl(u, projs.fallthrough_catchproj); 1248 } else { 1249 bool replaced = false; 1250 if (u->is_Phi()) { 1251 for (uint k = 1; k < u->req(); k++) { 1252 if (u->in(k) == n) { 1253 if (phase->is_dominator(projs.catchall_catchproj, u->in(0)->in(k))) { 1254 phase->igvn().replace_input_of(u, k, n_clone); 1255 replaced = true; 1256 } else if (!phase->is_dominator(projs.fallthrough_catchproj, u->in(0)->in(k))) { 1257 phase->igvn().replace_input_of(u, k, create_phis_on_call_return(ctrl, u->in(0)->in(k), n, n_clone, projs, phase)); 1258 replaced = true; 1259 } 1260 } 1261 } 1262 } else { 1263 if (phase->is_dominator(projs.catchall_catchproj, c)) { 1264 phase->igvn().rehash_node_delayed(u); 1265 int nb = u->replace_edge(n, n_clone, &phase->igvn()); 1266 assert(nb > 0, "should have replaced some uses"); 1267 replaced = true; 1268 } else if (!phase->is_dominator(projs.fallthrough_catchproj, c)) { 1269 if (u->is_If()) { 1270 // Can't break If/Bool/Cmp chain 1271 assert(n->is_Bool(), "unexpected If shape"); 1272 assert(stack.node_at(stack.size()-2)->is_Cmp(), "unexpected If shape"); 1273 assert(n_clone->is_Bool(), "unexpected clone"); 1274 assert(clones.at(clones.size()-2)->is_Cmp(), "unexpected clone"); 1275 Node* bol_clone = n->clone(); 1276 Node* cmp_clone = stack.node_at(stack.size()-2)->clone(); 1277 bol_clone->set_req(1, cmp_clone); 1278 1279 Node* nn = stack.node_at(stack.size()-3); 1280 Node* nn_clone = clones.at(clones.size()-3); 1281 assert(nn->Opcode() == nn_clone->Opcode(), "mismatch"); 1282 1283 int nb = cmp_clone->replace_edge(nn, create_phis_on_call_return(ctrl, c, nn, nn_clone, projs, phase), 1284 &phase->igvn()); 1285 assert(nb > 0, "should have replaced some uses"); 1286 1287 phase->register_new_node(bol_clone, u->in(0)); 1288 phase->register_new_node(cmp_clone, u->in(0)); 1289 1290 phase->igvn().replace_input_of(u, 1, bol_clone); 1291 1292 } else { 1293 phase->igvn().rehash_node_delayed(u); 1294 int nb = u->replace_edge(n, create_phis_on_call_return(ctrl, c, n, n_clone, projs, phase), &phase->igvn()); 1295 assert(nb > 0, "should have replaced some uses"); 1296 } 1297 replaced = true; 1298 } 1299 } 1300 if (!replaced) { 1301 stack.set_index(idx+1); 1302 } 1303 } 1304 } else { 1305 stack.pop(); 1306 clones.pop(); 1307 } 1308 } while (stack.size() > 0); 1309 assert(stack.size() == 0 && clones.size() == 0, ""); 1310 } 1311 } 1312 1313 for (int i = 0; i < state->load_reference_barriers_count(); i++) { 1314 ShenandoahLoadReferenceBarrierNode* lrb = state->load_reference_barrier(i); 1315 Node* ctrl = phase->get_ctrl(lrb); 1316 IdealLoopTree* loop = phase->get_loop(ctrl); 1317 Node* head = loop->head(); 1318 if (head->is_OuterStripMinedLoop()) { 1319 // Expanding a barrier here will break loop strip mining 1320 // verification. Transform the loop so the loop nest doesn't 1321 // appear as strip mined. 1322 OuterStripMinedLoopNode* outer = head->as_OuterStripMinedLoop(); 1323 hide_strip_mined_loop(outer, outer->unique_ctrl_out()->as_CountedLoop(), phase); 1324 } 1325 if (head->is_BaseCountedLoop() && ctrl->is_IfProj() && ctrl->in(0)->is_BaseCountedLoopEnd() && 1326 head->as_BaseCountedLoop()->loopexit() == ctrl->in(0)) { 1327 Node* entry = head->in(LoopNode::EntryControl); 1328 Node* backedge = head->in(LoopNode::LoopBackControl); 1329 Node* new_head = new LoopNode(entry, backedge); 1330 phase->register_control(new_head, phase->get_loop(entry), entry); 1331 phase->lazy_replace(head, new_head); 1332 } 1333 } 1334 1335 // Expand load-reference-barriers 1336 MemoryGraphFixer fixer(Compile::AliasIdxRaw, true, phase); 1337 Unique_Node_List uses_to_ignore; 1338 for (int i = state->load_reference_barriers_count() - 1; i >= 0; i--) { 1339 ShenandoahLoadReferenceBarrierNode* lrb = state->load_reference_barrier(i); 1340 uint last = phase->C->unique(); 1341 Node* ctrl = phase->get_ctrl(lrb); 1342 Node* val = lrb->in(ShenandoahLoadReferenceBarrierNode::ValueIn); 1343 1344 Node* orig_ctrl = ctrl; 1345 1346 Node* raw_mem = fixer.find_mem(ctrl, lrb); 1347 Node* raw_mem_for_ctrl = fixer.find_mem(ctrl, nullptr); 1348 1349 IdealLoopTree *loop = phase->get_loop(ctrl); 1350 1351 Node* heap_stable_ctrl = nullptr; 1352 Node* null_ctrl = nullptr; 1353 1354 assert(val->bottom_type()->make_oopptr(), "need oop"); 1355 assert(val->bottom_type()->make_oopptr()->const_oop() == nullptr, "expect non-constant"); 1356 1357 enum { _heap_stable = 1, _evac_path, _not_cset, PATH_LIMIT }; 1358 Node* region = new RegionNode(PATH_LIMIT); 1359 Node* val_phi = new PhiNode(region, val->bottom_type()->is_oopptr()); 1360 1361 // Stable path. 1362 int flags = ShenandoahHeap::HAS_FORWARDED; 1363 if (!ShenandoahBarrierSet::is_strong_access(lrb->decorators())) { 1364 flags |= ShenandoahHeap::WEAK_ROOTS; 1365 } 1366 test_gc_state(ctrl, raw_mem, heap_stable_ctrl, phase, flags); 1367 IfNode* heap_stable_iff = heap_stable_ctrl->in(0)->as_If(); 1368 1369 // Heap stable case 1370 region->init_req(_heap_stable, heap_stable_ctrl); 1371 val_phi->init_req(_heap_stable, val); 1372 1373 // Test for in-cset, unless it's a native-LRB. Native LRBs need to return null 1374 // even for non-cset objects to prevent resurrection of such objects. 1375 // Wires !in_cset(obj) to slot 2 of region and phis 1376 Node* not_cset_ctrl = nullptr; 1377 if (ShenandoahBarrierSet::is_strong_access(lrb->decorators())) { 1378 test_in_cset(ctrl, not_cset_ctrl, val, raw_mem, phase); 1379 } 1380 if (not_cset_ctrl != nullptr) { 1381 region->init_req(_not_cset, not_cset_ctrl); 1382 val_phi->init_req(_not_cset, val); 1383 } else { 1384 region->del_req(_not_cset); 1385 val_phi->del_req(_not_cset); 1386 } 1387 1388 // Resolve object when orig-value is in cset. 1389 // Make the unconditional resolve for fwdptr. 1390 1391 // Call lrb-stub and wire up that path in slots 4 1392 Node* result_mem = nullptr; 1393 1394 Node* addr; 1395 { 1396 VectorSet visited; 1397 addr = get_load_addr(phase, visited, lrb); 1398 } 1399 if (addr->Opcode() == Op_AddP) { 1400 Node* orig_base = addr->in(AddPNode::Base); 1401 Node* base = new CheckCastPPNode(ctrl, orig_base, orig_base->bottom_type(), ConstraintCastNode::StrongDependency); 1402 phase->register_new_node(base, ctrl); 1403 if (addr->in(AddPNode::Base) == addr->in((AddPNode::Address))) { 1404 // Field access 1405 addr = addr->clone(); 1406 addr->set_req(AddPNode::Base, base); 1407 addr->set_req(AddPNode::Address, base); 1408 phase->register_new_node(addr, ctrl); 1409 } else { 1410 Node* addr2 = addr->in(AddPNode::Address); 1411 if (addr2->Opcode() == Op_AddP && addr2->in(AddPNode::Base) == addr2->in(AddPNode::Address) && 1412 addr2->in(AddPNode::Base) == orig_base) { 1413 addr2 = addr2->clone(); 1414 addr2->set_req(AddPNode::Base, base); 1415 addr2->set_req(AddPNode::Address, base); 1416 phase->register_new_node(addr2, ctrl); 1417 addr = addr->clone(); 1418 addr->set_req(AddPNode::Base, base); 1419 addr->set_req(AddPNode::Address, addr2); 1420 phase->register_new_node(addr, ctrl); 1421 } 1422 } 1423 } 1424 call_lrb_stub(ctrl, val, addr, lrb->decorators(), phase); 1425 region->init_req(_evac_path, ctrl); 1426 val_phi->init_req(_evac_path, val); 1427 1428 phase->register_control(region, loop, heap_stable_iff); 1429 Node* out_val = val_phi; 1430 phase->register_new_node(val_phi, region); 1431 1432 fix_ctrl(lrb, region, fixer, uses, uses_to_ignore, last, phase); 1433 1434 ctrl = orig_ctrl; 1435 1436 phase->igvn().replace_node(lrb, out_val); 1437 1438 follow_barrier_uses(out_val, ctrl, uses, phase); 1439 1440 for(uint next = 0; next < uses.size(); next++ ) { 1441 Node *n = uses.at(next); 1442 assert(phase->get_ctrl(n) == ctrl, "bad control"); 1443 assert(n != raw_mem, "should leave input raw mem above the barrier"); 1444 phase->set_ctrl(n, region); 1445 follow_barrier_uses(n, ctrl, uses, phase); 1446 } 1447 fixer.record_new_ctrl(ctrl, region, raw_mem, raw_mem_for_ctrl); 1448 } 1449 // Done expanding load-reference-barriers. 1450 assert(ShenandoahBarrierSetC2::bsc2()->state()->load_reference_barriers_count() == 0, "all load reference barrier nodes should have been replaced"); 1451 1452 for (int i = state->iu_barriers_count() - 1; i >= 0; i--) { 1453 Node* barrier = state->iu_barrier(i); 1454 Node* pre_val = barrier->in(1); 1455 1456 if (phase->igvn().type(pre_val)->higher_equal(TypePtr::NULL_PTR)) { 1457 ShouldNotReachHere(); 1458 continue; 1459 } 1460 1461 Node* ctrl = phase->get_ctrl(barrier); 1462 1463 if (ctrl->is_Proj() && ctrl->in(0)->is_CallJava()) { 1464 assert(is_dominator(phase->get_ctrl(pre_val), ctrl->in(0)->in(0), pre_val, ctrl->in(0), phase), "can't move"); 1465 ctrl = ctrl->in(0)->in(0); 1466 phase->set_ctrl(barrier, ctrl); 1467 } else if (ctrl->is_CallRuntime()) { 1468 assert(is_dominator(phase->get_ctrl(pre_val), ctrl->in(0), pre_val, ctrl, phase), "can't move"); 1469 ctrl = ctrl->in(0); 1470 phase->set_ctrl(barrier, ctrl); 1471 } 1472 1473 Node* init_ctrl = ctrl; 1474 IdealLoopTree* loop = phase->get_loop(ctrl); 1475 Node* raw_mem = fixer.find_mem(ctrl, barrier); 1476 Node* init_raw_mem = raw_mem; 1477 Node* raw_mem_for_ctrl = fixer.find_mem(ctrl, nullptr); 1478 Node* heap_stable_ctrl = nullptr; 1479 Node* null_ctrl = nullptr; 1480 uint last = phase->C->unique(); 1481 1482 enum { _heap_stable = 1, _heap_unstable, PATH_LIMIT }; 1483 Node* region = new RegionNode(PATH_LIMIT); 1484 Node* phi = PhiNode::make(region, raw_mem, Type::MEMORY, TypeRawPtr::BOTTOM); 1485 1486 enum { _fast_path = 1, _slow_path, _null_path, PATH_LIMIT2 }; 1487 Node* region2 = new RegionNode(PATH_LIMIT2); 1488 Node* phi2 = PhiNode::make(region2, raw_mem, Type::MEMORY, TypeRawPtr::BOTTOM); 1489 1490 // Stable path. 1491 test_gc_state(ctrl, raw_mem, heap_stable_ctrl, phase, ShenandoahHeap::MARKING); 1492 region->init_req(_heap_stable, heap_stable_ctrl); 1493 phi->init_req(_heap_stable, raw_mem); 1494 1495 // Null path 1496 Node* reg2_ctrl = nullptr; 1497 test_null(ctrl, pre_val, null_ctrl, phase); 1498 if (null_ctrl != nullptr) { 1499 reg2_ctrl = null_ctrl->in(0); 1500 region2->init_req(_null_path, null_ctrl); 1501 phi2->init_req(_null_path, raw_mem); 1502 } else { 1503 region2->del_req(_null_path); 1504 phi2->del_req(_null_path); 1505 } 1506 1507 const int index_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()); 1508 const int buffer_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()); 1509 Node* thread = new ThreadLocalNode(); 1510 phase->register_new_node(thread, ctrl); 1511 Node* buffer_adr = new AddPNode(phase->C->top(), thread, phase->igvn().MakeConX(buffer_offset)); 1512 phase->register_new_node(buffer_adr, ctrl); 1513 Node* index_adr = new AddPNode(phase->C->top(), thread, phase->igvn().MakeConX(index_offset)); 1514 phase->register_new_node(index_adr, ctrl); 1515 1516 BasicType index_bt = TypeX_X->basic_type(); 1517 assert(sizeof(size_t) == type2aelembytes(index_bt), "Loading Shenandoah SATBMarkQueue::_index with wrong size."); 1518 const TypePtr* adr_type = TypeRawPtr::BOTTOM; 1519 Node* index = new LoadXNode(ctrl, raw_mem, index_adr, adr_type, TypeX_X, MemNode::unordered); 1520 phase->register_new_node(index, ctrl); 1521 Node* index_cmp = new CmpXNode(index, phase->igvn().MakeConX(0)); 1522 phase->register_new_node(index_cmp, ctrl); 1523 Node* index_test = new BoolNode(index_cmp, BoolTest::ne); 1524 phase->register_new_node(index_test, ctrl); 1525 IfNode* queue_full_iff = new IfNode(ctrl, index_test, PROB_LIKELY(0.999), COUNT_UNKNOWN); 1526 if (reg2_ctrl == nullptr) reg2_ctrl = queue_full_iff; 1527 phase->register_control(queue_full_iff, loop, ctrl); 1528 Node* not_full = new IfTrueNode(queue_full_iff); 1529 phase->register_control(not_full, loop, queue_full_iff); 1530 Node* full = new IfFalseNode(queue_full_iff); 1531 phase->register_control(full, loop, queue_full_iff); 1532 1533 ctrl = not_full; 1534 1535 Node* next_index = new SubXNode(index, phase->igvn().MakeConX(sizeof(intptr_t))); 1536 phase->register_new_node(next_index, ctrl); 1537 1538 Node* buffer = new LoadPNode(ctrl, raw_mem, buffer_adr, adr_type, TypeRawPtr::NOTNULL, MemNode::unordered); 1539 phase->register_new_node(buffer, ctrl); 1540 Node *log_addr = new AddPNode(phase->C->top(), buffer, next_index); 1541 phase->register_new_node(log_addr, ctrl); 1542 Node* log_store = new StorePNode(ctrl, raw_mem, log_addr, adr_type, pre_val, MemNode::unordered); 1543 phase->register_new_node(log_store, ctrl); 1544 // update the index 1545 Node* index_update = new StoreXNode(ctrl, log_store, index_adr, adr_type, next_index, MemNode::unordered); 1546 phase->register_new_node(index_update, ctrl); 1547 1548 // Fast-path case 1549 region2->init_req(_fast_path, ctrl); 1550 phi2->init_req(_fast_path, index_update); 1551 1552 ctrl = full; 1553 1554 Node* base = find_bottom_mem(ctrl, phase); 1555 1556 MergeMemNode* mm = MergeMemNode::make(base); 1557 mm->set_memory_at(Compile::AliasIdxRaw, raw_mem); 1558 phase->register_new_node(mm, ctrl); 1559 1560 Node* call = new CallLeafNode(ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type(), CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), "shenandoah_wb_pre", TypeRawPtr::BOTTOM); 1561 call->init_req(TypeFunc::Control, ctrl); 1562 call->init_req(TypeFunc::I_O, phase->C->top()); 1563 call->init_req(TypeFunc::Memory, mm); 1564 call->init_req(TypeFunc::FramePtr, phase->C->top()); 1565 call->init_req(TypeFunc::ReturnAdr, phase->C->top()); 1566 call->init_req(TypeFunc::Parms, pre_val); 1567 call->init_req(TypeFunc::Parms+1, thread); 1568 phase->register_control(call, loop, ctrl); 1569 1570 Node* ctrl_proj = new ProjNode(call, TypeFunc::Control); 1571 phase->register_control(ctrl_proj, loop, call); 1572 Node* mem_proj = new ProjNode(call, TypeFunc::Memory); 1573 phase->register_new_node(mem_proj, call); 1574 1575 // Slow-path case 1576 region2->init_req(_slow_path, ctrl_proj); 1577 phi2->init_req(_slow_path, mem_proj); 1578 1579 phase->register_control(region2, loop, reg2_ctrl); 1580 phase->register_new_node(phi2, region2); 1581 1582 region->init_req(_heap_unstable, region2); 1583 phi->init_req(_heap_unstable, phi2); 1584 1585 phase->register_control(region, loop, heap_stable_ctrl->in(0)); 1586 phase->register_new_node(phi, region); 1587 1588 fix_ctrl(barrier, region, fixer, uses, uses_to_ignore, last, phase); 1589 for(uint next = 0; next < uses.size(); next++ ) { 1590 Node *n = uses.at(next); 1591 assert(phase->get_ctrl(n) == init_ctrl, "bad control"); 1592 assert(n != init_raw_mem, "should leave input raw mem above the barrier"); 1593 phase->set_ctrl(n, region); 1594 follow_barrier_uses(n, init_ctrl, uses, phase); 1595 } 1596 fixer.fix_mem(init_ctrl, region, init_raw_mem, raw_mem_for_ctrl, phi, uses); 1597 1598 phase->igvn().replace_node(barrier, pre_val); 1599 } 1600 assert(state->iu_barriers_count() == 0, "all enqueue barrier nodes should have been replaced"); 1601 1602 } 1603 1604 Node* ShenandoahBarrierC2Support::get_load_addr(PhaseIdealLoop* phase, VectorSet& visited, Node* in) { 1605 if (visited.test_set(in->_idx)) { 1606 return nullptr; 1607 } 1608 switch (in->Opcode()) { 1609 case Op_Proj: 1610 return get_load_addr(phase, visited, in->in(0)); 1611 case Op_CastPP: 1612 case Op_CheckCastPP: 1613 case Op_DecodeN: 1614 case Op_EncodeP: 1615 return get_load_addr(phase, visited, in->in(1)); 1616 case Op_LoadN: 1617 case Op_LoadP: 1618 return in->in(MemNode::Address); 1619 case Op_CompareAndExchangeN: 1620 case Op_CompareAndExchangeP: 1621 case Op_GetAndSetN: 1622 case Op_GetAndSetP: 1623 case Op_ShenandoahCompareAndExchangeP: 1624 case Op_ShenandoahCompareAndExchangeN: 1625 // Those instructions would just have stored a different 1626 // value into the field. No use to attempt to fix it at this point. 1627 return phase->igvn().zerocon(T_OBJECT); 1628 case Op_CMoveP: 1629 case Op_CMoveN: { 1630 Node* t = get_load_addr(phase, visited, in->in(CMoveNode::IfTrue)); 1631 Node* f = get_load_addr(phase, visited, in->in(CMoveNode::IfFalse)); 1632 // Handle unambiguous cases: single address reported on both branches. 1633 if (t != nullptr && f == nullptr) return t; 1634 if (t == nullptr && f != nullptr) return f; 1635 if (t != nullptr && t == f) return t; 1636 // Ambiguity. 1637 return phase->igvn().zerocon(T_OBJECT); 1638 } 1639 case Op_Phi: { 1640 Node* addr = nullptr; 1641 for (uint i = 1; i < in->req(); i++) { 1642 Node* addr1 = get_load_addr(phase, visited, in->in(i)); 1643 if (addr == nullptr) { 1644 addr = addr1; 1645 } 1646 if (addr != addr1) { 1647 return phase->igvn().zerocon(T_OBJECT); 1648 } 1649 } 1650 return addr; 1651 } 1652 case Op_ShenandoahLoadReferenceBarrier: 1653 return get_load_addr(phase, visited, in->in(ShenandoahLoadReferenceBarrierNode::ValueIn)); 1654 case Op_ShenandoahIUBarrier: 1655 return get_load_addr(phase, visited, in->in(1)); 1656 case Op_CallDynamicJava: 1657 case Op_CallLeaf: 1658 case Op_CallStaticJava: 1659 case Op_ConN: 1660 case Op_ConP: 1661 case Op_Parm: 1662 case Op_CreateEx: 1663 return phase->igvn().zerocon(T_OBJECT); 1664 default: 1665 #ifdef ASSERT 1666 fatal("Unknown node in get_load_addr: %s", NodeClassNames[in->Opcode()]); 1667 #endif 1668 return phase->igvn().zerocon(T_OBJECT); 1669 } 1670 1671 } 1672 1673 void ShenandoahBarrierC2Support::move_gc_state_test_out_of_loop(IfNode* iff, PhaseIdealLoop* phase) { 1674 IdealLoopTree *loop = phase->get_loop(iff); 1675 Node* loop_head = loop->_head; 1676 Node* entry_c = loop_head->in(LoopNode::EntryControl); 1677 1678 Node* bol = iff->in(1); 1679 Node* cmp = bol->in(1); 1680 Node* andi = cmp->in(1); 1681 Node* load = andi->in(1); 1682 1683 assert(is_gc_state_load(load), "broken"); 1684 if (!phase->is_dominator(load->in(0), entry_c)) { 1685 Node* mem_ctrl = nullptr; 1686 Node* mem = dom_mem(load->in(MemNode::Memory), loop_head, Compile::AliasIdxRaw, mem_ctrl, phase); 1687 load = load->clone(); 1688 load->set_req(MemNode::Memory, mem); 1689 load->set_req(0, entry_c); 1690 phase->register_new_node(load, entry_c); 1691 andi = andi->clone(); 1692 andi->set_req(1, load); 1693 phase->register_new_node(andi, entry_c); 1694 cmp = cmp->clone(); 1695 cmp->set_req(1, andi); 1696 phase->register_new_node(cmp, entry_c); 1697 bol = bol->clone(); 1698 bol->set_req(1, cmp); 1699 phase->register_new_node(bol, entry_c); 1700 1701 phase->igvn().replace_input_of(iff, 1, bol); 1702 } 1703 } 1704 1705 bool ShenandoahBarrierC2Support::identical_backtoback_ifs(Node* n, PhaseIdealLoop* phase) { 1706 if (!n->is_If() || n->is_CountedLoopEnd()) { 1707 return false; 1708 } 1709 Node* region = n->in(0); 1710 1711 if (!region->is_Region()) { 1712 return false; 1713 } 1714 Node* dom = phase->idom(region); 1715 if (!dom->is_If()) { 1716 return false; 1717 } 1718 1719 if (!is_heap_stable_test(n) || !is_heap_stable_test(dom)) { 1720 return false; 1721 } 1722 1723 IfNode* dom_if = dom->as_If(); 1724 Node* proj_true = dom_if->proj_out(1); 1725 Node* proj_false = dom_if->proj_out(0); 1726 1727 for (uint i = 1; i < region->req(); i++) { 1728 if (phase->is_dominator(proj_true, region->in(i))) { 1729 continue; 1730 } 1731 if (phase->is_dominator(proj_false, region->in(i))) { 1732 continue; 1733 } 1734 return false; 1735 } 1736 1737 return true; 1738 } 1739 1740 bool ShenandoahBarrierC2Support::merge_point_safe(Node* region) { 1741 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { 1742 Node* n = region->fast_out(i); 1743 if (n->is_LoadStore()) { 1744 // Splitting a LoadStore node through phi, causes it to lose its SCMemProj: the split if code doesn't have support 1745 // for a LoadStore at the region the if is split through because that's not expected to happen (LoadStore nodes 1746 // should be between barrier nodes). It does however happen with Shenandoah though because barriers can get 1747 // expanded around a LoadStore node. 1748 return false; 1749 } 1750 } 1751 return true; 1752 } 1753 1754 1755 void ShenandoahBarrierC2Support::merge_back_to_back_tests(Node* n, PhaseIdealLoop* phase) { 1756 assert(is_heap_stable_test(n), "no other tests"); 1757 if (identical_backtoback_ifs(n, phase)) { 1758 Node* n_ctrl = n->in(0); 1759 if (phase->can_split_if(n_ctrl) && merge_point_safe(n_ctrl)) { 1760 IfNode* dom_if = phase->idom(n_ctrl)->as_If(); 1761 if (is_heap_stable_test(n)) { 1762 Node* gc_state_load = n->in(1)->in(1)->in(1)->in(1); 1763 assert(is_gc_state_load(gc_state_load), "broken"); 1764 Node* dom_gc_state_load = dom_if->in(1)->in(1)->in(1)->in(1); 1765 assert(is_gc_state_load(dom_gc_state_load), "broken"); 1766 if (gc_state_load != dom_gc_state_load) { 1767 phase->igvn().replace_node(gc_state_load, dom_gc_state_load); 1768 } 1769 } 1770 PhiNode* bolphi = PhiNode::make_blank(n_ctrl, n->in(1)); 1771 Node* proj_true = dom_if->proj_out(1); 1772 Node* proj_false = dom_if->proj_out(0); 1773 Node* con_true = phase->igvn().makecon(TypeInt::ONE); 1774 Node* con_false = phase->igvn().makecon(TypeInt::ZERO); 1775 1776 for (uint i = 1; i < n_ctrl->req(); i++) { 1777 if (phase->is_dominator(proj_true, n_ctrl->in(i))) { 1778 bolphi->init_req(i, con_true); 1779 } else { 1780 assert(phase->is_dominator(proj_false, n_ctrl->in(i)), "bad if"); 1781 bolphi->init_req(i, con_false); 1782 } 1783 } 1784 phase->register_new_node(bolphi, n_ctrl); 1785 phase->igvn().replace_input_of(n, 1, bolphi); 1786 phase->do_split_if(n); 1787 } 1788 } 1789 } 1790 1791 IfNode* ShenandoahBarrierC2Support::find_unswitching_candidate(const IdealLoopTree* loop, PhaseIdealLoop* phase) { 1792 // Find first invariant test that doesn't exit the loop 1793 LoopNode *head = loop->_head->as_Loop(); 1794 IfNode* unswitch_iff = nullptr; 1795 Node* n = head->in(LoopNode::LoopBackControl); 1796 int loop_has_sfpts = -1; 1797 while (n != head) { 1798 Node* n_dom = phase->idom(n); 1799 if (n->is_Region()) { 1800 if (n_dom->is_If()) { 1801 IfNode* iff = n_dom->as_If(); 1802 if (iff->in(1)->is_Bool()) { 1803 BoolNode* bol = iff->in(1)->as_Bool(); 1804 if (bol->in(1)->is_Cmp()) { 1805 // If condition is invariant and not a loop exit, 1806 // then found reason to unswitch. 1807 if (is_heap_stable_test(iff) && 1808 (loop_has_sfpts == -1 || loop_has_sfpts == 0)) { 1809 assert(!loop->is_loop_exit(iff), "both branches should be in the loop"); 1810 if (loop_has_sfpts == -1) { 1811 for(uint i = 0; i < loop->_body.size(); i++) { 1812 Node *m = loop->_body[i]; 1813 if (m->is_SafePoint() && !m->is_CallLeaf()) { 1814 loop_has_sfpts = 1; 1815 break; 1816 } 1817 } 1818 if (loop_has_sfpts == -1) { 1819 loop_has_sfpts = 0; 1820 } 1821 } 1822 if (!loop_has_sfpts) { 1823 unswitch_iff = iff; 1824 } 1825 } 1826 } 1827 } 1828 } 1829 } 1830 n = n_dom; 1831 } 1832 return unswitch_iff; 1833 } 1834 1835 1836 void ShenandoahBarrierC2Support::optimize_after_expansion(VectorSet &visited, Node_Stack &stack, Node_List &old_new, PhaseIdealLoop* phase) { 1837 Node_List heap_stable_tests; 1838 stack.push(phase->C->start(), 0); 1839 do { 1840 Node* n = stack.node(); 1841 uint i = stack.index(); 1842 1843 if (i < n->outcnt()) { 1844 Node* u = n->raw_out(i); 1845 stack.set_index(i+1); 1846 if (!visited.test_set(u->_idx)) { 1847 stack.push(u, 0); 1848 } 1849 } else { 1850 stack.pop(); 1851 if (n->is_If() && is_heap_stable_test(n)) { 1852 heap_stable_tests.push(n); 1853 } 1854 } 1855 } while (stack.size() > 0); 1856 1857 for (uint i = 0; i < heap_stable_tests.size(); i++) { 1858 Node* n = heap_stable_tests.at(i); 1859 assert(is_heap_stable_test(n), "only evacuation test"); 1860 merge_back_to_back_tests(n, phase); 1861 } 1862 1863 if (!phase->C->major_progress()) { 1864 VectorSet seen; 1865 for (uint i = 0; i < heap_stable_tests.size(); i++) { 1866 Node* n = heap_stable_tests.at(i); 1867 IdealLoopTree* loop = phase->get_loop(n); 1868 if (loop != phase->ltree_root() && 1869 loop->_child == nullptr && 1870 !loop->_irreducible) { 1871 Node* head = loop->_head; 1872 if (head->is_Loop() && 1873 (!head->is_CountedLoop() || head->as_CountedLoop()->is_main_loop() || head->as_CountedLoop()->is_normal_loop()) && 1874 !seen.test_set(head->_idx)) { 1875 IfNode* iff = find_unswitching_candidate(loop, phase); 1876 if (iff != nullptr) { 1877 Node* bol = iff->in(1); 1878 if (head->as_Loop()->is_strip_mined()) { 1879 head->as_Loop()->verify_strip_mined(0); 1880 } 1881 move_gc_state_test_out_of_loop(iff, phase); 1882 1883 AutoNodeBudget node_budget(phase); 1884 1885 if (loop->policy_unswitching(phase)) { 1886 if (head->as_Loop()->is_strip_mined()) { 1887 OuterStripMinedLoopNode* outer = head->as_CountedLoop()->outer_loop(); 1888 hide_strip_mined_loop(outer, head->as_CountedLoop(), phase); 1889 } 1890 phase->do_unswitching(loop, old_new); 1891 } else { 1892 // Not proceeding with unswitching. Move load back in 1893 // the loop. 1894 phase->igvn().replace_input_of(iff, 1, bol); 1895 } 1896 } 1897 } 1898 } 1899 } 1900 } 1901 } 1902 1903 ShenandoahIUBarrierNode::ShenandoahIUBarrierNode(Node* val) : Node(nullptr, val) { 1904 ShenandoahBarrierSetC2::bsc2()->state()->add_iu_barrier(this); 1905 } 1906 1907 const Type* ShenandoahIUBarrierNode::bottom_type() const { 1908 if (in(1) == nullptr || in(1)->is_top()) { 1909 return Type::TOP; 1910 } 1911 const Type* t = in(1)->bottom_type(); 1912 if (t == TypePtr::NULL_PTR) { 1913 return t; 1914 } 1915 return t->is_oopptr(); 1916 } 1917 1918 const Type* ShenandoahIUBarrierNode::Value(PhaseGVN* phase) const { 1919 if (in(1) == nullptr) { 1920 return Type::TOP; 1921 } 1922 const Type* t = phase->type(in(1)); 1923 if (t == Type::TOP) { 1924 return Type::TOP; 1925 } 1926 if (t == TypePtr::NULL_PTR) { 1927 return t; 1928 } 1929 return t->is_oopptr(); 1930 } 1931 1932 int ShenandoahIUBarrierNode::needed(Node* n) { 1933 if (n == nullptr || 1934 n->is_Allocate() || 1935 n->Opcode() == Op_ShenandoahIUBarrier || 1936 n->bottom_type() == TypePtr::NULL_PTR || 1937 (n->bottom_type()->make_oopptr() != nullptr && n->bottom_type()->make_oopptr()->const_oop() != nullptr)) { 1938 return NotNeeded; 1939 } 1940 if (n->is_Phi() || 1941 n->is_CMove()) { 1942 return MaybeNeeded; 1943 } 1944 return Needed; 1945 } 1946 1947 Node* ShenandoahIUBarrierNode::next(Node* n) { 1948 for (;;) { 1949 if (n == nullptr) { 1950 return n; 1951 } else if (n->bottom_type() == TypePtr::NULL_PTR) { 1952 return n; 1953 } else if (n->bottom_type()->make_oopptr() != nullptr && n->bottom_type()->make_oopptr()->const_oop() != nullptr) { 1954 return n; 1955 } else if (n->is_ConstraintCast() || 1956 n->Opcode() == Op_DecodeN || 1957 n->Opcode() == Op_EncodeP) { 1958 n = n->in(1); 1959 } else if (n->is_Proj()) { 1960 n = n->in(0); 1961 } else { 1962 return n; 1963 } 1964 } 1965 ShouldNotReachHere(); 1966 return nullptr; 1967 } 1968 1969 Node* ShenandoahIUBarrierNode::Identity(PhaseGVN* phase) { 1970 PhaseIterGVN* igvn = phase->is_IterGVN(); 1971 1972 Node* n = next(in(1)); 1973 1974 int cont = needed(n); 1975 1976 if (cont == NotNeeded) { 1977 return in(1); 1978 } else if (cont == MaybeNeeded) { 1979 if (igvn == nullptr) { 1980 phase->record_for_igvn(this); 1981 return this; 1982 } else { 1983 ResourceMark rm; 1984 Unique_Node_List wq; 1985 uint wq_i = 0; 1986 1987 for (;;) { 1988 if (n->is_Phi()) { 1989 for (uint i = 1; i < n->req(); i++) { 1990 Node* m = n->in(i); 1991 if (m != nullptr) { 1992 wq.push(m); 1993 } 1994 } 1995 } else { 1996 assert(n->is_CMove(), "nothing else here"); 1997 Node* m = n->in(CMoveNode::IfFalse); 1998 wq.push(m); 1999 m = n->in(CMoveNode::IfTrue); 2000 wq.push(m); 2001 } 2002 Node* orig_n = nullptr; 2003 do { 2004 if (wq_i >= wq.size()) { 2005 return in(1); 2006 } 2007 n = wq.at(wq_i); 2008 wq_i++; 2009 orig_n = n; 2010 n = next(n); 2011 cont = needed(n); 2012 if (cont == Needed) { 2013 return this; 2014 } 2015 } while (cont != MaybeNeeded || (orig_n != n && wq.member(n))); 2016 } 2017 } 2018 } 2019 2020 return this; 2021 } 2022 2023 #ifdef ASSERT 2024 static bool has_never_branch(Node* root) { 2025 for (uint i = 1; i < root->req(); i++) { 2026 Node* in = root->in(i); 2027 if (in != nullptr && in->Opcode() == Op_Halt && in->in(0)->is_Proj() && in->in(0)->in(0)->is_NeverBranch()) { 2028 return true; 2029 } 2030 } 2031 return false; 2032 } 2033 #endif 2034 2035 void MemoryGraphFixer::collect_memory_nodes() { 2036 Node_Stack stack(0); 2037 VectorSet visited; 2038 Node_List regions; 2039 2040 // Walk the raw memory graph and create a mapping from CFG node to 2041 // memory node. Exclude phis for now. 2042 stack.push(_phase->C->root(), 1); 2043 do { 2044 Node* n = stack.node(); 2045 int opc = n->Opcode(); 2046 uint i = stack.index(); 2047 if (i < n->req()) { 2048 Node* mem = nullptr; 2049 if (opc == Op_Root) { 2050 Node* in = n->in(i); 2051 int in_opc = in->Opcode(); 2052 if (in_opc == Op_Return || in_opc == Op_Rethrow) { 2053 mem = in->in(TypeFunc::Memory); 2054 } else if (in_opc == Op_Halt) { 2055 if (in->in(0)->is_Region()) { 2056 Node* r = in->in(0); 2057 for (uint j = 1; j < r->req(); j++) { 2058 assert(!r->in(j)->is_NeverBranch(), ""); 2059 } 2060 } else { 2061 Node* proj = in->in(0); 2062 assert(proj->is_Proj(), ""); 2063 Node* in = proj->in(0); 2064 assert(in->is_CallStaticJava() || in->is_NeverBranch() || in->Opcode() == Op_Catch || proj->is_IfProj(), ""); 2065 if (in->is_CallStaticJava()) { 2066 mem = in->in(TypeFunc::Memory); 2067 } else if (in->Opcode() == Op_Catch) { 2068 Node* call = in->in(0)->in(0); 2069 assert(call->is_Call(), ""); 2070 mem = call->in(TypeFunc::Memory); 2071 } else if (in->is_NeverBranch()) { 2072 mem = collect_memory_for_infinite_loop(in); 2073 } 2074 } 2075 } else { 2076 #ifdef ASSERT 2077 n->dump(); 2078 in->dump(); 2079 #endif 2080 ShouldNotReachHere(); 2081 } 2082 } else { 2083 assert(n->is_Phi() && n->bottom_type() == Type::MEMORY, ""); 2084 assert(n->adr_type() == TypePtr::BOTTOM || _phase->C->get_alias_index(n->adr_type()) == _alias, ""); 2085 mem = n->in(i); 2086 } 2087 i++; 2088 stack.set_index(i); 2089 if (mem == nullptr) { 2090 continue; 2091 } 2092 for (;;) { 2093 if (visited.test_set(mem->_idx) || mem->is_Start()) { 2094 break; 2095 } 2096 if (mem->is_Phi()) { 2097 stack.push(mem, 2); 2098 mem = mem->in(1); 2099 } else if (mem->is_Proj()) { 2100 stack.push(mem, mem->req()); 2101 mem = mem->in(0); 2102 } else if (mem->is_SafePoint() || mem->is_MemBar()) { 2103 mem = mem->in(TypeFunc::Memory); 2104 } else if (mem->is_MergeMem()) { 2105 MergeMemNode* mm = mem->as_MergeMem(); 2106 mem = mm->memory_at(_alias); 2107 } else if (mem->is_Store() || mem->is_LoadStore() || mem->is_ClearArray()) { 2108 assert(_alias == Compile::AliasIdxRaw, ""); 2109 stack.push(mem, mem->req()); 2110 mem = mem->in(MemNode::Memory); 2111 } else { 2112 #ifdef ASSERT 2113 mem->dump(); 2114 #endif 2115 ShouldNotReachHere(); 2116 } 2117 } 2118 } else { 2119 if (n->is_Phi()) { 2120 // Nothing 2121 } else if (!n->is_Root()) { 2122 Node* c = get_ctrl(n); 2123 _memory_nodes.map(c->_idx, n); 2124 } 2125 stack.pop(); 2126 } 2127 } while(stack.is_nonempty()); 2128 2129 // Iterate over CFG nodes in rpo and propagate memory state to 2130 // compute memory state at regions, creating new phis if needed. 2131 Node_List rpo_list; 2132 visited.clear(); 2133 _phase->rpo(_phase->C->root(), stack, visited, rpo_list); 2134 Node* root = rpo_list.pop(); 2135 assert(root == _phase->C->root(), ""); 2136 2137 const bool trace = false; 2138 #ifdef ASSERT 2139 if (trace) { 2140 for (int i = rpo_list.size() - 1; i >= 0; i--) { 2141 Node* c = rpo_list.at(i); 2142 if (_memory_nodes[c->_idx] != nullptr) { 2143 tty->print("X %d", c->_idx); _memory_nodes[c->_idx]->dump(); 2144 } 2145 } 2146 } 2147 #endif 2148 uint last = _phase->C->unique(); 2149 2150 #ifdef ASSERT 2151 uint16_t max_depth = 0; 2152 for (LoopTreeIterator iter(_phase->ltree_root()); !iter.done(); iter.next()) { 2153 IdealLoopTree* lpt = iter.current(); 2154 max_depth = MAX2(max_depth, lpt->_nest); 2155 } 2156 #endif 2157 2158 bool progress = true; 2159 int iteration = 0; 2160 Node_List dead_phis; 2161 while (progress) { 2162 progress = false; 2163 iteration++; 2164 assert(iteration <= 2+max_depth || _phase->C->has_irreducible_loop() || has_never_branch(_phase->C->root()), ""); 2165 if (trace) { tty->print_cr("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); } 2166 2167 for (int i = rpo_list.size() - 1; i >= 0; i--) { 2168 Node* c = rpo_list.at(i); 2169 2170 Node* prev_mem = _memory_nodes[c->_idx]; 2171 if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) { 2172 Node* prev_region = regions[c->_idx]; 2173 Node* unique = nullptr; 2174 for (uint j = 1; j < c->req() && unique != NodeSentinel; j++) { 2175 Node* m = _memory_nodes[c->in(j)->_idx]; 2176 assert(m != nullptr || (c->is_Loop() && j == LoopNode::LoopBackControl && iteration == 1) || _phase->C->has_irreducible_loop() || has_never_branch(_phase->C->root()), "expect memory state"); 2177 if (m != nullptr) { 2178 if (m == prev_region && ((c->is_Loop() && j == LoopNode::LoopBackControl) || (prev_region->is_Phi() && prev_region->in(0) == c))) { 2179 assert(c->is_Loop() && j == LoopNode::LoopBackControl || _phase->C->has_irreducible_loop() || has_never_branch(_phase->C->root()), ""); 2180 // continue 2181 } else if (unique == nullptr) { 2182 unique = m; 2183 } else if (m == unique) { 2184 // continue 2185 } else { 2186 unique = NodeSentinel; 2187 } 2188 } 2189 } 2190 assert(unique != nullptr, "empty phi???"); 2191 if (unique != NodeSentinel) { 2192 if (prev_region != nullptr && prev_region->is_Phi() && prev_region->in(0) == c) { 2193 dead_phis.push(prev_region); 2194 } 2195 regions.map(c->_idx, unique); 2196 } else { 2197 Node* phi = nullptr; 2198 if (prev_region != nullptr && prev_region->is_Phi() && prev_region->in(0) == c && prev_region->_idx >= last) { 2199 phi = prev_region; 2200 for (uint k = 1; k < c->req(); k++) { 2201 Node* m = _memory_nodes[c->in(k)->_idx]; 2202 assert(m != nullptr, "expect memory state"); 2203 phi->set_req(k, m); 2204 } 2205 } else { 2206 for (DUIterator_Fast jmax, j = c->fast_outs(jmax); j < jmax && phi == nullptr; j++) { 2207 Node* u = c->fast_out(j); 2208 if (u->is_Phi() && u->bottom_type() == Type::MEMORY && 2209 (u->adr_type() == TypePtr::BOTTOM || _phase->C->get_alias_index(u->adr_type()) == _alias)) { 2210 phi = u; 2211 for (uint k = 1; k < c->req() && phi != nullptr; k++) { 2212 Node* m = _memory_nodes[c->in(k)->_idx]; 2213 assert(m != nullptr, "expect memory state"); 2214 if (u->in(k) != m) { 2215 phi = NodeSentinel; 2216 } 2217 } 2218 } 2219 } 2220 if (phi == NodeSentinel) { 2221 phi = new PhiNode(c, Type::MEMORY, _phase->C->get_adr_type(_alias)); 2222 for (uint k = 1; k < c->req(); k++) { 2223 Node* m = _memory_nodes[c->in(k)->_idx]; 2224 assert(m != nullptr, "expect memory state"); 2225 phi->init_req(k, m); 2226 } 2227 } 2228 } 2229 if (phi != nullptr) { 2230 regions.map(c->_idx, phi); 2231 } else { 2232 assert(c->unique_ctrl_out()->Opcode() == Op_Halt, "expected memory state"); 2233 } 2234 } 2235 Node* current_region = regions[c->_idx]; 2236 if (current_region != prev_region) { 2237 progress = true; 2238 if (prev_region == prev_mem) { 2239 _memory_nodes.map(c->_idx, current_region); 2240 } 2241 } 2242 } else if (prev_mem == nullptr || prev_mem->is_Phi() || ctrl_or_self(prev_mem) != c) { 2243 Node* m = _memory_nodes[_phase->idom(c)->_idx]; 2244 assert(m != nullptr || c->Opcode() == Op_Halt, "expect memory state"); 2245 if (m != prev_mem) { 2246 _memory_nodes.map(c->_idx, m); 2247 progress = true; 2248 } 2249 } 2250 #ifdef ASSERT 2251 if (trace) { tty->print("X %d", c->_idx); _memory_nodes[c->_idx]->dump(); } 2252 #endif 2253 } 2254 } 2255 2256 // Replace existing phi with computed memory state for that region 2257 // if different (could be a new phi or a dominating memory node if 2258 // that phi was found to be useless). 2259 while (dead_phis.size() > 0) { 2260 Node* n = dead_phis.pop(); 2261 n->replace_by(_phase->C->top()); 2262 n->destruct(&_phase->igvn()); 2263 } 2264 for (int i = rpo_list.size() - 1; i >= 0; i--) { 2265 Node* c = rpo_list.at(i); 2266 if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) { 2267 Node* n = regions[c->_idx]; 2268 assert(n != nullptr || c->unique_ctrl_out()->Opcode() == Op_Halt, "expected memory state"); 2269 if (n != nullptr && n->is_Phi() && n->_idx >= last && n->in(0) == c) { 2270 _phase->register_new_node(n, c); 2271 } 2272 } 2273 } 2274 for (int i = rpo_list.size() - 1; i >= 0; i--) { 2275 Node* c = rpo_list.at(i); 2276 if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) { 2277 Node* n = regions[c->_idx]; 2278 assert(n != nullptr || c->unique_ctrl_out()->Opcode() == Op_Halt, "expected memory state"); 2279 for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax; i++) { 2280 Node* u = c->fast_out(i); 2281 if (u->is_Phi() && u->bottom_type() == Type::MEMORY && 2282 u != n) { 2283 assert(c->unique_ctrl_out()->Opcode() != Op_Halt, "expected memory state"); 2284 if (u->adr_type() == TypePtr::BOTTOM) { 2285 fix_memory_uses(u, n, n, c); 2286 } else if (_phase->C->get_alias_index(u->adr_type()) == _alias) { 2287 _phase->lazy_replace(u, n); 2288 --i; --imax; 2289 } 2290 } 2291 } 2292 } 2293 } 2294 } 2295 2296 Node* MemoryGraphFixer::collect_memory_for_infinite_loop(const Node* in) { 2297 Node* mem = nullptr; 2298 Node* head = in->in(0); 2299 assert(head->is_Region(), "unexpected infinite loop graph shape"); 2300 2301 Node* phi_mem = nullptr; 2302 for (DUIterator_Fast jmax, j = head->fast_outs(jmax); j < jmax; j++) { 2303 Node* u = head->fast_out(j); 2304 if (u->is_Phi() && u->bottom_type() == Type::MEMORY) { 2305 if (_phase->C->get_alias_index(u->adr_type()) == _alias) { 2306 assert(phi_mem == nullptr || phi_mem->adr_type() == TypePtr::BOTTOM, ""); 2307 phi_mem = u; 2308 } else if (u->adr_type() == TypePtr::BOTTOM) { 2309 assert(phi_mem == nullptr || _phase->C->get_alias_index(phi_mem->adr_type()) == _alias, ""); 2310 if (phi_mem == nullptr) { 2311 phi_mem = u; 2312 } 2313 } 2314 } 2315 } 2316 if (phi_mem == nullptr) { 2317 ResourceMark rm; 2318 Node_Stack stack(0); 2319 stack.push(head, 1); 2320 do { 2321 Node* n = stack.node(); 2322 uint i = stack.index(); 2323 if (i >= n->req()) { 2324 stack.pop(); 2325 } else { 2326 stack.set_index(i + 1); 2327 Node* c = n->in(i); 2328 assert(c != head, "should have found a safepoint on the way"); 2329 if (stack.size() != 1 || _phase->is_dominator(head, c)) { 2330 for (;;) { 2331 if (c->is_Region()) { 2332 stack.push(c, 1); 2333 break; 2334 } else if (c->is_SafePoint() && !c->is_CallLeaf()) { 2335 Node* m = c->in(TypeFunc::Memory); 2336 if (m->is_MergeMem()) { 2337 m = m->as_MergeMem()->memory_at(_alias); 2338 } 2339 assert(mem == nullptr || mem == m, "several memory states"); 2340 mem = m; 2341 break; 2342 } else { 2343 assert(c != c->in(0), ""); 2344 c = c->in(0); 2345 } 2346 } 2347 } 2348 } 2349 } while (stack.size() > 0); 2350 assert(mem != nullptr, "should have found safepoint"); 2351 } else { 2352 mem = phi_mem; 2353 } 2354 return mem; 2355 } 2356 2357 Node* MemoryGraphFixer::get_ctrl(Node* n) const { 2358 Node* c = _phase->get_ctrl(n); 2359 if (n->is_Proj() && n->in(0) != nullptr && n->in(0)->is_Call()) { 2360 assert(c == n->in(0), ""); 2361 CallNode* call = c->as_Call(); 2362 CallProjections projs; 2363 call->extract_projections(&projs, true, false); 2364 if (projs.catchall_memproj != nullptr) { 2365 if (projs.fallthrough_memproj == n) { 2366 c = projs.fallthrough_catchproj; 2367 } else { 2368 assert(projs.catchall_memproj == n, ""); 2369 c = projs.catchall_catchproj; 2370 } 2371 } 2372 } 2373 return c; 2374 } 2375 2376 Node* MemoryGraphFixer::ctrl_or_self(Node* n) const { 2377 if (_phase->has_ctrl(n)) 2378 return get_ctrl(n); 2379 else { 2380 assert (n->is_CFG(), "must be a CFG node"); 2381 return n; 2382 } 2383 } 2384 2385 bool MemoryGraphFixer::mem_is_valid(Node* m, Node* c) const { 2386 return m != nullptr && get_ctrl(m) == c; 2387 } 2388 2389 Node* MemoryGraphFixer::find_mem(Node* ctrl, Node* n) const { 2390 assert(n == nullptr || _phase->ctrl_or_self(n) == ctrl, ""); 2391 assert(!ctrl->is_Call() || ctrl == n, "projection expected"); 2392 #ifdef ASSERT 2393 if ((ctrl->is_Proj() && ctrl->in(0)->is_Call()) || 2394 (ctrl->is_Catch() && ctrl->in(0)->in(0)->is_Call())) { 2395 CallNode* call = ctrl->is_Proj() ? ctrl->in(0)->as_Call() : ctrl->in(0)->in(0)->as_Call(); 2396 int mems = 0; 2397 for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) { 2398 Node* u = call->fast_out(i); 2399 if (u->bottom_type() == Type::MEMORY) { 2400 mems++; 2401 } 2402 } 2403 assert(mems <= 1, "No node right after call if multiple mem projections"); 2404 } 2405 #endif 2406 Node* mem = _memory_nodes[ctrl->_idx]; 2407 Node* c = ctrl; 2408 while (!mem_is_valid(mem, c) && 2409 (!c->is_CatchProj() || mem == nullptr || c->in(0)->in(0)->in(0) != get_ctrl(mem))) { 2410 c = _phase->idom(c); 2411 mem = _memory_nodes[c->_idx]; 2412 } 2413 if (n != nullptr && mem_is_valid(mem, c)) { 2414 while (!ShenandoahBarrierC2Support::is_dominator_same_ctrl(c, mem, n, _phase) && _phase->ctrl_or_self(mem) == ctrl) { 2415 mem = next_mem(mem, _alias); 2416 } 2417 if (mem->is_MergeMem()) { 2418 mem = mem->as_MergeMem()->memory_at(_alias); 2419 } 2420 if (!mem_is_valid(mem, c)) { 2421 do { 2422 c = _phase->idom(c); 2423 mem = _memory_nodes[c->_idx]; 2424 } while (!mem_is_valid(mem, c) && 2425 (!c->is_CatchProj() || mem == nullptr || c->in(0)->in(0)->in(0) != get_ctrl(mem))); 2426 } 2427 } 2428 assert(mem->bottom_type() == Type::MEMORY, ""); 2429 return mem; 2430 } 2431 2432 bool MemoryGraphFixer::has_mem_phi(Node* region) const { 2433 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { 2434 Node* use = region->fast_out(i); 2435 if (use->is_Phi() && use->bottom_type() == Type::MEMORY && 2436 (_phase->C->get_alias_index(use->adr_type()) == _alias)) { 2437 return true; 2438 } 2439 } 2440 return false; 2441 } 2442 2443 void MemoryGraphFixer::fix_mem(Node* ctrl, Node* new_ctrl, Node* mem, Node* mem_for_ctrl, Node* new_mem, Unique_Node_List& uses) { 2444 assert(_phase->ctrl_or_self(new_mem) == new_ctrl, ""); 2445 const bool trace = false; 2446 DEBUG_ONLY(if (trace) { tty->print("ZZZ control is"); ctrl->dump(); }); 2447 DEBUG_ONLY(if (trace) { tty->print("ZZZ mem is"); mem->dump(); }); 2448 GrowableArray<Node*> phis; 2449 if (mem_for_ctrl != mem) { 2450 Node* old = mem_for_ctrl; 2451 Node* prev = nullptr; 2452 while (old != mem) { 2453 prev = old; 2454 if (old->is_Store() || old->is_ClearArray() || old->is_LoadStore()) { 2455 assert(_alias == Compile::AliasIdxRaw, ""); 2456 old = old->in(MemNode::Memory); 2457 } else if (old->Opcode() == Op_SCMemProj) { 2458 assert(_alias == Compile::AliasIdxRaw, ""); 2459 old = old->in(0); 2460 } else { 2461 ShouldNotReachHere(); 2462 } 2463 } 2464 assert(prev != nullptr, ""); 2465 if (new_ctrl != ctrl) { 2466 _memory_nodes.map(ctrl->_idx, mem); 2467 _memory_nodes.map(new_ctrl->_idx, mem_for_ctrl); 2468 } 2469 uint input = (uint)MemNode::Memory; 2470 _phase->igvn().replace_input_of(prev, input, new_mem); 2471 } else { 2472 uses.clear(); 2473 _memory_nodes.map(new_ctrl->_idx, new_mem); 2474 uses.push(new_ctrl); 2475 for(uint next = 0; next < uses.size(); next++ ) { 2476 Node *n = uses.at(next); 2477 assert(n->is_CFG(), ""); 2478 DEBUG_ONLY(if (trace) { tty->print("ZZZ ctrl"); n->dump(); }); 2479 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 2480 Node* u = n->fast_out(i); 2481 if (!u->is_Root() && u->is_CFG() && u != n) { 2482 Node* m = _memory_nodes[u->_idx]; 2483 if (u->is_Region() && (!u->is_OuterStripMinedLoop() || _include_lsm) && 2484 !has_mem_phi(u) && 2485 u->unique_ctrl_out()->Opcode() != Op_Halt) { 2486 DEBUG_ONLY(if (trace) { tty->print("ZZZ region"); u->dump(); }); 2487 DEBUG_ONLY(if (trace && m != nullptr) { tty->print("ZZZ mem"); m->dump(); }); 2488 2489 if (!mem_is_valid(m, u) || !m->is_Phi()) { 2490 bool push = true; 2491 bool create_phi = true; 2492 if (_phase->is_dominator(new_ctrl, u)) { 2493 create_phi = false; 2494 } 2495 if (create_phi) { 2496 Node* phi = new PhiNode(u, Type::MEMORY, _phase->C->get_adr_type(_alias)); 2497 _phase->register_new_node(phi, u); 2498 phis.push(phi); 2499 DEBUG_ONLY(if (trace) { tty->print("ZZZ new phi"); phi->dump(); }); 2500 if (!mem_is_valid(m, u)) { 2501 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting mem"); phi->dump(); }); 2502 _memory_nodes.map(u->_idx, phi); 2503 } else { 2504 DEBUG_ONLY(if (trace) { tty->print("ZZZ NOT setting mem"); m->dump(); }); 2505 for (;;) { 2506 assert(m->is_Mem() || m->is_LoadStore() || m->is_Proj(), ""); 2507 Node* next = nullptr; 2508 if (m->is_Proj()) { 2509 next = m->in(0); 2510 } else { 2511 assert(m->is_Mem() || m->is_LoadStore(), ""); 2512 assert(_alias == Compile::AliasIdxRaw, ""); 2513 next = m->in(MemNode::Memory); 2514 } 2515 if (_phase->get_ctrl(next) != u) { 2516 break; 2517 } 2518 if (next->is_MergeMem()) { 2519 assert(_phase->get_ctrl(next->as_MergeMem()->memory_at(_alias)) != u, ""); 2520 break; 2521 } 2522 if (next->is_Phi()) { 2523 assert(next->adr_type() == TypePtr::BOTTOM && next->in(0) == u, ""); 2524 break; 2525 } 2526 m = next; 2527 } 2528 2529 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting to phi"); m->dump(); }); 2530 assert(m->is_Mem() || m->is_LoadStore(), ""); 2531 uint input = (uint)MemNode::Memory; 2532 _phase->igvn().replace_input_of(m, input, phi); 2533 push = false; 2534 } 2535 } else { 2536 DEBUG_ONLY(if (trace) { tty->print("ZZZ skipping region"); u->dump(); }); 2537 } 2538 if (push) { 2539 uses.push(u); 2540 } 2541 } 2542 } else if (!mem_is_valid(m, u) && 2543 !(u->Opcode() == Op_CProj && u->in(0)->is_NeverBranch() && u->as_Proj()->_con == 1)) { 2544 uses.push(u); 2545 } 2546 } 2547 } 2548 } 2549 for (int i = 0; i < phis.length(); i++) { 2550 Node* n = phis.at(i); 2551 Node* r = n->in(0); 2552 DEBUG_ONLY(if (trace) { tty->print("ZZZ fixing new phi"); n->dump(); }); 2553 for (uint j = 1; j < n->req(); j++) { 2554 Node* m = find_mem(r->in(j), nullptr); 2555 _phase->igvn().replace_input_of(n, j, m); 2556 DEBUG_ONLY(if (trace) { tty->print("ZZZ fixing new phi: %d", j); m->dump(); }); 2557 } 2558 } 2559 } 2560 uint last = _phase->C->unique(); 2561 MergeMemNode* mm = nullptr; 2562 int alias = _alias; 2563 DEBUG_ONLY(if (trace) { tty->print("ZZZ raw mem is"); mem->dump(); }); 2564 // Process loads first to not miss an anti-dependency: if the memory 2565 // edge of a store is updated before a load is processed then an 2566 // anti-dependency may be missed. 2567 for (DUIterator i = mem->outs(); mem->has_out(i); i++) { 2568 Node* u = mem->out(i); 2569 if (u->_idx < last && u->is_Load() && _phase->C->get_alias_index(u->adr_type()) == alias) { 2570 Node* m = find_mem(_phase->get_ctrl(u), u); 2571 if (m != mem) { 2572 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); }); 2573 _phase->igvn().replace_input_of(u, MemNode::Memory, m); 2574 --i; 2575 } 2576 } 2577 } 2578 for (DUIterator i = mem->outs(); mem->has_out(i); i++) { 2579 Node* u = mem->out(i); 2580 if (u->_idx < last) { 2581 if (u->is_Mem()) { 2582 if (_phase->C->get_alias_index(u->adr_type()) == alias) { 2583 Node* m = find_mem(_phase->get_ctrl(u), u); 2584 if (m != mem) { 2585 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); }); 2586 _phase->igvn().replace_input_of(u, MemNode::Memory, m); 2587 --i; 2588 } 2589 } 2590 } else if (u->is_MergeMem()) { 2591 MergeMemNode* u_mm = u->as_MergeMem(); 2592 if (u_mm->memory_at(alias) == mem) { 2593 MergeMemNode* newmm = nullptr; 2594 for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) { 2595 Node* uu = u->fast_out(j); 2596 assert(!uu->is_MergeMem(), "chain of MergeMems?"); 2597 if (uu->is_Phi()) { 2598 assert(uu->adr_type() == TypePtr::BOTTOM, ""); 2599 Node* region = uu->in(0); 2600 int nb = 0; 2601 for (uint k = 1; k < uu->req(); k++) { 2602 if (uu->in(k) == u) { 2603 Node* m = find_mem(region->in(k), nullptr); 2604 if (m != mem) { 2605 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of phi %d", k); uu->dump(); }); 2606 newmm = clone_merge_mem(u, mem, m, _phase->ctrl_or_self(m), i); 2607 if (newmm != u) { 2608 _phase->igvn().replace_input_of(uu, k, newmm); 2609 nb++; 2610 --jmax; 2611 } 2612 } 2613 } 2614 } 2615 if (nb > 0) { 2616 --j; 2617 } 2618 } else { 2619 Node* m = find_mem(_phase->ctrl_or_self(uu), uu); 2620 if (m != mem) { 2621 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); uu->dump(); }); 2622 newmm = clone_merge_mem(u, mem, m, _phase->ctrl_or_self(m), i); 2623 if (newmm != u) { 2624 _phase->igvn().replace_input_of(uu, uu->find_edge(u), newmm); 2625 --j, --jmax; 2626 } 2627 } 2628 } 2629 } 2630 } 2631 } else if (u->is_Phi()) { 2632 assert(u->bottom_type() == Type::MEMORY, "what else?"); 2633 if (_phase->C->get_alias_index(u->adr_type()) == alias || u->adr_type() == TypePtr::BOTTOM) { 2634 Node* region = u->in(0); 2635 bool replaced = false; 2636 for (uint j = 1; j < u->req(); j++) { 2637 if (u->in(j) == mem) { 2638 Node* m = find_mem(region->in(j), nullptr); 2639 Node* nnew = m; 2640 if (m != mem) { 2641 if (u->adr_type() == TypePtr::BOTTOM) { 2642 mm = allocate_merge_mem(mem, m, _phase->ctrl_or_self(m)); 2643 nnew = mm; 2644 } 2645 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of phi %d", j); u->dump(); }); 2646 _phase->igvn().replace_input_of(u, j, nnew); 2647 replaced = true; 2648 } 2649 } 2650 } 2651 if (replaced) { 2652 --i; 2653 } 2654 } 2655 } else if ((u->adr_type() == TypePtr::BOTTOM && u->Opcode() != Op_StrInflatedCopy) || 2656 u->adr_type() == nullptr) { 2657 assert(u->adr_type() != nullptr || 2658 u->Opcode() == Op_Rethrow || 2659 u->Opcode() == Op_Return || 2660 u->Opcode() == Op_SafePoint || 2661 (u->is_CallStaticJava() && u->as_CallStaticJava()->uncommon_trap_request() != 0) || 2662 (u->is_CallStaticJava() && u->as_CallStaticJava()->_entry_point == OptoRuntime::rethrow_stub()) || 2663 u->Opcode() == Op_CallLeaf, ""); 2664 Node* m = find_mem(_phase->ctrl_or_self(u), u); 2665 if (m != mem) { 2666 mm = allocate_merge_mem(mem, m, _phase->get_ctrl(m)); 2667 _phase->igvn().replace_input_of(u, u->find_edge(mem), mm); 2668 --i; 2669 } 2670 } else if (_phase->C->get_alias_index(u->adr_type()) == alias) { 2671 Node* m = find_mem(_phase->ctrl_or_self(u), u); 2672 if (m != mem) { 2673 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); }); 2674 _phase->igvn().replace_input_of(u, u->find_edge(mem), m); 2675 --i; 2676 } 2677 } else if (u->adr_type() != TypePtr::BOTTOM && 2678 _memory_nodes[_phase->ctrl_or_self(u)->_idx] == u) { 2679 Node* m = find_mem(_phase->ctrl_or_self(u), u); 2680 assert(m != mem, ""); 2681 // u is on the wrong slice... 2682 assert(u->is_ClearArray(), ""); 2683 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); }); 2684 _phase->igvn().replace_input_of(u, u->find_edge(mem), m); 2685 --i; 2686 } 2687 } 2688 } 2689 #ifdef ASSERT 2690 assert(new_mem->outcnt() > 0, ""); 2691 for (int i = 0; i < phis.length(); i++) { 2692 Node* n = phis.at(i); 2693 assert(n->outcnt() > 0, "new phi must have uses now"); 2694 } 2695 #endif 2696 } 2697 2698 void MemoryGraphFixer::record_new_ctrl(Node* ctrl, Node* new_ctrl, Node* mem, Node* mem_for_ctrl) { 2699 if (mem_for_ctrl != mem && new_ctrl != ctrl) { 2700 _memory_nodes.map(ctrl->_idx, mem); 2701 _memory_nodes.map(new_ctrl->_idx, mem_for_ctrl); 2702 } 2703 } 2704 2705 MergeMemNode* MemoryGraphFixer::allocate_merge_mem(Node* mem, Node* rep_proj, Node* rep_ctrl) const { 2706 MergeMemNode* mm = MergeMemNode::make(mem); 2707 mm->set_memory_at(_alias, rep_proj); 2708 _phase->register_new_node(mm, rep_ctrl); 2709 return mm; 2710 } 2711 2712 MergeMemNode* MemoryGraphFixer::clone_merge_mem(Node* u, Node* mem, Node* rep_proj, Node* rep_ctrl, DUIterator& i) const { 2713 MergeMemNode* newmm = nullptr; 2714 MergeMemNode* u_mm = u->as_MergeMem(); 2715 Node* c = _phase->get_ctrl(u); 2716 if (_phase->is_dominator(c, rep_ctrl)) { 2717 c = rep_ctrl; 2718 } else { 2719 assert(_phase->is_dominator(rep_ctrl, c), "one must dominate the other"); 2720 } 2721 if (u->outcnt() == 1) { 2722 if (u->req() > (uint)_alias && u->in(_alias) == mem) { 2723 _phase->igvn().replace_input_of(u, _alias, rep_proj); 2724 --i; 2725 } else { 2726 _phase->igvn().rehash_node_delayed(u); 2727 u_mm->set_memory_at(_alias, rep_proj); 2728 } 2729 newmm = u_mm; 2730 _phase->set_ctrl_and_loop(u, c); 2731 } else { 2732 // can't simply clone u and then change one of its input because 2733 // it adds and then removes an edge which messes with the 2734 // DUIterator 2735 newmm = MergeMemNode::make(u_mm->base_memory()); 2736 for (uint j = 0; j < u->req(); j++) { 2737 if (j < newmm->req()) { 2738 if (j == (uint)_alias) { 2739 newmm->set_req(j, rep_proj); 2740 } else if (newmm->in(j) != u->in(j)) { 2741 newmm->set_req(j, u->in(j)); 2742 } 2743 } else if (j == (uint)_alias) { 2744 newmm->add_req(rep_proj); 2745 } else { 2746 newmm->add_req(u->in(j)); 2747 } 2748 } 2749 if ((uint)_alias >= u->req()) { 2750 newmm->set_memory_at(_alias, rep_proj); 2751 } 2752 _phase->register_new_node(newmm, c); 2753 } 2754 return newmm; 2755 } 2756 2757 bool MemoryGraphFixer::should_process_phi(Node* phi) const { 2758 if (phi->adr_type() == TypePtr::BOTTOM) { 2759 Node* region = phi->in(0); 2760 for (DUIterator_Fast jmax, j = region->fast_outs(jmax); j < jmax; j++) { 2761 Node* uu = region->fast_out(j); 2762 if (uu->is_Phi() && uu != phi && uu->bottom_type() == Type::MEMORY && _phase->C->get_alias_index(uu->adr_type()) == _alias) { 2763 return false; 2764 } 2765 } 2766 return true; 2767 } 2768 return _phase->C->get_alias_index(phi->adr_type()) == _alias; 2769 } 2770 2771 void MemoryGraphFixer::fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl) const { 2772 uint last = _phase-> C->unique(); 2773 MergeMemNode* mm = nullptr; 2774 assert(mem->bottom_type() == Type::MEMORY, ""); 2775 for (DUIterator i = mem->outs(); mem->has_out(i); i++) { 2776 Node* u = mem->out(i); 2777 if (u != replacement && u->_idx < last) { 2778 if (u->is_MergeMem()) { 2779 MergeMemNode* u_mm = u->as_MergeMem(); 2780 if (u_mm->memory_at(_alias) == mem) { 2781 MergeMemNode* newmm = nullptr; 2782 for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) { 2783 Node* uu = u->fast_out(j); 2784 assert(!uu->is_MergeMem(), "chain of MergeMems?"); 2785 if (uu->is_Phi()) { 2786 if (should_process_phi(uu)) { 2787 Node* region = uu->in(0); 2788 int nb = 0; 2789 for (uint k = 1; k < uu->req(); k++) { 2790 if (uu->in(k) == u && _phase->is_dominator(rep_ctrl, region->in(k))) { 2791 if (newmm == nullptr) { 2792 newmm = clone_merge_mem(u, mem, rep_proj, rep_ctrl, i); 2793 } 2794 if (newmm != u) { 2795 _phase->igvn().replace_input_of(uu, k, newmm); 2796 nb++; 2797 --jmax; 2798 } 2799 } 2800 } 2801 if (nb > 0) { 2802 --j; 2803 } 2804 } 2805 } else { 2806 if (rep_ctrl != uu && ShenandoahBarrierC2Support::is_dominator(rep_ctrl, _phase->ctrl_or_self(uu), replacement, uu, _phase)) { 2807 if (newmm == nullptr) { 2808 newmm = clone_merge_mem(u, mem, rep_proj, rep_ctrl, i); 2809 } 2810 if (newmm != u) { 2811 _phase->igvn().replace_input_of(uu, uu->find_edge(u), newmm); 2812 --j, --jmax; 2813 } 2814 } 2815 } 2816 } 2817 } 2818 } else if (u->is_Phi()) { 2819 assert(u->bottom_type() == Type::MEMORY, "what else?"); 2820 Node* region = u->in(0); 2821 if (should_process_phi(u)) { 2822 bool replaced = false; 2823 for (uint j = 1; j < u->req(); j++) { 2824 if (u->in(j) == mem && _phase->is_dominator(rep_ctrl, region->in(j))) { 2825 Node* nnew = rep_proj; 2826 if (u->adr_type() == TypePtr::BOTTOM) { 2827 if (mm == nullptr) { 2828 mm = allocate_merge_mem(mem, rep_proj, rep_ctrl); 2829 } 2830 nnew = mm; 2831 } 2832 _phase->igvn().replace_input_of(u, j, nnew); 2833 replaced = true; 2834 } 2835 } 2836 if (replaced) { 2837 --i; 2838 } 2839 2840 } 2841 } else if ((u->adr_type() == TypePtr::BOTTOM && u->Opcode() != Op_StrInflatedCopy) || 2842 u->adr_type() == nullptr) { 2843 assert(u->adr_type() != nullptr || 2844 u->Opcode() == Op_Rethrow || 2845 u->Opcode() == Op_Return || 2846 u->Opcode() == Op_SafePoint || 2847 (u->is_CallStaticJava() && u->as_CallStaticJava()->uncommon_trap_request() != 0) || 2848 (u->is_CallStaticJava() && u->as_CallStaticJava()->_entry_point == OptoRuntime::rethrow_stub()) || 2849 u->Opcode() == Op_CallLeaf, "%s", u->Name()); 2850 if (ShenandoahBarrierC2Support::is_dominator(rep_ctrl, _phase->ctrl_or_self(u), replacement, u, _phase)) { 2851 if (mm == nullptr) { 2852 mm = allocate_merge_mem(mem, rep_proj, rep_ctrl); 2853 } 2854 _phase->igvn().replace_input_of(u, u->find_edge(mem), mm); 2855 --i; 2856 } 2857 } else if (_phase->C->get_alias_index(u->adr_type()) == _alias) { 2858 if (ShenandoahBarrierC2Support::is_dominator(rep_ctrl, _phase->ctrl_or_self(u), replacement, u, _phase)) { 2859 _phase->igvn().replace_input_of(u, u->find_edge(mem), rep_proj); 2860 --i; 2861 } 2862 } 2863 } 2864 } 2865 } 2866 2867 ShenandoahLoadReferenceBarrierNode::ShenandoahLoadReferenceBarrierNode(Node* ctrl, Node* obj, DecoratorSet decorators) 2868 : Node(ctrl, obj), _decorators(decorators) { 2869 ShenandoahBarrierSetC2::bsc2()->state()->add_load_reference_barrier(this); 2870 } 2871 2872 DecoratorSet ShenandoahLoadReferenceBarrierNode::decorators() const { 2873 return _decorators; 2874 } 2875 2876 uint ShenandoahLoadReferenceBarrierNode::size_of() const { 2877 return sizeof(*this); 2878 } 2879 2880 static DecoratorSet mask_decorators(DecoratorSet decorators) { 2881 return decorators & (ON_STRONG_OOP_REF | ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF | ON_UNKNOWN_OOP_REF | IN_NATIVE); 2882 } 2883 2884 uint ShenandoahLoadReferenceBarrierNode::hash() const { 2885 uint hash = Node::hash(); 2886 hash += mask_decorators(_decorators); 2887 return hash; 2888 } 2889 2890 bool ShenandoahLoadReferenceBarrierNode::cmp( const Node &n ) const { 2891 return Node::cmp(n) && n.Opcode() == Op_ShenandoahLoadReferenceBarrier && 2892 mask_decorators(_decorators) == mask_decorators(((const ShenandoahLoadReferenceBarrierNode&)n)._decorators); 2893 } 2894 2895 const Type* ShenandoahLoadReferenceBarrierNode::bottom_type() const { 2896 if (in(ValueIn) == nullptr || in(ValueIn)->is_top()) { 2897 return Type::TOP; 2898 } 2899 const Type* t = in(ValueIn)->bottom_type(); 2900 if (t == TypePtr::NULL_PTR) { 2901 return t; 2902 } 2903 2904 if (ShenandoahBarrierSet::is_strong_access(decorators())) { 2905 return t; 2906 } 2907 2908 return t->meet(TypePtr::NULL_PTR); 2909 } 2910 2911 const Type* ShenandoahLoadReferenceBarrierNode::Value(PhaseGVN* phase) const { 2912 // Either input is TOP ==> the result is TOP 2913 const Type *t2 = phase->type(in(ValueIn)); 2914 if( t2 == Type::TOP ) return Type::TOP; 2915 2916 if (t2 == TypePtr::NULL_PTR) { 2917 return t2; 2918 } 2919 2920 if (ShenandoahBarrierSet::is_strong_access(decorators())) { 2921 return t2; 2922 } 2923 2924 return t2->meet(TypePtr::NULL_PTR); 2925 } 2926 2927 Node* ShenandoahLoadReferenceBarrierNode::Identity(PhaseGVN* phase) { 2928 Node* value = in(ValueIn); 2929 if (!needs_barrier(phase, value)) { 2930 return value; 2931 } 2932 return this; 2933 } 2934 2935 bool ShenandoahLoadReferenceBarrierNode::needs_barrier(PhaseGVN* phase, Node* n) { 2936 Unique_Node_List visited; 2937 return needs_barrier_impl(phase, n, visited); 2938 } 2939 2940 bool ShenandoahLoadReferenceBarrierNode::needs_barrier_impl(PhaseGVN* phase, Node* n, Unique_Node_List &visited) { 2941 if (n == nullptr) return false; 2942 if (visited.member(n)) { 2943 return false; // Been there. 2944 } 2945 visited.push(n); 2946 2947 if (n->is_Allocate()) { 2948 // tty->print_cr("optimize barrier on alloc"); 2949 return false; 2950 } 2951 if (n->is_Call()) { 2952 // tty->print_cr("optimize barrier on call"); 2953 return false; 2954 } 2955 2956 const Type* type = phase->type(n); 2957 if (type == Type::TOP) { 2958 return false; 2959 } 2960 if (type->make_ptr()->higher_equal(TypePtr::NULL_PTR)) { 2961 // tty->print_cr("optimize barrier on null"); 2962 return false; 2963 } 2964 if (type->make_oopptr() && type->make_oopptr()->const_oop() != nullptr) { 2965 // tty->print_cr("optimize barrier on constant"); 2966 return false; 2967 } 2968 2969 switch (n->Opcode()) { 2970 case Op_AddP: 2971 return true; // TODO: Can refine? 2972 case Op_LoadP: 2973 case Op_ShenandoahCompareAndExchangeN: 2974 case Op_ShenandoahCompareAndExchangeP: 2975 case Op_CompareAndExchangeN: 2976 case Op_CompareAndExchangeP: 2977 case Op_GetAndSetN: 2978 case Op_GetAndSetP: 2979 return true; 2980 case Op_Phi: { 2981 for (uint i = 1; i < n->req(); i++) { 2982 if (needs_barrier_impl(phase, n->in(i), visited)) return true; 2983 } 2984 return false; 2985 } 2986 case Op_CheckCastPP: 2987 case Op_CastPP: 2988 return needs_barrier_impl(phase, n->in(1), visited); 2989 case Op_Proj: 2990 return needs_barrier_impl(phase, n->in(0), visited); 2991 case Op_ShenandoahLoadReferenceBarrier: 2992 // tty->print_cr("optimize barrier on barrier"); 2993 return false; 2994 case Op_Parm: 2995 // tty->print_cr("optimize barrier on input arg"); 2996 return false; 2997 case Op_DecodeN: 2998 case Op_EncodeP: 2999 return needs_barrier_impl(phase, n->in(1), visited); 3000 case Op_LoadN: 3001 return true; 3002 case Op_CMoveN: 3003 case Op_CMoveP: 3004 return needs_barrier_impl(phase, n->in(2), visited) || 3005 needs_barrier_impl(phase, n->in(3), visited); 3006 case Op_ShenandoahIUBarrier: 3007 return needs_barrier_impl(phase, n->in(1), visited); 3008 case Op_CreateEx: 3009 return false; 3010 default: 3011 break; 3012 } 3013 #ifdef ASSERT 3014 tty->print("need barrier on?: "); 3015 tty->print_cr("ins:"); 3016 n->dump(2); 3017 tty->print_cr("outs:"); 3018 n->dump(-2); 3019 ShouldNotReachHere(); 3020 #endif 3021 return true; 3022 }