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_anti_dependent_load_at_control(PhaseIdealLoop* phase, Node* maybe_load, Node* store, 629 Node* control) { 630 return maybe_load->is_Load() && phase->C->can_alias(store->adr_type(), phase->C->get_alias_index(maybe_load->adr_type())) && 631 phase->ctrl_or_self(maybe_load) == control; 632 } 633 634 void ShenandoahBarrierC2Support::maybe_push_anti_dependent_loads(PhaseIdealLoop* phase, Node* maybe_store, Node* control, Unique_Node_List &wq) { 635 if (!maybe_store->is_Store() && !maybe_store->is_LoadStore()) { 636 return; 637 } 638 Node* mem = maybe_store->in(MemNode::Memory); 639 for (DUIterator_Fast imax, i = mem->fast_outs(imax); i < imax; i++) { 640 Node* u = mem->fast_out(i); 641 if (is_anti_dependent_load_at_control(phase, u, maybe_store, control)) { 642 wq.push(u); 643 } 644 } 645 } 646 647 void ShenandoahBarrierC2Support::push_data_inputs_at_control(PhaseIdealLoop* phase, Node* n, Node* ctrl, Unique_Node_List &wq) { 648 for (uint i = 0; i < n->req(); i++) { 649 Node* in = n->in(i); 650 if (in != nullptr && phase->has_ctrl(in) && phase->get_ctrl(in) == ctrl) { 651 wq.push(in); 652 } 653 } 654 } 655 656 bool ShenandoahBarrierC2Support::is_dominator_same_ctrl(Node* c, Node* d, Node* n, PhaseIdealLoop* phase) { 657 // That both nodes have the same control is not sufficient to prove 658 // domination, verify that there's no path from d to n 659 ResourceMark rm; 660 Unique_Node_List wq; 661 wq.push(d); 662 for (uint next = 0; next < wq.size(); next++) { 663 Node *m = wq.at(next); 664 if (m == n) { 665 return false; 666 } 667 if (m->is_Phi() && m->in(0)->is_Loop()) { 668 assert(phase->ctrl_or_self(m->in(LoopNode::EntryControl)) != c, "following loop entry should lead to new control"); 669 } else { 670 // Take anti-dependencies into account 671 maybe_push_anti_dependent_loads(phase, m, c, wq); 672 push_data_inputs_at_control(phase, m, c, wq); 673 } 674 } 675 return true; 676 } 677 678 bool ShenandoahBarrierC2Support::is_dominator(Node* d_c, Node* n_c, Node* d, Node* n, PhaseIdealLoop* phase) { 679 if (d_c != n_c) { 680 return phase->is_dominator(d_c, n_c); 681 } 682 return is_dominator_same_ctrl(d_c, d, n, phase); 683 } 684 685 Node* next_mem(Node* mem, int alias) { 686 Node* res = nullptr; 687 if (mem->is_Proj()) { 688 res = mem->in(0); 689 } else if (mem->is_SafePoint() || mem->is_MemBar()) { 690 res = mem->in(TypeFunc::Memory); 691 } else if (mem->is_Phi()) { 692 res = mem->in(1); 693 } else if (mem->is_MergeMem()) { 694 res = mem->as_MergeMem()->memory_at(alias); 695 } else if (mem->is_Store() || mem->is_LoadStore() || mem->is_ClearArray()) { 696 assert(alias == Compile::AliasIdxRaw, "following raw memory can't lead to a barrier"); 697 res = mem->in(MemNode::Memory); 698 } else { 699 #ifdef ASSERT 700 mem->dump(); 701 #endif 702 ShouldNotReachHere(); 703 } 704 return res; 705 } 706 707 Node* ShenandoahBarrierC2Support::no_branches(Node* c, Node* dom, bool allow_one_proj, PhaseIdealLoop* phase) { 708 Node* iffproj = nullptr; 709 while (c != dom) { 710 Node* next = phase->idom(c); 711 assert(next->unique_ctrl_out_or_null() == c || c->is_Proj() || c->is_Region(), "multiple control flow out but no proj or region?"); 712 if (c->is_Region()) { 713 ResourceMark rm; 714 Unique_Node_List wq; 715 wq.push(c); 716 for (uint i = 0; i < wq.size(); i++) { 717 Node *n = wq.at(i); 718 if (n == next) { 719 continue; 720 } 721 if (n->is_Region()) { 722 for (uint j = 1; j < n->req(); j++) { 723 wq.push(n->in(j)); 724 } 725 } else { 726 wq.push(n->in(0)); 727 } 728 } 729 for (uint i = 0; i < wq.size(); i++) { 730 Node *n = wq.at(i); 731 assert(n->is_CFG(), ""); 732 if (n->is_Multi()) { 733 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { 734 Node* u = n->fast_out(j); 735 if (u->is_CFG()) { 736 if (!wq.member(u) && !u->as_Proj()->is_uncommon_trap_proj()) { 737 return NodeSentinel; 738 } 739 } 740 } 741 } 742 } 743 } else if (c->is_Proj()) { 744 if (c->is_IfProj()) { 745 if (c->as_Proj()->is_uncommon_trap_if_pattern() != nullptr) { 746 // continue; 747 } else { 748 if (!allow_one_proj) { 749 return NodeSentinel; 750 } 751 if (iffproj == nullptr) { 752 iffproj = c; 753 } else { 754 return NodeSentinel; 755 } 756 } 757 } else if (c->Opcode() == Op_JumpProj) { 758 return NodeSentinel; // unsupported 759 } else if (c->Opcode() == Op_CatchProj) { 760 return NodeSentinel; // unsupported 761 } else if (c->Opcode() == Op_CProj && next->is_NeverBranch()) { 762 return NodeSentinel; // unsupported 763 } else { 764 assert(next->unique_ctrl_out() == c, "unsupported branch pattern"); 765 } 766 } 767 c = next; 768 } 769 return iffproj; 770 } 771 772 Node* ShenandoahBarrierC2Support::dom_mem(Node* mem, Node* ctrl, int alias, Node*& mem_ctrl, PhaseIdealLoop* phase) { 773 ResourceMark rm; 774 VectorSet wq; 775 wq.set(mem->_idx); 776 mem_ctrl = phase->ctrl_or_self(mem); 777 while (!phase->is_dominator(mem_ctrl, ctrl) || mem_ctrl == ctrl) { 778 mem = next_mem(mem, alias); 779 if (wq.test_set(mem->_idx)) { 780 return nullptr; 781 } 782 mem_ctrl = phase->ctrl_or_self(mem); 783 } 784 if (mem->is_MergeMem()) { 785 mem = mem->as_MergeMem()->memory_at(alias); 786 mem_ctrl = phase->ctrl_or_self(mem); 787 } 788 return mem; 789 } 790 791 Node* ShenandoahBarrierC2Support::find_bottom_mem(Node* ctrl, PhaseIdealLoop* phase) { 792 Node* mem = nullptr; 793 Node* c = ctrl; 794 do { 795 if (c->is_Region()) { 796 for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax && mem == nullptr; i++) { 797 Node* u = c->fast_out(i); 798 if (u->is_Phi() && u->bottom_type() == Type::MEMORY) { 799 if (u->adr_type() == TypePtr::BOTTOM) { 800 mem = u; 801 } 802 } 803 } 804 } else { 805 if (c->is_Call() && c->as_Call()->adr_type() != nullptr) { 806 CallProjections* projs = c->as_Call()->extract_projections(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_root_as_ctrl(gc_state_offset); 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_root_as_ctrl(cset_addr_ptr); 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::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::collect_nodes_above_barrier(Unique_Node_List &nodes_above_barrier, PhaseIdealLoop* phase, Node* ctrl, Node* init_raw_mem) { 1024 nodes_above_barrier.clear(); 1025 if (phase->has_ctrl(init_raw_mem) && phase->get_ctrl(init_raw_mem) == ctrl && !init_raw_mem->is_Phi()) { 1026 nodes_above_barrier.push(init_raw_mem); 1027 } 1028 for (uint next = 0; next < nodes_above_barrier.size(); next++) { 1029 Node* n = nodes_above_barrier.at(next); 1030 // Take anti-dependencies into account 1031 maybe_push_anti_dependent_loads(phase, n, ctrl, nodes_above_barrier); 1032 push_data_inputs_at_control(phase, n, ctrl, nodes_above_barrier); 1033 } 1034 } 1035 1036 void ShenandoahBarrierC2Support::fix_ctrl(Node* barrier, Node* region, const MemoryGraphFixer& fixer, Unique_Node_List& uses, Unique_Node_List& nodes_above_barrier, uint last, PhaseIdealLoop* phase) { 1037 Node* ctrl = phase->get_ctrl(barrier); 1038 Node* init_raw_mem = fixer.find_mem(ctrl, barrier); 1039 1040 // Update the control of all nodes that should be after the 1041 // barrier control flow 1042 uses.clear(); 1043 // Every node that is control dependent on the barrier's input 1044 // control will be after the expanded barrier. The raw memory (if 1045 // its memory is control dependent on the barrier's input control) 1046 // must stay above the barrier. 1047 collect_nodes_above_barrier(nodes_above_barrier, phase, ctrl, init_raw_mem); 1048 for (DUIterator_Fast imax, i = ctrl->fast_outs(imax); i < imax; i++) { 1049 Node* u = ctrl->fast_out(i); 1050 if (u->_idx < last && 1051 u != barrier && 1052 !u->depends_only_on_test() && // preserve dependency on test 1053 !nodes_above_barrier.member(u) && 1054 (u->in(0) != ctrl || (!u->is_Region() && !u->is_Phi())) && 1055 (ctrl->Opcode() != Op_CatchProj || u->Opcode() != Op_CreateEx)) { 1056 Node* old_c = phase->ctrl_or_self(u); 1057 if (old_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 Node_Stack stack(0); 1106 Node_List clones; 1107 for (int i = state->load_reference_barriers_count() - 1; i >= 0; i--) { 1108 ShenandoahLoadReferenceBarrierNode* lrb = state->load_reference_barrier(i); 1109 1110 Node* ctrl = phase->get_ctrl(lrb); 1111 Node* val = lrb->in(ShenandoahLoadReferenceBarrierNode::ValueIn); 1112 1113 CallStaticJavaNode* unc = nullptr; 1114 Node* unc_ctrl = nullptr; 1115 Node* uncasted_val = val; 1116 1117 for (DUIterator_Fast imax, i = lrb->fast_outs(imax); i < imax; i++) { 1118 Node* u = lrb->fast_out(i); 1119 if (u->Opcode() == Op_CastPP && 1120 u->in(0) != nullptr && 1121 phase->is_dominator(u->in(0), ctrl)) { 1122 const Type* u_t = phase->igvn().type(u); 1123 1124 if (u_t->meet(TypePtr::NULL_PTR) != u_t && 1125 u->in(0)->Opcode() == Op_IfTrue && 1126 u->in(0)->as_Proj()->is_uncommon_trap_if_pattern() && 1127 u->in(0)->in(0)->is_If() && 1128 u->in(0)->in(0)->in(1)->Opcode() == Op_Bool && 1129 u->in(0)->in(0)->in(1)->as_Bool()->_test._test == BoolTest::ne && 1130 u->in(0)->in(0)->in(1)->in(1)->Opcode() == Op_CmpP && 1131 u->in(0)->in(0)->in(1)->in(1)->in(1) == val && 1132 u->in(0)->in(0)->in(1)->in(1)->in(2)->bottom_type() == TypePtr::NULL_PTR) { 1133 IdealLoopTree* loop = phase->get_loop(ctrl); 1134 IdealLoopTree* unc_loop = phase->get_loop(u->in(0)); 1135 1136 if (!unc_loop->is_member(loop)) { 1137 continue; 1138 } 1139 1140 Node* branch = no_branches(ctrl, u->in(0), false, phase); 1141 assert(branch == nullptr || branch == NodeSentinel, "was not looking for a branch"); 1142 if (branch == NodeSentinel) { 1143 continue; 1144 } 1145 1146 Node* iff = u->in(0)->in(0); 1147 Node* bol = iff->in(1)->clone(); 1148 Node* cmp = bol->in(1)->clone(); 1149 cmp->set_req(1, lrb); 1150 bol->set_req(1, cmp); 1151 phase->igvn().replace_input_of(iff, 1, bol); 1152 phase->set_ctrl(lrb, iff->in(0)); 1153 phase->register_new_node(cmp, iff->in(0)); 1154 phase->register_new_node(bol, iff->in(0)); 1155 break; 1156 } 1157 } 1158 } 1159 // Load barrier on the control output of a call 1160 if ((ctrl->is_Proj() && ctrl->in(0)->is_CallJava()) || ctrl->is_CallJava()) { 1161 CallJavaNode* call = ctrl->is_Proj() ? ctrl->in(0)->as_CallJava() : ctrl->as_CallJava(); 1162 if (call->entry_point() == OptoRuntime::rethrow_stub()) { 1163 // The rethrow call may have too many projections to be 1164 // properly handled here. Given there's no reason for a 1165 // barrier to depend on the call, move it above the call 1166 stack.push(lrb, 0); 1167 do { 1168 Node* n = stack.node(); 1169 uint idx = stack.index(); 1170 if (idx < n->req()) { 1171 Node* in = n->in(idx); 1172 stack.set_index(idx+1); 1173 if (in != nullptr) { 1174 if (phase->has_ctrl(in)) { 1175 if (phase->is_dominator(call, phase->get_ctrl(in))) { 1176 #ifdef ASSERT 1177 for (uint i = 0; i < stack.size(); i++) { 1178 assert(stack.node_at(i) != in, "node shouldn't have been seen yet"); 1179 } 1180 #endif 1181 stack.push(in, 0); 1182 } 1183 } else { 1184 assert(phase->is_dominator(in, call->in(0)), "no dependency on the call"); 1185 } 1186 } 1187 } else { 1188 phase->set_ctrl(n, call->in(0)); 1189 stack.pop(); 1190 } 1191 } while(stack.size() > 0); 1192 continue; 1193 } 1194 CallProjections* projs = call->extract_projections(false, false); 1195 1196 // If this is a runtime call, it doesn't have an exception handling path 1197 if (projs->fallthrough_catchproj == nullptr) { 1198 assert(call->method() == nullptr, "should be runtime call"); 1199 assert(projs->catchall_catchproj == nullptr, "runtime call should not have catch all projection"); 1200 continue; 1201 } 1202 1203 // Otherwise, clone the barrier so there's one for the fallthrough and one for the exception handling path 1204 #ifdef ASSERT 1205 VectorSet cloned; 1206 #endif 1207 Node* lrb_clone = lrb->clone(); 1208 phase->register_new_node(lrb_clone, projs->catchall_catchproj); 1209 phase->set_ctrl(lrb, projs->fallthrough_catchproj); 1210 1211 stack.push(lrb, 0); 1212 clones.push(lrb_clone); 1213 1214 do { 1215 assert(stack.size() == clones.size(), ""); 1216 Node* n = stack.node(); 1217 #ifdef ASSERT 1218 if (n->is_Load()) { 1219 Node* mem = n->in(MemNode::Memory); 1220 for (DUIterator_Fast jmax, j = mem->fast_outs(jmax); j < jmax; j++) { 1221 Node* u = mem->fast_out(j); 1222 assert(!u->is_Store() || !u->is_LoadStore() || phase->get_ctrl(u) != ctrl, "anti dependent store?"); 1223 } 1224 } 1225 #endif 1226 uint idx = stack.index(); 1227 Node* n_clone = clones.at(clones.size()-1); 1228 if (idx < n->outcnt()) { 1229 Node* u = n->raw_out(idx); 1230 Node* c = phase->ctrl_or_self(u); 1231 if (phase->is_dominator(call, c) && phase->is_dominator(c, projs->fallthrough_proj)) { 1232 stack.set_index(idx+1); 1233 assert(!u->is_CFG(), ""); 1234 stack.push(u, 0); 1235 assert(!cloned.test_set(u->_idx), "only one clone"); 1236 Node* u_clone = u->clone(); 1237 int nb = u_clone->replace_edge(n, n_clone, &phase->igvn()); 1238 assert(nb > 0, "should have replaced some uses"); 1239 phase->register_new_node(u_clone, projs->catchall_catchproj); 1240 clones.push(u_clone); 1241 phase->set_ctrl(u, projs->fallthrough_catchproj); 1242 } else { 1243 bool replaced = false; 1244 if (u->is_Phi()) { 1245 for (uint k = 1; k < u->req(); k++) { 1246 if (u->in(k) == n) { 1247 if (phase->is_dominator(projs->catchall_catchproj, u->in(0)->in(k))) { 1248 phase->igvn().replace_input_of(u, k, n_clone); 1249 replaced = true; 1250 } else if (!phase->is_dominator(projs->fallthrough_catchproj, u->in(0)->in(k))) { 1251 phase->igvn().replace_input_of(u, k, create_phis_on_call_return(ctrl, u->in(0)->in(k), n, n_clone, projs, phase)); 1252 replaced = true; 1253 } 1254 } 1255 } 1256 } else { 1257 if (phase->is_dominator(projs->catchall_catchproj, c)) { 1258 phase->igvn().rehash_node_delayed(u); 1259 int nb = u->replace_edge(n, n_clone, &phase->igvn()); 1260 assert(nb > 0, "should have replaced some uses"); 1261 replaced = true; 1262 } else if (!phase->is_dominator(projs->fallthrough_catchproj, c)) { 1263 if (u->is_If()) { 1264 // Can't break If/Bool/Cmp chain 1265 assert(n->is_Bool(), "unexpected If shape"); 1266 assert(stack.node_at(stack.size()-2)->is_Cmp(), "unexpected If shape"); 1267 assert(n_clone->is_Bool(), "unexpected clone"); 1268 assert(clones.at(clones.size()-2)->is_Cmp(), "unexpected clone"); 1269 Node* bol_clone = n->clone(); 1270 Node* cmp_clone = stack.node_at(stack.size()-2)->clone(); 1271 bol_clone->set_req(1, cmp_clone); 1272 1273 Node* nn = stack.node_at(stack.size()-3); 1274 Node* nn_clone = clones.at(clones.size()-3); 1275 assert(nn->Opcode() == nn_clone->Opcode(), "mismatch"); 1276 1277 int nb = cmp_clone->replace_edge(nn, create_phis_on_call_return(ctrl, c, nn, nn_clone, projs, phase), 1278 &phase->igvn()); 1279 assert(nb > 0, "should have replaced some uses"); 1280 1281 phase->register_new_node(bol_clone, u->in(0)); 1282 phase->register_new_node(cmp_clone, u->in(0)); 1283 1284 phase->igvn().replace_input_of(u, 1, bol_clone); 1285 1286 } else { 1287 phase->igvn().rehash_node_delayed(u); 1288 int nb = u->replace_edge(n, create_phis_on_call_return(ctrl, c, n, n_clone, projs, phase), &phase->igvn()); 1289 assert(nb > 0, "should have replaced some uses"); 1290 } 1291 replaced = true; 1292 } 1293 } 1294 if (!replaced) { 1295 stack.set_index(idx+1); 1296 } 1297 } 1298 } else { 1299 stack.pop(); 1300 clones.pop(); 1301 } 1302 } while (stack.size() > 0); 1303 assert(stack.size() == 0 && clones.size() == 0, ""); 1304 } 1305 } 1306 1307 for (int i = 0; i < state->load_reference_barriers_count(); i++) { 1308 ShenandoahLoadReferenceBarrierNode* lrb = state->load_reference_barrier(i); 1309 Node* ctrl = phase->get_ctrl(lrb); 1310 IdealLoopTree* loop = phase->get_loop(ctrl); 1311 Node* head = loop->head(); 1312 if (head->is_OuterStripMinedLoop()) { 1313 // Expanding a barrier here will break loop strip mining 1314 // verification. Transform the loop so the loop nest doesn't 1315 // appear as strip mined. 1316 OuterStripMinedLoopNode* outer = head->as_OuterStripMinedLoop(); 1317 hide_strip_mined_loop(outer, outer->unique_ctrl_out()->as_CountedLoop(), phase); 1318 } 1319 if (head->is_BaseCountedLoop() && ctrl->is_IfProj() && ctrl->in(0)->is_BaseCountedLoopEnd() && 1320 head->as_BaseCountedLoop()->loopexit() == ctrl->in(0)) { 1321 Node* entry = head->in(LoopNode::EntryControl); 1322 Node* backedge = head->in(LoopNode::LoopBackControl); 1323 Node* new_head = new LoopNode(entry, backedge); 1324 phase->register_control(new_head, phase->get_loop(entry), entry); 1325 phase->lazy_replace(head, new_head); 1326 } 1327 } 1328 1329 // Expand load-reference-barriers 1330 MemoryGraphFixer fixer(Compile::AliasIdxRaw, true, phase); 1331 Unique_Node_List nodes_above_barriers; 1332 for (int i = state->load_reference_barriers_count() - 1; i >= 0; i--) { 1333 ShenandoahLoadReferenceBarrierNode* lrb = state->load_reference_barrier(i); 1334 uint last = phase->C->unique(); 1335 Node* ctrl = phase->get_ctrl(lrb); 1336 Node* val = lrb->in(ShenandoahLoadReferenceBarrierNode::ValueIn); 1337 1338 Node* orig_ctrl = ctrl; 1339 1340 Node* raw_mem = fixer.find_mem(ctrl, lrb); 1341 Node* raw_mem_for_ctrl = fixer.find_mem(ctrl, nullptr); 1342 1343 IdealLoopTree *loop = phase->get_loop(ctrl); 1344 1345 Node* heap_stable_ctrl = nullptr; 1346 Node* null_ctrl = nullptr; 1347 1348 assert(val->bottom_type()->make_oopptr(), "need oop"); 1349 assert(val->bottom_type()->make_oopptr()->const_oop() == nullptr, "expect non-constant"); 1350 1351 enum { _heap_stable = 1, _evac_path, _not_cset, PATH_LIMIT }; 1352 Node* region = new RegionNode(PATH_LIMIT); 1353 Node* val_phi = new PhiNode(region, val->bottom_type()->is_oopptr()); 1354 1355 // Stable path. 1356 int flags = ShenandoahHeap::HAS_FORWARDED; 1357 if (!ShenandoahBarrierSet::is_strong_access(lrb->decorators())) { 1358 flags |= ShenandoahHeap::WEAK_ROOTS; 1359 } 1360 test_gc_state(ctrl, raw_mem, heap_stable_ctrl, phase, flags); 1361 IfNode* heap_stable_iff = heap_stable_ctrl->in(0)->as_If(); 1362 1363 // Heap stable case 1364 region->init_req(_heap_stable, heap_stable_ctrl); 1365 val_phi->init_req(_heap_stable, val); 1366 1367 // Test for in-cset, unless it's a native-LRB. Native LRBs need to return null 1368 // even for non-cset objects to prevent resurrection of such objects. 1369 // Wires !in_cset(obj) to slot 2 of region and phis 1370 Node* not_cset_ctrl = nullptr; 1371 if (ShenandoahBarrierSet::is_strong_access(lrb->decorators())) { 1372 test_in_cset(ctrl, not_cset_ctrl, val, raw_mem, phase); 1373 } 1374 if (not_cset_ctrl != nullptr) { 1375 region->init_req(_not_cset, not_cset_ctrl); 1376 val_phi->init_req(_not_cset, val); 1377 } else { 1378 region->del_req(_not_cset); 1379 val_phi->del_req(_not_cset); 1380 } 1381 1382 // Resolve object when orig-value is in cset. 1383 // Make the unconditional resolve for fwdptr. 1384 1385 // Call lrb-stub and wire up that path in slots 4 1386 Node* result_mem = nullptr; 1387 1388 Node* addr; 1389 { 1390 VectorSet visited; 1391 addr = get_load_addr(phase, visited, lrb); 1392 } 1393 if (addr->Opcode() == Op_AddP) { 1394 Node* orig_base = addr->in(AddPNode::Base); 1395 Node* base = new CheckCastPPNode(ctrl, orig_base, orig_base->bottom_type(), ConstraintCastNode::StrongDependency); 1396 phase->register_new_node(base, ctrl); 1397 if (addr->in(AddPNode::Base) == addr->in((AddPNode::Address))) { 1398 // Field access 1399 addr = addr->clone(); 1400 addr->set_req(AddPNode::Base, base); 1401 addr->set_req(AddPNode::Address, base); 1402 phase->register_new_node(addr, ctrl); 1403 } else { 1404 Node* addr2 = addr->in(AddPNode::Address); 1405 if (addr2->Opcode() == Op_AddP && addr2->in(AddPNode::Base) == addr2->in(AddPNode::Address) && 1406 addr2->in(AddPNode::Base) == orig_base) { 1407 addr2 = addr2->clone(); 1408 addr2->set_req(AddPNode::Base, base); 1409 addr2->set_req(AddPNode::Address, base); 1410 phase->register_new_node(addr2, ctrl); 1411 addr = addr->clone(); 1412 addr->set_req(AddPNode::Base, base); 1413 addr->set_req(AddPNode::Address, addr2); 1414 phase->register_new_node(addr, ctrl); 1415 } 1416 } 1417 } 1418 call_lrb_stub(ctrl, val, addr, lrb->decorators(), phase); 1419 region->init_req(_evac_path, ctrl); 1420 val_phi->init_req(_evac_path, val); 1421 1422 phase->register_control(region, loop, heap_stable_iff); 1423 Node* out_val = val_phi; 1424 phase->register_new_node(val_phi, region); 1425 1426 fix_ctrl(lrb, region, fixer, uses, nodes_above_barriers, last, phase); 1427 1428 ctrl = orig_ctrl; 1429 1430 phase->igvn().replace_node(lrb, out_val); 1431 1432 follow_barrier_uses(out_val, ctrl, uses, phase); 1433 1434 for(uint next = 0; next < uses.size(); next++ ) { 1435 Node *n = uses.at(next); 1436 assert(phase->get_ctrl(n) == ctrl, "bad control"); 1437 assert(n != raw_mem, "should leave input raw mem above the barrier"); 1438 phase->set_ctrl(n, region); 1439 follow_barrier_uses(n, ctrl, uses, phase); 1440 } 1441 fixer.record_new_ctrl(ctrl, region, raw_mem, raw_mem_for_ctrl); 1442 } 1443 // Done expanding load-reference-barriers. 1444 assert(ShenandoahBarrierSetC2::bsc2()->state()->load_reference_barriers_count() == 0, "all load reference barrier nodes should have been replaced"); 1445 } 1446 1447 Node* ShenandoahBarrierC2Support::get_load_addr(PhaseIdealLoop* phase, VectorSet& visited, Node* in) { 1448 if (visited.test_set(in->_idx)) { 1449 return nullptr; 1450 } 1451 switch (in->Opcode()) { 1452 case Op_Proj: 1453 return get_load_addr(phase, visited, in->in(0)); 1454 case Op_CastPP: 1455 case Op_CheckCastPP: 1456 case Op_DecodeN: 1457 case Op_EncodeP: 1458 return get_load_addr(phase, visited, in->in(1)); 1459 case Op_LoadN: 1460 case Op_LoadP: 1461 return in->in(MemNode::Address); 1462 case Op_CompareAndExchangeN: 1463 case Op_CompareAndExchangeP: 1464 case Op_GetAndSetN: 1465 case Op_GetAndSetP: 1466 case Op_ShenandoahCompareAndExchangeP: 1467 case Op_ShenandoahCompareAndExchangeN: 1468 // Those instructions would just have stored a different 1469 // value into the field. No use to attempt to fix it at this point. 1470 return phase->igvn().zerocon(T_OBJECT); 1471 case Op_CMoveP: 1472 case Op_CMoveN: { 1473 Node* t = get_load_addr(phase, visited, in->in(CMoveNode::IfTrue)); 1474 Node* f = get_load_addr(phase, visited, in->in(CMoveNode::IfFalse)); 1475 // Handle unambiguous cases: single address reported on both branches. 1476 if (t != nullptr && f == nullptr) return t; 1477 if (t == nullptr && f != nullptr) return f; 1478 if (t != nullptr && t == f) return t; 1479 // Ambiguity. 1480 return phase->igvn().zerocon(T_OBJECT); 1481 } 1482 case Op_Phi: { 1483 Node* addr = nullptr; 1484 for (uint i = 1; i < in->req(); i++) { 1485 Node* addr1 = get_load_addr(phase, visited, in->in(i)); 1486 if (addr == nullptr) { 1487 addr = addr1; 1488 } 1489 if (addr != addr1) { 1490 return phase->igvn().zerocon(T_OBJECT); 1491 } 1492 } 1493 return addr; 1494 } 1495 case Op_ShenandoahLoadReferenceBarrier: 1496 return get_load_addr(phase, visited, in->in(ShenandoahLoadReferenceBarrierNode::ValueIn)); 1497 case Op_CallDynamicJava: 1498 case Op_CallLeaf: 1499 case Op_CallStaticJava: 1500 case Op_ConN: 1501 case Op_ConP: 1502 case Op_Parm: 1503 case Op_CreateEx: 1504 return phase->igvn().zerocon(T_OBJECT); 1505 default: 1506 #ifdef ASSERT 1507 fatal("Unknown node in get_load_addr: %s", NodeClassNames[in->Opcode()]); 1508 #endif 1509 return phase->igvn().zerocon(T_OBJECT); 1510 } 1511 1512 } 1513 1514 #ifdef ASSERT 1515 static bool has_never_branch(Node* root) { 1516 for (uint i = 1; i < root->req(); i++) { 1517 Node* in = root->in(i); 1518 if (in != nullptr && in->Opcode() == Op_Halt && in->in(0)->is_Proj() && in->in(0)->in(0)->is_NeverBranch()) { 1519 return true; 1520 } 1521 } 1522 return false; 1523 } 1524 #endif 1525 1526 void MemoryGraphFixer::collect_memory_nodes() { 1527 Node_Stack stack(0); 1528 VectorSet visited; 1529 Node_List regions; 1530 1531 // Walk the raw memory graph and create a mapping from CFG node to 1532 // memory node. Exclude phis for now. 1533 stack.push(_phase->C->root(), 1); 1534 do { 1535 Node* n = stack.node(); 1536 int opc = n->Opcode(); 1537 uint i = stack.index(); 1538 if (i < n->req()) { 1539 Node* mem = nullptr; 1540 if (opc == Op_Root) { 1541 Node* in = n->in(i); 1542 int in_opc = in->Opcode(); 1543 if (in_opc == Op_Return || in_opc == Op_Rethrow) { 1544 mem = in->in(TypeFunc::Memory); 1545 } else if (in_opc == Op_Halt) { 1546 if (in->in(0)->is_Region()) { 1547 Node* r = in->in(0); 1548 for (uint j = 1; j < r->req(); j++) { 1549 assert(!r->in(j)->is_NeverBranch(), ""); 1550 } 1551 } else { 1552 Node* proj = in->in(0); 1553 assert(proj->is_Proj(), ""); 1554 Node* in = proj->in(0); 1555 assert(in->is_CallStaticJava() || in->is_NeverBranch() || in->Opcode() == Op_Catch || proj->is_IfProj(), ""); 1556 if (in->is_CallStaticJava()) { 1557 mem = in->in(TypeFunc::Memory); 1558 } else if (in->Opcode() == Op_Catch) { 1559 Node* call = in->in(0)->in(0); 1560 assert(call->is_Call(), ""); 1561 mem = call->in(TypeFunc::Memory); 1562 } else if (in->is_NeverBranch()) { 1563 mem = collect_memory_for_infinite_loop(in); 1564 } 1565 } 1566 } else { 1567 #ifdef ASSERT 1568 n->dump(); 1569 in->dump(); 1570 #endif 1571 ShouldNotReachHere(); 1572 } 1573 } else { 1574 assert(n->is_Phi() && n->bottom_type() == Type::MEMORY, ""); 1575 assert(n->adr_type() == TypePtr::BOTTOM || _phase->C->get_alias_index(n->adr_type()) == _alias, ""); 1576 mem = n->in(i); 1577 } 1578 i++; 1579 stack.set_index(i); 1580 if (mem == nullptr) { 1581 continue; 1582 } 1583 for (;;) { 1584 if (visited.test_set(mem->_idx) || mem->is_Start()) { 1585 break; 1586 } 1587 if (mem->is_Phi()) { 1588 stack.push(mem, 2); 1589 mem = mem->in(1); 1590 } else if (mem->is_Proj()) { 1591 stack.push(mem, mem->req()); 1592 mem = mem->in(0); 1593 } else if (mem->is_SafePoint() || mem->is_MemBar()) { 1594 mem = mem->in(TypeFunc::Memory); 1595 } else if (mem->is_MergeMem()) { 1596 MergeMemNode* mm = mem->as_MergeMem(); 1597 mem = mm->memory_at(_alias); 1598 } else if (mem->is_Store() || mem->is_LoadStore() || mem->is_ClearArray()) { 1599 assert(_alias == Compile::AliasIdxRaw, ""); 1600 stack.push(mem, mem->req()); 1601 mem = mem->in(MemNode::Memory); 1602 } else { 1603 #ifdef ASSERT 1604 mem->dump(); 1605 #endif 1606 ShouldNotReachHere(); 1607 } 1608 } 1609 } else { 1610 if (n->is_Phi()) { 1611 // Nothing 1612 } else if (!n->is_Root()) { 1613 Node* c = get_ctrl(n); 1614 _memory_nodes.map(c->_idx, n); 1615 } 1616 stack.pop(); 1617 } 1618 } while(stack.is_nonempty()); 1619 1620 // Iterate over CFG nodes in rpo and propagate memory state to 1621 // compute memory state at regions, creating new phis if needed. 1622 Node_List rpo_list; 1623 visited.clear(); 1624 _phase->rpo(_phase->C->root(), stack, visited, rpo_list); 1625 Node* root = rpo_list.pop(); 1626 assert(root == _phase->C->root(), ""); 1627 1628 const bool trace = false; 1629 #ifdef ASSERT 1630 if (trace) { 1631 for (int i = rpo_list.size() - 1; i >= 0; i--) { 1632 Node* c = rpo_list.at(i); 1633 if (_memory_nodes[c->_idx] != nullptr) { 1634 tty->print("X %d", c->_idx); _memory_nodes[c->_idx]->dump(); 1635 } 1636 } 1637 } 1638 #endif 1639 uint last = _phase->C->unique(); 1640 1641 #ifdef ASSERT 1642 uint16_t max_depth = 0; 1643 for (LoopTreeIterator iter(_phase->ltree_root()); !iter.done(); iter.next()) { 1644 IdealLoopTree* lpt = iter.current(); 1645 max_depth = MAX2(max_depth, lpt->_nest); 1646 } 1647 #endif 1648 1649 bool progress = true; 1650 int iteration = 0; 1651 Node_List dead_phis; 1652 while (progress) { 1653 progress = false; 1654 iteration++; 1655 assert(iteration <= 2+max_depth || _phase->C->has_irreducible_loop() || has_never_branch(_phase->C->root()), ""); 1656 if (trace) { tty->print_cr("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); } 1657 1658 for (int i = rpo_list.size() - 1; i >= 0; i--) { 1659 Node* c = rpo_list.at(i); 1660 1661 Node* prev_mem = _memory_nodes[c->_idx]; 1662 if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) { 1663 Node* prev_region = regions[c->_idx]; 1664 Node* unique = nullptr; 1665 for (uint j = 1; j < c->req() && unique != NodeSentinel; j++) { 1666 Node* m = _memory_nodes[c->in(j)->_idx]; 1667 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"); 1668 if (m != nullptr) { 1669 if (m == prev_region && ((c->is_Loop() && j == LoopNode::LoopBackControl) || (prev_region->is_Phi() && prev_region->in(0) == c))) { 1670 assert((c->is_Loop() && j == LoopNode::LoopBackControl) || _phase->C->has_irreducible_loop() || has_never_branch(_phase->C->root()), ""); 1671 // continue 1672 } else if (unique == nullptr) { 1673 unique = m; 1674 } else if (m == unique) { 1675 // continue 1676 } else { 1677 unique = NodeSentinel; 1678 } 1679 } 1680 } 1681 assert(unique != nullptr, "empty phi???"); 1682 if (unique != NodeSentinel) { 1683 if (prev_region != nullptr && prev_region->is_Phi() && prev_region->in(0) == c) { 1684 dead_phis.push(prev_region); 1685 } 1686 regions.map(c->_idx, unique); 1687 } else { 1688 Node* phi = nullptr; 1689 if (prev_region != nullptr && prev_region->is_Phi() && prev_region->in(0) == c && prev_region->_idx >= last) { 1690 phi = prev_region; 1691 for (uint k = 1; k < c->req(); k++) { 1692 Node* m = _memory_nodes[c->in(k)->_idx]; 1693 assert(m != nullptr, "expect memory state"); 1694 phi->set_req(k, m); 1695 } 1696 } else { 1697 for (DUIterator_Fast jmax, j = c->fast_outs(jmax); j < jmax && phi == nullptr; j++) { 1698 Node* u = c->fast_out(j); 1699 if (u->is_Phi() && u->bottom_type() == Type::MEMORY && 1700 (u->adr_type() == TypePtr::BOTTOM || _phase->C->get_alias_index(u->adr_type()) == _alias)) { 1701 phi = u; 1702 for (uint k = 1; k < c->req() && phi != nullptr; k++) { 1703 Node* m = _memory_nodes[c->in(k)->_idx]; 1704 assert(m != nullptr, "expect memory state"); 1705 if (u->in(k) != m) { 1706 phi = NodeSentinel; 1707 } 1708 } 1709 } 1710 } 1711 if (phi == NodeSentinel) { 1712 phi = new PhiNode(c, Type::MEMORY, _phase->C->get_adr_type(_alias)); 1713 for (uint k = 1; k < c->req(); k++) { 1714 Node* m = _memory_nodes[c->in(k)->_idx]; 1715 assert(m != nullptr, "expect memory state"); 1716 phi->init_req(k, m); 1717 } 1718 } 1719 } 1720 if (phi != nullptr) { 1721 regions.map(c->_idx, phi); 1722 } else { 1723 assert(c->unique_ctrl_out()->Opcode() == Op_Halt, "expected memory state"); 1724 } 1725 } 1726 Node* current_region = regions[c->_idx]; 1727 if (current_region != prev_region) { 1728 progress = true; 1729 if (prev_region == prev_mem) { 1730 _memory_nodes.map(c->_idx, current_region); 1731 } 1732 } 1733 } else if (prev_mem == nullptr || prev_mem->is_Phi() || ctrl_or_self(prev_mem) != c) { 1734 Node* m = _memory_nodes[_phase->idom(c)->_idx]; 1735 assert(m != nullptr || c->Opcode() == Op_Halt, "expect memory state"); 1736 if (m != prev_mem) { 1737 _memory_nodes.map(c->_idx, m); 1738 progress = true; 1739 } 1740 } 1741 #ifdef ASSERT 1742 if (trace) { tty->print("X %d", c->_idx); _memory_nodes[c->_idx]->dump(); } 1743 #endif 1744 } 1745 } 1746 1747 // Replace existing phi with computed memory state for that region 1748 // if different (could be a new phi or a dominating memory node if 1749 // that phi was found to be useless). 1750 while (dead_phis.size() > 0) { 1751 Node* n = dead_phis.pop(); 1752 n->replace_by(_phase->C->top()); 1753 n->destruct(&_phase->igvn()); 1754 } 1755 for (int i = rpo_list.size() - 1; i >= 0; i--) { 1756 Node* c = rpo_list.at(i); 1757 if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) { 1758 Node* n = regions[c->_idx]; 1759 assert(n != nullptr || c->unique_ctrl_out()->Opcode() == Op_Halt, "expected memory state"); 1760 if (n != nullptr && n->is_Phi() && n->_idx >= last && n->in(0) == c) { 1761 _phase->register_new_node(n, c); 1762 } 1763 } 1764 } 1765 for (int i = rpo_list.size() - 1; i >= 0; i--) { 1766 Node* c = rpo_list.at(i); 1767 if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) { 1768 Node* n = regions[c->_idx]; 1769 assert(n != nullptr || c->unique_ctrl_out()->Opcode() == Op_Halt, "expected memory state"); 1770 for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax; i++) { 1771 Node* u = c->fast_out(i); 1772 if (u->is_Phi() && u->bottom_type() == Type::MEMORY && 1773 u != n) { 1774 assert(c->unique_ctrl_out()->Opcode() != Op_Halt, "expected memory state"); 1775 if (u->adr_type() == TypePtr::BOTTOM) { 1776 fix_memory_uses(u, n, n, c); 1777 } else if (_phase->C->get_alias_index(u->adr_type()) == _alias) { 1778 _phase->lazy_replace(u, n); 1779 --i; --imax; 1780 } 1781 } 1782 } 1783 } 1784 } 1785 } 1786 1787 Node* MemoryGraphFixer::collect_memory_for_infinite_loop(const Node* in) { 1788 Node* mem = nullptr; 1789 Node* head = in->in(0); 1790 assert(head->is_Region(), "unexpected infinite loop graph shape"); 1791 1792 Node* phi_mem = nullptr; 1793 for (DUIterator_Fast jmax, j = head->fast_outs(jmax); j < jmax; j++) { 1794 Node* u = head->fast_out(j); 1795 if (u->is_Phi() && u->bottom_type() == Type::MEMORY) { 1796 if (_phase->C->get_alias_index(u->adr_type()) == _alias) { 1797 assert(phi_mem == nullptr || phi_mem->adr_type() == TypePtr::BOTTOM, ""); 1798 phi_mem = u; 1799 } else if (u->adr_type() == TypePtr::BOTTOM) { 1800 assert(phi_mem == nullptr || _phase->C->get_alias_index(phi_mem->adr_type()) == _alias, ""); 1801 if (phi_mem == nullptr) { 1802 phi_mem = u; 1803 } 1804 } 1805 } 1806 } 1807 if (phi_mem == nullptr) { 1808 ResourceMark rm; 1809 Node_Stack stack(0); 1810 stack.push(head, 1); 1811 do { 1812 Node* n = stack.node(); 1813 uint i = stack.index(); 1814 if (i >= n->req()) { 1815 stack.pop(); 1816 } else { 1817 stack.set_index(i + 1); 1818 Node* c = n->in(i); 1819 assert(c != head, "should have found a safepoint on the way"); 1820 if (stack.size() != 1 || _phase->is_dominator(head, c)) { 1821 for (;;) { 1822 if (c->is_Region()) { 1823 stack.push(c, 1); 1824 break; 1825 } else if (c->is_SafePoint() && !c->is_CallLeaf()) { 1826 Node* m = c->in(TypeFunc::Memory); 1827 if (m->is_MergeMem()) { 1828 m = m->as_MergeMem()->memory_at(_alias); 1829 } 1830 assert(mem == nullptr || mem == m, "several memory states"); 1831 mem = m; 1832 break; 1833 } else { 1834 assert(c != c->in(0), ""); 1835 c = c->in(0); 1836 } 1837 } 1838 } 1839 } 1840 } while (stack.size() > 0); 1841 assert(mem != nullptr, "should have found safepoint"); 1842 } else { 1843 mem = phi_mem; 1844 } 1845 return mem; 1846 } 1847 1848 Node* MemoryGraphFixer::get_ctrl(Node* n) const { 1849 Node* c = _phase->get_ctrl(n); 1850 if (n->is_Proj() && n->in(0) != nullptr && n->in(0)->is_Call()) { 1851 assert(c == n->in(0), ""); 1852 CallNode* call = c->as_Call(); 1853 CallProjections* projs = call->extract_projections(true, false); 1854 if (projs->catchall_memproj != nullptr) { 1855 if (projs->fallthrough_memproj == n) { 1856 c = projs->fallthrough_catchproj; 1857 } else { 1858 assert(projs->catchall_memproj == n, ""); 1859 c = projs->catchall_catchproj; 1860 } 1861 } 1862 } 1863 return c; 1864 } 1865 1866 Node* MemoryGraphFixer::ctrl_or_self(Node* n) const { 1867 if (_phase->has_ctrl(n)) 1868 return get_ctrl(n); 1869 else { 1870 assert (n->is_CFG(), "must be a CFG node"); 1871 return n; 1872 } 1873 } 1874 1875 bool MemoryGraphFixer::mem_is_valid(Node* m, Node* c) const { 1876 return m != nullptr && get_ctrl(m) == c; 1877 } 1878 1879 Node* MemoryGraphFixer::find_mem(Node* ctrl, Node* n) const { 1880 assert(n == nullptr || _phase->ctrl_or_self(n) == ctrl, ""); 1881 assert(!ctrl->is_Call() || ctrl == n, "projection expected"); 1882 #ifdef ASSERT 1883 if ((ctrl->is_Proj() && ctrl->in(0)->is_Call()) || 1884 (ctrl->is_Catch() && ctrl->in(0)->in(0)->is_Call())) { 1885 CallNode* call = ctrl->is_Proj() ? ctrl->in(0)->as_Call() : ctrl->in(0)->in(0)->as_Call(); 1886 int mems = 0; 1887 for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) { 1888 Node* u = call->fast_out(i); 1889 if (u->bottom_type() == Type::MEMORY) { 1890 mems++; 1891 } 1892 } 1893 assert(mems <= 1, "No node right after call if multiple mem projections"); 1894 } 1895 #endif 1896 Node* mem = _memory_nodes[ctrl->_idx]; 1897 Node* c = ctrl; 1898 while (!mem_is_valid(mem, c) && 1899 (!c->is_CatchProj() || mem == nullptr || c->in(0)->in(0)->in(0) != get_ctrl(mem))) { 1900 c = _phase->idom(c); 1901 mem = _memory_nodes[c->_idx]; 1902 } 1903 if (n != nullptr && mem_is_valid(mem, c)) { 1904 while (!ShenandoahBarrierC2Support::is_dominator_same_ctrl(c, mem, n, _phase) && _phase->ctrl_or_self(mem) == ctrl) { 1905 mem = next_mem(mem, _alias); 1906 } 1907 if (mem->is_MergeMem()) { 1908 mem = mem->as_MergeMem()->memory_at(_alias); 1909 } 1910 if (!mem_is_valid(mem, c)) { 1911 do { 1912 c = _phase->idom(c); 1913 mem = _memory_nodes[c->_idx]; 1914 } while (!mem_is_valid(mem, c) && 1915 (!c->is_CatchProj() || mem == nullptr || c->in(0)->in(0)->in(0) != get_ctrl(mem))); 1916 } 1917 } 1918 assert(mem->bottom_type() == Type::MEMORY, ""); 1919 return mem; 1920 } 1921 1922 bool MemoryGraphFixer::has_mem_phi(Node* region) const { 1923 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { 1924 Node* use = region->fast_out(i); 1925 if (use->is_Phi() && use->bottom_type() == Type::MEMORY && 1926 (_phase->C->get_alias_index(use->adr_type()) == _alias)) { 1927 return true; 1928 } 1929 } 1930 return false; 1931 } 1932 1933 void MemoryGraphFixer::fix_mem(Node* ctrl, Node* new_ctrl, Node* mem, Node* mem_for_ctrl, Node* new_mem, Unique_Node_List& uses) { 1934 assert(_phase->ctrl_or_self(new_mem) == new_ctrl, ""); 1935 const bool trace = false; 1936 DEBUG_ONLY(if (trace) { tty->print("ZZZ control is"); ctrl->dump(); }); 1937 DEBUG_ONLY(if (trace) { tty->print("ZZZ mem is"); mem->dump(); }); 1938 GrowableArray<Node*> phis; 1939 if (mem_for_ctrl != mem) { 1940 Node* old = mem_for_ctrl; 1941 Node* prev = nullptr; 1942 while (old != mem) { 1943 prev = old; 1944 if (old->is_Store() || old->is_ClearArray() || old->is_LoadStore()) { 1945 assert(_alias == Compile::AliasIdxRaw, ""); 1946 old = old->in(MemNode::Memory); 1947 } else if (old->Opcode() == Op_SCMemProj) { 1948 assert(_alias == Compile::AliasIdxRaw, ""); 1949 old = old->in(0); 1950 } else { 1951 ShouldNotReachHere(); 1952 } 1953 } 1954 assert(prev != nullptr, ""); 1955 if (new_ctrl != ctrl) { 1956 _memory_nodes.map(ctrl->_idx, mem); 1957 _memory_nodes.map(new_ctrl->_idx, mem_for_ctrl); 1958 } 1959 uint input = (uint)MemNode::Memory; 1960 _phase->igvn().replace_input_of(prev, input, new_mem); 1961 } else { 1962 uses.clear(); 1963 _memory_nodes.map(new_ctrl->_idx, new_mem); 1964 uses.push(new_ctrl); 1965 for(uint next = 0; next < uses.size(); next++ ) { 1966 Node *n = uses.at(next); 1967 assert(n->is_CFG(), ""); 1968 DEBUG_ONLY(if (trace) { tty->print("ZZZ ctrl"); n->dump(); }); 1969 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1970 Node* u = n->fast_out(i); 1971 if (!u->is_Root() && u->is_CFG() && u != n) { 1972 Node* m = _memory_nodes[u->_idx]; 1973 if (u->is_Region() && (!u->is_OuterStripMinedLoop() || _include_lsm) && 1974 !has_mem_phi(u) && 1975 u->unique_ctrl_out()->Opcode() != Op_Halt) { 1976 DEBUG_ONLY(if (trace) { tty->print("ZZZ region"); u->dump(); }); 1977 DEBUG_ONLY(if (trace && m != nullptr) { tty->print("ZZZ mem"); m->dump(); }); 1978 1979 if (!mem_is_valid(m, u) || !m->is_Phi()) { 1980 bool push = true; 1981 bool create_phi = true; 1982 if (_phase->is_dominator(new_ctrl, u)) { 1983 create_phi = false; 1984 } 1985 if (create_phi) { 1986 Node* phi = new PhiNode(u, Type::MEMORY, _phase->C->get_adr_type(_alias)); 1987 _phase->register_new_node(phi, u); 1988 phis.push(phi); 1989 DEBUG_ONLY(if (trace) { tty->print("ZZZ new phi"); phi->dump(); }); 1990 if (!mem_is_valid(m, u)) { 1991 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting mem"); phi->dump(); }); 1992 _memory_nodes.map(u->_idx, phi); 1993 } else { 1994 DEBUG_ONLY(if (trace) { tty->print("ZZZ NOT setting mem"); m->dump(); }); 1995 for (;;) { 1996 assert(m->is_Mem() || m->is_LoadStore() || m->is_Proj(), ""); 1997 Node* next = nullptr; 1998 if (m->is_Proj()) { 1999 next = m->in(0); 2000 } else { 2001 assert(m->is_Mem() || m->is_LoadStore(), ""); 2002 assert(_alias == Compile::AliasIdxRaw, ""); 2003 next = m->in(MemNode::Memory); 2004 } 2005 if (_phase->get_ctrl(next) != u) { 2006 break; 2007 } 2008 if (next->is_MergeMem()) { 2009 assert(_phase->get_ctrl(next->as_MergeMem()->memory_at(_alias)) != u, ""); 2010 break; 2011 } 2012 if (next->is_Phi()) { 2013 assert(next->adr_type() == TypePtr::BOTTOM && next->in(0) == u, ""); 2014 break; 2015 } 2016 m = next; 2017 } 2018 2019 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting to phi"); m->dump(); }); 2020 assert(m->is_Mem() || m->is_LoadStore(), ""); 2021 uint input = (uint)MemNode::Memory; 2022 _phase->igvn().replace_input_of(m, input, phi); 2023 push = false; 2024 } 2025 } else { 2026 DEBUG_ONLY(if (trace) { tty->print("ZZZ skipping region"); u->dump(); }); 2027 } 2028 if (push) { 2029 uses.push(u); 2030 } 2031 } 2032 } else if (!mem_is_valid(m, u) && 2033 !(u->Opcode() == Op_CProj && u->in(0)->is_NeverBranch() && u->as_Proj()->_con == 1)) { 2034 uses.push(u); 2035 } 2036 } 2037 } 2038 } 2039 for (int i = 0; i < phis.length(); i++) { 2040 Node* n = phis.at(i); 2041 Node* r = n->in(0); 2042 DEBUG_ONLY(if (trace) { tty->print("ZZZ fixing new phi"); n->dump(); }); 2043 for (uint j = 1; j < n->req(); j++) { 2044 Node* m = find_mem(r->in(j), nullptr); 2045 _phase->igvn().replace_input_of(n, j, m); 2046 DEBUG_ONLY(if (trace) { tty->print("ZZZ fixing new phi: %d", j); m->dump(); }); 2047 } 2048 } 2049 } 2050 uint last = _phase->C->unique(); 2051 MergeMemNode* mm = nullptr; 2052 int alias = _alias; 2053 DEBUG_ONLY(if (trace) { tty->print("ZZZ raw mem is"); mem->dump(); }); 2054 // Process loads first to not miss an anti-dependency: if the memory 2055 // edge of a store is updated before a load is processed then an 2056 // anti-dependency may be missed. 2057 for (DUIterator i = mem->outs(); mem->has_out(i); i++) { 2058 Node* u = mem->out(i); 2059 if (u->_idx < last && u->is_Load() && _phase->C->get_alias_index(u->adr_type()) == alias) { 2060 Node* m = find_mem(_phase->get_ctrl(u), u); 2061 if (m != mem) { 2062 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); }); 2063 _phase->igvn().replace_input_of(u, MemNode::Memory, m); 2064 --i; 2065 } 2066 } 2067 } 2068 for (DUIterator i = mem->outs(); mem->has_out(i); i++) { 2069 Node* u = mem->out(i); 2070 if (u->_idx < last) { 2071 if (u->is_Mem()) { 2072 if (_phase->C->get_alias_index(u->adr_type()) == alias) { 2073 Node* m = find_mem(_phase->get_ctrl(u), u); 2074 if (m != mem) { 2075 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); }); 2076 _phase->igvn().replace_input_of(u, MemNode::Memory, m); 2077 --i; 2078 } 2079 } 2080 } else if (u->is_MergeMem()) { 2081 MergeMemNode* u_mm = u->as_MergeMem(); 2082 if (u_mm->memory_at(alias) == mem) { 2083 MergeMemNode* newmm = nullptr; 2084 for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) { 2085 Node* uu = u->fast_out(j); 2086 assert(!uu->is_MergeMem(), "chain of MergeMems?"); 2087 if (uu->is_Phi()) { 2088 assert(uu->adr_type() == TypePtr::BOTTOM, ""); 2089 Node* region = uu->in(0); 2090 int nb = 0; 2091 for (uint k = 1; k < uu->req(); k++) { 2092 if (uu->in(k) == u) { 2093 Node* m = find_mem(region->in(k), nullptr); 2094 if (m != mem) { 2095 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of phi %d", k); uu->dump(); }); 2096 newmm = clone_merge_mem(u, mem, m, _phase->ctrl_or_self(m), i); 2097 if (newmm != u) { 2098 _phase->igvn().replace_input_of(uu, k, newmm); 2099 nb++; 2100 --jmax; 2101 } 2102 } 2103 } 2104 } 2105 if (nb > 0) { 2106 --j; 2107 } 2108 } else { 2109 Node* m = find_mem(_phase->ctrl_or_self(uu), uu); 2110 if (m != mem) { 2111 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); uu->dump(); }); 2112 newmm = clone_merge_mem(u, mem, m, _phase->ctrl_or_self(m), i); 2113 if (newmm != u) { 2114 _phase->igvn().replace_input_of(uu, uu->find_edge(u), newmm); 2115 --j, --jmax; 2116 } 2117 } 2118 } 2119 } 2120 } 2121 } else if (u->is_Phi()) { 2122 assert(u->bottom_type() == Type::MEMORY, "what else?"); 2123 if (_phase->C->get_alias_index(u->adr_type()) == alias || u->adr_type() == TypePtr::BOTTOM) { 2124 Node* region = u->in(0); 2125 bool replaced = false; 2126 for (uint j = 1; j < u->req(); j++) { 2127 if (u->in(j) == mem) { 2128 Node* m = find_mem(region->in(j), nullptr); 2129 Node* nnew = m; 2130 if (m != mem) { 2131 if (u->adr_type() == TypePtr::BOTTOM) { 2132 mm = allocate_merge_mem(mem, m, _phase->ctrl_or_self(m)); 2133 nnew = mm; 2134 } 2135 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of phi %d", j); u->dump(); }); 2136 _phase->igvn().replace_input_of(u, j, nnew); 2137 replaced = true; 2138 } 2139 } 2140 } 2141 if (replaced) { 2142 --i; 2143 } 2144 } 2145 } else if ((u->adr_type() == TypePtr::BOTTOM && u->Opcode() != Op_StrInflatedCopy) || 2146 u->adr_type() == nullptr) { 2147 assert(u->adr_type() != nullptr || 2148 u->Opcode() == Op_Rethrow || 2149 u->Opcode() == Op_Return || 2150 u->Opcode() == Op_SafePoint || 2151 (u->is_CallStaticJava() && u->as_CallStaticJava()->uncommon_trap_request() != 0) || 2152 (u->is_CallStaticJava() && u->as_CallStaticJava()->_entry_point == OptoRuntime::rethrow_stub()) || 2153 u->Opcode() == Op_CallLeaf, ""); 2154 Node* m = find_mem(_phase->ctrl_or_self(u), u); 2155 if (m != mem) { 2156 mm = allocate_merge_mem(mem, m, _phase->get_ctrl(m)); 2157 _phase->igvn().replace_input_of(u, u->find_edge(mem), mm); 2158 --i; 2159 } 2160 } else if (_phase->C->get_alias_index(u->adr_type()) == alias) { 2161 Node* m = find_mem(_phase->ctrl_or_self(u), u); 2162 if (m != mem) { 2163 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); }); 2164 _phase->igvn().replace_input_of(u, u->find_edge(mem), m); 2165 --i; 2166 } 2167 } else if (u->adr_type() != TypePtr::BOTTOM && 2168 _memory_nodes[_phase->ctrl_or_self(u)->_idx] == u) { 2169 Node* m = find_mem(_phase->ctrl_or_self(u), u); 2170 assert(m != mem, ""); 2171 // u is on the wrong slice... 2172 assert(u->is_ClearArray(), ""); 2173 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); }); 2174 _phase->igvn().replace_input_of(u, u->find_edge(mem), m); 2175 --i; 2176 } 2177 } 2178 } 2179 #ifdef ASSERT 2180 assert(new_mem->outcnt() > 0, ""); 2181 for (int i = 0; i < phis.length(); i++) { 2182 Node* n = phis.at(i); 2183 assert(n->outcnt() > 0, "new phi must have uses now"); 2184 } 2185 #endif 2186 } 2187 2188 void MemoryGraphFixer::record_new_ctrl(Node* ctrl, Node* new_ctrl, Node* mem, Node* mem_for_ctrl) { 2189 if (mem_for_ctrl != mem && new_ctrl != ctrl) { 2190 _memory_nodes.map(ctrl->_idx, mem); 2191 _memory_nodes.map(new_ctrl->_idx, mem_for_ctrl); 2192 } 2193 } 2194 2195 MergeMemNode* MemoryGraphFixer::allocate_merge_mem(Node* mem, Node* rep_proj, Node* rep_ctrl) const { 2196 MergeMemNode* mm = MergeMemNode::make(mem); 2197 mm->set_memory_at(_alias, rep_proj); 2198 _phase->register_new_node(mm, rep_ctrl); 2199 return mm; 2200 } 2201 2202 MergeMemNode* MemoryGraphFixer::clone_merge_mem(Node* u, Node* mem, Node* rep_proj, Node* rep_ctrl, DUIterator& i) const { 2203 MergeMemNode* newmm = nullptr; 2204 MergeMemNode* u_mm = u->as_MergeMem(); 2205 Node* c = _phase->get_ctrl(u); 2206 if (_phase->is_dominator(c, rep_ctrl)) { 2207 c = rep_ctrl; 2208 } else { 2209 assert(_phase->is_dominator(rep_ctrl, c), "one must dominate the other"); 2210 } 2211 if (u->outcnt() == 1) { 2212 if (u->req() > (uint)_alias && u->in(_alias) == mem) { 2213 _phase->igvn().replace_input_of(u, _alias, rep_proj); 2214 --i; 2215 } else { 2216 _phase->igvn().rehash_node_delayed(u); 2217 u_mm->set_memory_at(_alias, rep_proj); 2218 } 2219 newmm = u_mm; 2220 _phase->set_ctrl_and_loop(u, c); 2221 } else { 2222 // can't simply clone u and then change one of its input because 2223 // it adds and then removes an edge which messes with the 2224 // DUIterator 2225 newmm = MergeMemNode::make(u_mm->base_memory()); 2226 for (uint j = 0; j < u->req(); j++) { 2227 if (j < newmm->req()) { 2228 if (j == (uint)_alias) { 2229 newmm->set_req(j, rep_proj); 2230 } else if (newmm->in(j) != u->in(j)) { 2231 newmm->set_req(j, u->in(j)); 2232 } 2233 } else if (j == (uint)_alias) { 2234 newmm->add_req(rep_proj); 2235 } else { 2236 newmm->add_req(u->in(j)); 2237 } 2238 } 2239 if ((uint)_alias >= u->req()) { 2240 newmm->set_memory_at(_alias, rep_proj); 2241 } 2242 _phase->register_new_node(newmm, c); 2243 } 2244 return newmm; 2245 } 2246 2247 bool MemoryGraphFixer::should_process_phi(Node* phi) const { 2248 if (phi->adr_type() == TypePtr::BOTTOM) { 2249 Node* region = phi->in(0); 2250 for (DUIterator_Fast jmax, j = region->fast_outs(jmax); j < jmax; j++) { 2251 Node* uu = region->fast_out(j); 2252 if (uu->is_Phi() && uu != phi && uu->bottom_type() == Type::MEMORY && _phase->C->get_alias_index(uu->adr_type()) == _alias) { 2253 return false; 2254 } 2255 } 2256 return true; 2257 } 2258 return _phase->C->get_alias_index(phi->adr_type()) == _alias; 2259 } 2260 2261 void MemoryGraphFixer::fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl) const { 2262 uint last = _phase-> C->unique(); 2263 MergeMemNode* mm = nullptr; 2264 assert(mem->bottom_type() == Type::MEMORY, ""); 2265 for (DUIterator i = mem->outs(); mem->has_out(i); i++) { 2266 Node* u = mem->out(i); 2267 if (u != replacement && u->_idx < last) { 2268 if (u->is_MergeMem()) { 2269 MergeMemNode* u_mm = u->as_MergeMem(); 2270 if (u_mm->memory_at(_alias) == mem) { 2271 MergeMemNode* newmm = nullptr; 2272 for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) { 2273 Node* uu = u->fast_out(j); 2274 assert(!uu->is_MergeMem(), "chain of MergeMems?"); 2275 if (uu->is_Phi()) { 2276 if (should_process_phi(uu)) { 2277 Node* region = uu->in(0); 2278 int nb = 0; 2279 for (uint k = 1; k < uu->req(); k++) { 2280 if (uu->in(k) == u && _phase->is_dominator(rep_ctrl, region->in(k))) { 2281 if (newmm == nullptr) { 2282 newmm = clone_merge_mem(u, mem, rep_proj, rep_ctrl, i); 2283 } 2284 if (newmm != u) { 2285 _phase->igvn().replace_input_of(uu, k, newmm); 2286 nb++; 2287 --jmax; 2288 } 2289 } 2290 } 2291 if (nb > 0) { 2292 --j; 2293 } 2294 } 2295 } else { 2296 if (rep_ctrl != uu && ShenandoahBarrierC2Support::is_dominator(rep_ctrl, _phase->ctrl_or_self(uu), replacement, uu, _phase)) { 2297 if (newmm == nullptr) { 2298 newmm = clone_merge_mem(u, mem, rep_proj, rep_ctrl, i); 2299 } 2300 if (newmm != u) { 2301 _phase->igvn().replace_input_of(uu, uu->find_edge(u), newmm); 2302 --j, --jmax; 2303 } 2304 } 2305 } 2306 } 2307 } 2308 } else if (u->is_Phi()) { 2309 assert(u->bottom_type() == Type::MEMORY, "what else?"); 2310 Node* region = u->in(0); 2311 if (should_process_phi(u)) { 2312 bool replaced = false; 2313 for (uint j = 1; j < u->req(); j++) { 2314 if (u->in(j) == mem && _phase->is_dominator(rep_ctrl, region->in(j))) { 2315 Node* nnew = rep_proj; 2316 if (u->adr_type() == TypePtr::BOTTOM) { 2317 if (mm == nullptr) { 2318 mm = allocate_merge_mem(mem, rep_proj, rep_ctrl); 2319 } 2320 nnew = mm; 2321 } 2322 _phase->igvn().replace_input_of(u, j, nnew); 2323 replaced = true; 2324 } 2325 } 2326 if (replaced) { 2327 --i; 2328 } 2329 2330 } 2331 } else if ((u->adr_type() == TypePtr::BOTTOM && u->Opcode() != Op_StrInflatedCopy) || 2332 u->adr_type() == nullptr) { 2333 assert(u->adr_type() != nullptr || 2334 u->Opcode() == Op_Rethrow || 2335 u->Opcode() == Op_Return || 2336 u->Opcode() == Op_SafePoint || 2337 (u->is_CallStaticJava() && u->as_CallStaticJava()->uncommon_trap_request() != 0) || 2338 (u->is_CallStaticJava() && u->as_CallStaticJava()->_entry_point == OptoRuntime::rethrow_stub()) || 2339 u->Opcode() == Op_CallLeaf, "%s", u->Name()); 2340 if (ShenandoahBarrierC2Support::is_dominator(rep_ctrl, _phase->ctrl_or_self(u), replacement, u, _phase)) { 2341 if (mm == nullptr) { 2342 mm = allocate_merge_mem(mem, rep_proj, rep_ctrl); 2343 } 2344 _phase->igvn().replace_input_of(u, u->find_edge(mem), mm); 2345 --i; 2346 } 2347 } else if (_phase->C->get_alias_index(u->adr_type()) == _alias) { 2348 if (ShenandoahBarrierC2Support::is_dominator(rep_ctrl, _phase->ctrl_or_self(u), replacement, u, _phase)) { 2349 _phase->igvn().replace_input_of(u, u->find_edge(mem), rep_proj); 2350 --i; 2351 } 2352 } 2353 } 2354 } 2355 } 2356 2357 ShenandoahLoadReferenceBarrierNode::ShenandoahLoadReferenceBarrierNode(Node* ctrl, Node* obj, DecoratorSet decorators) 2358 : Node(ctrl, obj), _decorators(decorators) { 2359 ShenandoahBarrierSetC2::bsc2()->state()->add_load_reference_barrier(this); 2360 } 2361 2362 DecoratorSet ShenandoahLoadReferenceBarrierNode::decorators() const { 2363 return _decorators; 2364 } 2365 2366 uint ShenandoahLoadReferenceBarrierNode::size_of() const { 2367 return sizeof(*this); 2368 } 2369 2370 static DecoratorSet mask_decorators(DecoratorSet decorators) { 2371 return decorators & (ON_STRONG_OOP_REF | ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF | ON_UNKNOWN_OOP_REF | IN_NATIVE); 2372 } 2373 2374 uint ShenandoahLoadReferenceBarrierNode::hash() const { 2375 uint hash = Node::hash(); 2376 hash += mask_decorators(_decorators); 2377 return hash; 2378 } 2379 2380 bool ShenandoahLoadReferenceBarrierNode::cmp( const Node &n ) const { 2381 return Node::cmp(n) && n.Opcode() == Op_ShenandoahLoadReferenceBarrier && 2382 mask_decorators(_decorators) == mask_decorators(((const ShenandoahLoadReferenceBarrierNode&)n)._decorators); 2383 } 2384 2385 const Type* ShenandoahLoadReferenceBarrierNode::bottom_type() const { 2386 if (in(ValueIn) == nullptr || in(ValueIn)->is_top()) { 2387 return Type::TOP; 2388 } 2389 const Type* t = in(ValueIn)->bottom_type(); 2390 if (t == TypePtr::NULL_PTR) { 2391 return t; 2392 } 2393 2394 if (ShenandoahBarrierSet::is_strong_access(decorators())) { 2395 return t; 2396 } 2397 2398 return t->meet(TypePtr::NULL_PTR); 2399 } 2400 2401 const Type* ShenandoahLoadReferenceBarrierNode::Value(PhaseGVN* phase) const { 2402 // Either input is TOP ==> the result is TOP 2403 const Type *t2 = phase->type(in(ValueIn)); 2404 if( t2 == Type::TOP ) return Type::TOP; 2405 2406 if (t2 == TypePtr::NULL_PTR) { 2407 return t2; 2408 } 2409 2410 if (ShenandoahBarrierSet::is_strong_access(decorators())) { 2411 return t2; 2412 } 2413 2414 return t2->meet(TypePtr::NULL_PTR); 2415 } 2416 2417 Node* ShenandoahLoadReferenceBarrierNode::Identity(PhaseGVN* phase) { 2418 Node* value = in(ValueIn); 2419 if (!needs_barrier(phase, value)) { 2420 return value; 2421 } 2422 return this; 2423 } 2424 2425 bool ShenandoahLoadReferenceBarrierNode::needs_barrier(PhaseGVN* phase, Node* n) { 2426 Unique_Node_List visited; 2427 return needs_barrier_impl(phase, n, visited); 2428 } 2429 2430 bool ShenandoahLoadReferenceBarrierNode::needs_barrier_impl(PhaseGVN* phase, Node* n, Unique_Node_List &visited) { 2431 if (n == nullptr) return false; 2432 if (visited.member(n)) { 2433 return false; // Been there. 2434 } 2435 visited.push(n); 2436 2437 if (n->is_Allocate()) { 2438 // tty->print_cr("optimize barrier on alloc"); 2439 return false; 2440 } 2441 if (n->is_Call()) { 2442 // tty->print_cr("optimize barrier on call"); 2443 return false; 2444 } 2445 2446 const Type* type = phase->type(n); 2447 if (type == Type::TOP) { 2448 return false; 2449 } 2450 if (type->make_ptr()->higher_equal(TypePtr::NULL_PTR)) { 2451 // tty->print_cr("optimize barrier on null"); 2452 return false; 2453 } 2454 if (type->make_oopptr() && type->make_oopptr()->const_oop() != nullptr) { 2455 // tty->print_cr("optimize barrier on constant"); 2456 return false; 2457 } 2458 2459 switch (n->Opcode()) { 2460 case Op_AddP: 2461 return true; // TODO: Can refine? 2462 case Op_LoadP: 2463 case Op_ShenandoahCompareAndExchangeN: 2464 case Op_ShenandoahCompareAndExchangeP: 2465 case Op_CompareAndExchangeN: 2466 case Op_CompareAndExchangeP: 2467 case Op_GetAndSetN: 2468 case Op_GetAndSetP: 2469 return true; 2470 case Op_Phi: { 2471 for (uint i = 1; i < n->req(); i++) { 2472 if (needs_barrier_impl(phase, n->in(i), visited)) return true; 2473 } 2474 return false; 2475 } 2476 case Op_CheckCastPP: 2477 case Op_CastPP: 2478 return needs_barrier_impl(phase, n->in(1), visited); 2479 case Op_Proj: 2480 return needs_barrier_impl(phase, n->in(0), visited); 2481 case Op_ShenandoahLoadReferenceBarrier: 2482 // tty->print_cr("optimize barrier on barrier"); 2483 return false; 2484 case Op_Parm: 2485 // tty->print_cr("optimize barrier on input arg"); 2486 return false; 2487 case Op_DecodeN: 2488 case Op_EncodeP: 2489 return needs_barrier_impl(phase, n->in(1), visited); 2490 case Op_LoadN: 2491 return true; 2492 case Op_CMoveN: 2493 case Op_CMoveP: 2494 return needs_barrier_impl(phase, n->in(2), visited) || 2495 needs_barrier_impl(phase, n->in(3), visited); 2496 case Op_CreateEx: 2497 return false; 2498 default: 2499 break; 2500 } 2501 #ifdef ASSERT 2502 tty->print("need barrier on?: "); 2503 tty->print_cr("ins:"); 2504 n->dump(2); 2505 tty->print_cr("outs:"); 2506 n->dump(-2); 2507 ShouldNotReachHere(); 2508 #endif 2509 return true; 2510 }