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