1 /*
   2  * Copyright (c) 2015, 2026, Red Hat, Inc. All rights reserved.
   3  * Copyright (C) 2022, Tencent. 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 "code/aotCodeCache.hpp"
  29 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
  30 #include "gc/shenandoah/c2/shenandoahSupport.hpp"
  31 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  32 #include "gc/shenandoah/shenandoahForwarding.hpp"
  33 #include "gc/shenandoah/shenandoahHeap.hpp"
  34 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  35 #include "gc/shenandoah/shenandoahRuntime.hpp"
  36 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  37 #include "opto/arraycopynode.hpp"
  38 #include "opto/block.hpp"
  39 #include "opto/callnode.hpp"
  40 #include "opto/castnode.hpp"
  41 #include "opto/movenode.hpp"
  42 #include "opto/phaseX.hpp"
  43 #include "opto/rootnode.hpp"
  44 #include "opto/runtime.hpp"
  45 #include "opto/subnode.hpp"
  46 
  47 bool ShenandoahBarrierC2Support::expand(Compile* C, PhaseIterGVN& igvn) {
  48   ShenandoahBarrierSetC2State* state = ShenandoahBarrierSetC2::bsc2()->state();
  49   if (state->load_reference_barriers_count() > 0) {
  50     assert(C->post_loop_opts_phase(), "no loop opts allowed");
  51     C->reset_post_loop_opts_phase(); // ... but we know what we are doing
  52     C->clear_major_progress();
  53     PhaseIdealLoop::optimize(igvn, LoopOptsShenandoahExpand);
  54     if (C->failing()) return false;
  55     C->process_for_post_loop_opts_igvn(igvn);
  56     if (C->failing()) return false;
  57 
  58     C->set_post_loop_opts_phase(); // now for real!
  59   }
  60   return true;
  61 }
  62 
  63 bool ShenandoahBarrierC2Support::is_gc_state_test(Node* iff, int mask) {
  64   if (!UseShenandoahGC) {
  65     return false;
  66   }
  67   assert(iff->is_If(), "bad input");
  68   if (iff->Opcode() != Op_If) {
  69     return false;
  70   }
  71   Node* bol = iff->in(1);
  72   if (!bol->is_Bool() || bol->as_Bool()->_test._test != BoolTest::ne) {
  73     return false;
  74   }
  75   Node* cmp = bol->in(1);
  76   if (cmp->Opcode() != Op_CmpI) {
  77     return false;
  78   }
  79   Node* in1 = cmp->in(1);
  80   Node* in2 = cmp->in(2);
  81   if (in2->find_int_con(-1) != 0) {
  82     return false;
  83   }
  84   if (in1->Opcode() != Op_AndI) {
  85     return false;
  86   }
  87   in2 = in1->in(2);
  88   if (in2->find_int_con(-1) != mask) {
  89     return false;
  90   }
  91   in1 = in1->in(1);
  92 
  93   return is_gc_state_load(in1);
  94 }
  95 
  96 bool ShenandoahBarrierC2Support::is_heap_stable_test(Node* iff) {
  97   return is_gc_state_test(iff, ShenandoahHeap::HAS_FORWARDED);
  98 }
  99 
 100 bool ShenandoahBarrierC2Support::is_gc_state_load(Node *n) {
 101   if (!UseShenandoahGC) {
 102     return false;
 103   }
 104   if (n->Opcode() != Op_LoadB && n->Opcode() != Op_LoadUB) {
 105     return false;
 106   }
 107   Node* addp = n->in(MemNode::Address);
 108   if (!addp->is_AddP()) {
 109     return false;
 110   }
 111   Node* base = addp->in(AddPNode::Address);
 112   Node* off = addp->in(AddPNode::Offset);
 113   if (base->Opcode() != Op_ThreadLocal) {
 114     return false;
 115   }
 116   if (off->find_intptr_t_con(-1) != in_bytes(ShenandoahThreadLocalData::gc_state_offset())) {
 117     return false;
 118   }
 119   return true;
 120 }
 121 
 122 bool ShenandoahBarrierC2Support::has_safepoint_between(Node* start, Node* stop, PhaseIdealLoop *phase) {
 123   assert(phase->is_dominator(stop, start), "bad inputs");
 124   ResourceMark rm;
 125   Unique_Node_List wq;
 126   wq.push(start);
 127   for (uint next = 0; next < wq.size(); next++) {
 128     Node *m = wq.at(next);
 129     if (m == stop) {
 130       continue;
 131     }
 132     if (m->is_SafePoint() && !m->is_CallLeaf()) {
 133       return true;
 134     }
 135     if (m->is_Region()) {
 136       for (uint i = 1; i < m->req(); i++) {
 137         wq.push(m->in(i));
 138       }
 139     } else {
 140       wq.push(m->in(0));
 141     }
 142   }
 143   return false;
 144 }
 145 
 146 #ifdef ASSERT
 147 bool ShenandoahBarrierC2Support::verify_helper(Node* in, Node_Stack& phis, VectorSet& visited, verify_type t, bool trace, Unique_Node_List& barriers_used) {
 148   assert(phis.size() == 0, "");
 149 
 150   while (true) {
 151     if (in->bottom_type() == TypePtr::NULL_PTR) {
 152       if (trace) {tty->print_cr("null");}
 153     } else if (!in->bottom_type()->make_ptr()->make_oopptr()) {
 154       if (trace) {tty->print_cr("Non oop");}
 155     } else {
 156       if (in->is_ConstraintCast()) {
 157         in = in->in(1);
 158         continue;
 159       } else if (in->is_AddP()) {
 160         assert(!in->in(AddPNode::Address)->is_top(), "no raw memory access");
 161         in = in->in(AddPNode::Address);
 162         continue;
 163       } else if (in->is_Con()) {
 164         if (trace) {
 165           tty->print("Found constant");
 166           in->dump();
 167         }
 168       } else if (in->Opcode() == Op_Parm) {
 169         if (trace) {
 170           tty->print("Found argument");
 171         }
 172       } else if (in->Opcode() == Op_CreateEx) {
 173         if (trace) {
 174           tty->print("Found create-exception");
 175         }
 176       } else if (in->Opcode() == Op_LoadP && in->adr_type() == TypeRawPtr::BOTTOM) {
 177         if (trace) {
 178           tty->print("Found raw LoadP (OSR argument?)");
 179         }
 180       } else if (in->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
 181         if (t == ShenandoahOopStore) {
 182           return false;
 183         }
 184         barriers_used.push(in);
 185         if (trace) {tty->print("Found barrier"); in->dump();}
 186       } else if (in->is_Proj() && in->in(0)->is_Allocate()) {
 187         if (trace) {
 188           tty->print("Found alloc");
 189           in->in(0)->dump();
 190         }
 191       } else if (in->is_Proj() && (in->in(0)->Opcode() == Op_CallStaticJava || in->in(0)->Opcode() == Op_CallDynamicJava)) {
 192         if (trace) {
 193           tty->print("Found Java call");
 194         }
 195       } else if (in->is_Phi()) {
 196         if (!visited.test_set(in->_idx)) {
 197           if (trace) {tty->print("Pushed phi:"); in->dump();}
 198           phis.push(in, 2);
 199           in = in->in(1);
 200           continue;
 201         }
 202         if (trace) {tty->print("Already seen phi:"); in->dump();}
 203       } else if (in->Opcode() == Op_CMoveP || in->Opcode() == Op_CMoveN) {
 204         if (!visited.test_set(in->_idx)) {
 205           if (trace) {tty->print("Pushed cmovep:"); in->dump();}
 206           phis.push(in, CMoveNode::IfTrue);
 207           in = in->in(CMoveNode::IfFalse);
 208           continue;
 209         }
 210         if (trace) {tty->print("Already seen cmovep:"); in->dump();}
 211       } else if (in->Opcode() == Op_EncodeP || in->Opcode() == Op_DecodeN) {
 212         in = in->in(1);
 213         continue;
 214       } else {
 215         return false;
 216       }
 217     }
 218     bool cont = false;
 219     while (phis.is_nonempty()) {
 220       uint idx = phis.index();
 221       Node* phi = phis.node();
 222       if (idx >= phi->req()) {
 223         if (trace) {tty->print("Popped phi:"); phi->dump();}
 224         phis.pop();
 225         continue;
 226       }
 227       if (trace) {tty->print("Next entry(%d) for phi:", idx); phi->dump();}
 228       in = phi->in(idx);
 229       phis.set_index(idx+1);
 230       cont = true;
 231       break;
 232     }
 233     if (!cont) {
 234       break;
 235     }
 236   }
 237   return true;
 238 }
 239 
 240 void ShenandoahBarrierC2Support::report_verify_failure(const char* msg, Node* n1, Node* n2) {
 241   if (n1 != nullptr) {
 242     n1->dump(+10);
 243   }
 244   if (n2 != nullptr) {
 245     n2->dump(+10);
 246   }
 247   fatal("%s", msg);
 248 }
 249 
 250 void ShenandoahBarrierC2Support::verify(RootNode* root) {
 251   ResourceMark rm;
 252   Unique_Node_List wq;
 253   GrowableArray<Node*> barriers;
 254   Unique_Node_List barriers_used;
 255   Node_Stack phis(0);
 256   VectorSet visited;
 257   const bool trace = false;
 258   const bool verify_no_useless_barrier = false;
 259 
 260   wq.push(root);
 261   for (uint next = 0; next < wq.size(); next++) {
 262     Node *n = wq.at(next);
 263     if (n->is_Load()) {
 264       const bool trace = false;
 265       if (trace) {tty->print("Verifying"); n->dump();}
 266       if (n->Opcode() == Op_LoadRange || n->Opcode() == Op_LoadKlass || n->Opcode() == Op_LoadNKlass) {
 267         if (trace) {tty->print_cr("Load range/klass");}
 268       } else {
 269         const TypePtr* adr_type = n->as_Load()->adr_type();
 270 
 271         if (adr_type->isa_oopptr() && adr_type->is_oopptr()->offset() == oopDesc::mark_offset_in_bytes()) {
 272           if (trace) {tty->print_cr("Mark load");}
 273         } else if (adr_type->isa_instptr() &&
 274                    adr_type->is_instptr()->instance_klass()->is_subtype_of(Compile::current()->env()->Reference_klass()) &&
 275                    adr_type->is_instptr()->offset() == java_lang_ref_Reference::referent_offset()) {
 276           if (trace) {tty->print_cr("Reference.get()");}
 277         } else if (!verify_helper(n->in(MemNode::Address), phis, visited, ShenandoahLoad, trace, barriers_used)) {
 278           report_verify_failure("Shenandoah verification: Load should have barriers", n);
 279         }
 280       }
 281     } else if (n->is_Store()) {
 282       const bool trace = false;
 283 
 284       if (trace) {tty->print("Verifying"); n->dump();}
 285       if (n->in(MemNode::ValueIn)->bottom_type()->make_oopptr()) {
 286         Node* adr = n->in(MemNode::Address);
 287         bool verify = true;
 288 
 289         if (adr->is_AddP() && adr->in(AddPNode::Base)->is_top()) {
 290           adr = adr->in(AddPNode::Address);
 291           if (adr->is_AddP()) {
 292             assert(adr->in(AddPNode::Base)->is_top(), "");
 293             adr = adr->in(AddPNode::Address);
 294             if (adr->Opcode() == Op_LoadP &&
 295                 adr->in(MemNode::Address)->in(AddPNode::Base)->is_top() &&
 296                 adr->in(MemNode::Address)->in(AddPNode::Address)->Opcode() == Op_ThreadLocal &&
 297                 adr->in(MemNode::Address)->in(AddPNode::Offset)->find_intptr_t_con(-1) == in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset())) {
 298               if (trace) {tty->print_cr("SATB prebarrier");}
 299               verify = false;
 300             }
 301           }
 302         }
 303 
 304         if (verify && !verify_helper(n->in(MemNode::ValueIn), phis, visited, ShenandoahValue, trace, barriers_used)) {
 305           report_verify_failure("Shenandoah verification: Store should have barriers", n);
 306         }
 307       }
 308       if (!verify_helper(n->in(MemNode::Address), phis, visited, ShenandoahStore, trace, barriers_used)) {
 309         report_verify_failure("Shenandoah verification: Store (address) should have barriers", n);
 310       }
 311     } else if (n->Opcode() == Op_CmpP) {
 312       const bool trace = false;
 313 
 314       Node* in1 = n->in(1);
 315       Node* in2 = n->in(2);
 316       if (in1->bottom_type()->isa_oopptr()) {
 317         if (trace) {tty->print("Verifying"); n->dump();}
 318 
 319         bool mark_inputs = false;
 320         if (in1->bottom_type() == TypePtr::NULL_PTR || in2->bottom_type() == TypePtr::NULL_PTR ||
 321             (in1->is_Con() || in2->is_Con())) {
 322           if (trace) {tty->print_cr("Comparison against a constant");}
 323           mark_inputs = true;
 324         } else if ((in1->is_CheckCastPP() && in1->in(1)->is_Proj() && in1->in(1)->in(0)->is_Allocate()) ||
 325                    (in2->is_CheckCastPP() && in2->in(1)->is_Proj() && in2->in(1)->in(0)->is_Allocate())) {
 326           if (trace) {tty->print_cr("Comparison with newly alloc'ed object");}
 327           mark_inputs = true;
 328         } else {
 329           assert(in2->bottom_type()->isa_oopptr(), "");
 330 
 331           if (!verify_helper(in1, phis, visited, ShenandoahStore, trace, barriers_used) ||
 332               !verify_helper(in2, phis, visited, ShenandoahStore, trace, barriers_used)) {
 333             report_verify_failure("Shenandoah verification: Cmp should have barriers", n);
 334           }
 335         }
 336         if (verify_no_useless_barrier &&
 337             mark_inputs &&
 338             (!verify_helper(in1, phis, visited, ShenandoahValue, trace, barriers_used) ||
 339              !verify_helper(in2, phis, visited, ShenandoahValue, trace, barriers_used))) {
 340           phis.clear();
 341           visited.reset();
 342         }
 343       }
 344     } else if (n->is_LoadStore()) {
 345       if (n->in(MemNode::ValueIn)->bottom_type()->make_ptr() &&
 346           !verify_helper(n->in(MemNode::ValueIn), phis, visited, ShenandoahValue, trace, barriers_used)) {
 347         report_verify_failure("Shenandoah verification: LoadStore (value) should have barriers", n);
 348       }
 349 
 350       if (n->in(MemNode::Address)->bottom_type()->make_oopptr() && !verify_helper(n->in(MemNode::Address), phis, visited, ShenandoahStore, trace, barriers_used)) {
 351         report_verify_failure("Shenandoah verification: LoadStore (address) should have barriers", n);
 352       }
 353     } else if (n->Opcode() == Op_CallLeafNoFP || n->Opcode() == Op_CallLeaf) {
 354       CallNode* call = n->as_Call();
 355 
 356       static struct {
 357         const char* name;
 358         struct {
 359           int pos;
 360           verify_type t;
 361         } args[6];
 362       } calls[] = {
 363         "array_partition_stub",
 364         { { TypeFunc::Parms, ShenandoahStore }, { TypeFunc::Parms+4, ShenandoahStore },   { -1, ShenandoahNone },
 365           { -1, ShenandoahNone },                { -1, ShenandoahNone },                  { -1, ShenandoahNone } },
 366         "arraysort_stub",
 367         { { TypeFunc::Parms, ShenandoahStore },  { -1, ShenandoahNone },                  { -1, ShenandoahNone },
 368           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 369         "aescrypt_encryptBlock",
 370         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahStore },  { TypeFunc::Parms+2, ShenandoahLoad },
 371           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 372         "aescrypt_decryptBlock",
 373         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahStore },  { TypeFunc::Parms+2, ShenandoahLoad },
 374           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 375         "multiplyToLen",
 376         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+2, ShenandoahLoad },   { TypeFunc::Parms+4, ShenandoahStore },
 377           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 378         "squareToLen",
 379         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+2, ShenandoahLoad },   { -1,  ShenandoahNone},
 380           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 381         "montgomery_multiply",
 382         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahLoad },   { TypeFunc::Parms+2, ShenandoahLoad },
 383           { TypeFunc::Parms+6, ShenandoahStore }, { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 384         "montgomery_square",
 385         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahLoad },   { TypeFunc::Parms+5, ShenandoahStore },
 386           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 387         "mulAdd",
 388         { { TypeFunc::Parms, ShenandoahStore },  { TypeFunc::Parms+1, ShenandoahLoad },   { -1,  ShenandoahNone},
 389           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 390         "vectorizedMismatch",
 391         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahLoad },   { -1,  ShenandoahNone},
 392           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 393         "updateBytesCRC32",
 394         { { TypeFunc::Parms+1, ShenandoahLoad }, { -1,  ShenandoahNone},                  { -1,  ShenandoahNone},
 395           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 396         "updateBytesAdler32",
 397         { { TypeFunc::Parms+1, ShenandoahLoad }, { -1,  ShenandoahNone},                  { -1,  ShenandoahNone},
 398           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 399         "updateBytesCRC32C",
 400         { { TypeFunc::Parms+1, ShenandoahLoad }, { TypeFunc::Parms+3, ShenandoahLoad},    { -1,  ShenandoahNone},
 401           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 402         "counterMode_AESCrypt",
 403         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahStore },  { TypeFunc::Parms+2, ShenandoahLoad },
 404           { TypeFunc::Parms+3, ShenandoahStore }, { TypeFunc::Parms+5, ShenandoahStore }, { TypeFunc::Parms+6, ShenandoahStore } },
 405         "cipherBlockChaining_encryptAESCrypt",
 406         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahStore },  { TypeFunc::Parms+2, ShenandoahLoad },
 407           { TypeFunc::Parms+3, ShenandoahLoad },  { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 408         "cipherBlockChaining_decryptAESCrypt",
 409         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahStore },  { TypeFunc::Parms+2, ShenandoahLoad },
 410           { TypeFunc::Parms+3, ShenandoahLoad },  { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 411         "shenandoah_clone",
 412         { { TypeFunc::Parms, ShenandoahLoad },   { -1,  ShenandoahNone},                  { -1,  ShenandoahNone},
 413           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 414         "ghash_processBlocks",
 415         { { TypeFunc::Parms, ShenandoahStore },  { TypeFunc::Parms+1, ShenandoahLoad },   { TypeFunc::Parms+2, ShenandoahLoad },
 416           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 417         "sha1_implCompress",
 418         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
 419           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 420         "sha256_implCompress",
 421         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
 422           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 423         "sha512_implCompress",
 424         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
 425           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 426         "sha1_implCompressMB",
 427         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
 428           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 429         "sha256_implCompressMB",
 430         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
 431           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 432         "sha512_implCompressMB",
 433         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
 434           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 435         "encodeBlock",
 436         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+3, ShenandoahStore },   { -1, ShenandoahNone },
 437           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 438         "decodeBlock",
 439         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+3, ShenandoahStore },   { -1, ShenandoahNone },
 440           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 441         "intpoly_montgomeryMult_P256",
 442         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahLoad  },   { TypeFunc::Parms+2, ShenandoahStore },
 443           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 444         "intpoly_assign",
 445         { { TypeFunc::Parms+1, ShenandoahStore }, { TypeFunc::Parms+2, ShenandoahLoad },  { -1, ShenandoahNone },
 446           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 447       };
 448 
 449       if (call->is_call_to_arraycopystub()) {
 450         Node* dest = nullptr;
 451         const TypeTuple* args = n->as_Call()->_tf->domain_sig();
 452         for (uint i = TypeFunc::Parms, j = 0; i < args->cnt(); i++) {
 453           if (args->field_at(i)->isa_ptr()) {
 454             j++;
 455             if (j == 2) {
 456               dest = n->in(i);
 457               break;
 458             }
 459           }
 460         }
 461         if (!verify_helper(n->in(TypeFunc::Parms), phis, visited, ShenandoahLoad, trace, barriers_used) ||
 462             !verify_helper(dest, phis, visited, ShenandoahStore, trace, barriers_used)) {
 463           report_verify_failure("Shenandoah verification: ArrayCopy should have barriers", n);
 464         }
 465       } else if (strlen(call->_name) > 5 &&
 466                  !strcmp(call->_name + strlen(call->_name) - 5, "_fill")) {
 467         if (!verify_helper(n->in(TypeFunc::Parms), phis, visited, ShenandoahStore, trace, barriers_used)) {
 468           report_verify_failure("Shenandoah verification: _fill should have barriers", n);
 469         }
 470       } else if (!strcmp(call->_name, "shenandoah_wb_pre")) {
 471         // skip
 472       } else {
 473         const int calls_len = sizeof(calls) / sizeof(calls[0]);
 474         int i = 0;
 475         for (; i < calls_len; i++) {
 476           if (!strcmp(calls[i].name, call->_name)) {
 477             break;
 478           }
 479         }
 480         if (i != calls_len) {
 481           const uint args_len = sizeof(calls[0].args) / sizeof(calls[0].args[0]);
 482           for (uint j = 0; j < args_len; j++) {
 483             int pos = calls[i].args[j].pos;
 484             if (pos == -1) {
 485               break;
 486             }
 487             if (!verify_helper(call->in(pos), phis, visited, calls[i].args[j].t, trace, barriers_used)) {
 488               report_verify_failure("Shenandoah verification: intrinsic calls should have barriers", n);
 489             }
 490           }
 491           for (uint j = TypeFunc::Parms; j < call->req(); j++) {
 492             if (call->in(j)->bottom_type()->make_ptr() &&
 493                 call->in(j)->bottom_type()->make_ptr()->isa_oopptr()) {
 494               uint k = 0;
 495               for (; k < args_len && calls[i].args[k].pos != (int)j; k++);
 496               if (k == args_len) {
 497                 fatal("arg %d for call %s not covered", j, call->_name);
 498               }
 499             }
 500           }
 501         } else {
 502           for (uint j = TypeFunc::Parms; j < call->req(); j++) {
 503             if (call->in(j)->bottom_type()->make_ptr() &&
 504                 call->in(j)->bottom_type()->make_ptr()->isa_oopptr()) {
 505               fatal("%s not covered", call->_name);
 506             }
 507           }
 508         }
 509       }
 510     } else if (n->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
 511       // skip
 512     } else if (n->is_AddP()
 513                || n->is_Phi()
 514                || n->is_ConstraintCast()
 515                || n->Opcode() == Op_Return
 516                || n->Opcode() == Op_CMoveP
 517                || n->Opcode() == Op_CMoveN
 518                || n->Opcode() == Op_Rethrow
 519                || n->is_MemBar()
 520                || n->Opcode() == Op_Conv2B
 521                || n->Opcode() == Op_SafePoint
 522                || n->is_CallJava()
 523                || n->Opcode() == Op_Unlock
 524                || n->Opcode() == Op_EncodeP
 525                || n->Opcode() == Op_DecodeN) {
 526       // nothing to do
 527     } else {
 528       static struct {
 529         int opcode;
 530         struct {
 531           int pos;
 532           verify_type t;
 533         } inputs[2];
 534       } others[] = {
 535         Op_FastLock,
 536         { { 1, ShenandoahLoad },                  { -1, ShenandoahNone} },
 537         Op_Lock,
 538         { { TypeFunc::Parms, ShenandoahLoad },    { -1, ShenandoahNone} },
 539         Op_ArrayCopy,
 540         { { ArrayCopyNode::Src, ShenandoahLoad }, { ArrayCopyNode::Dest, ShenandoahStore } },
 541         Op_StrCompressedCopy,
 542         { { 2, ShenandoahLoad },                  { 3, ShenandoahStore } },
 543         Op_StrInflatedCopy,
 544         { { 2, ShenandoahLoad },                  { 3, ShenandoahStore } },
 545         Op_AryEq,
 546         { { 2, ShenandoahLoad },                  { 3, ShenandoahLoad } },
 547         Op_StrIndexOf,
 548         { { 2, ShenandoahLoad },                  { 4, ShenandoahLoad } },
 549         Op_StrComp,
 550         { { 2, ShenandoahLoad },                  { 4, ShenandoahLoad } },
 551         Op_StrEquals,
 552         { { 2, ShenandoahLoad },                  { 3, ShenandoahLoad } },
 553         Op_VectorizedHashCode,
 554         { { 2, ShenandoahLoad },                  { -1, ShenandoahNone } },
 555         Op_EncodeISOArray,
 556         { { 2, ShenandoahLoad },                  { 3, ShenandoahStore } },
 557         Op_CountPositives,
 558         { { 2, ShenandoahLoad },                  { -1, ShenandoahNone} },
 559         Op_CastP2X,
 560         { { 1, ShenandoahLoad },                  { -1, ShenandoahNone} },
 561         Op_StrIndexOfChar,
 562         { { 2, ShenandoahLoad },                  { -1, ShenandoahNone } },
 563       };
 564 
 565       const int others_len = sizeof(others) / sizeof(others[0]);
 566       int i = 0;
 567       for (; i < others_len; i++) {
 568         if (others[i].opcode == n->Opcode()) {
 569           break;
 570         }
 571       }
 572       uint stop = n->is_Call() ? n->as_Call()->tf()->domain_sig()->cnt() : n->req();
 573       if (i != others_len) {
 574         const uint inputs_len = sizeof(others[0].inputs) / sizeof(others[0].inputs[0]);
 575         for (uint j = 0; j < inputs_len; j++) {
 576           int pos = others[i].inputs[j].pos;
 577           if (pos == -1) {
 578             break;
 579           }
 580           if (!verify_helper(n->in(pos), phis, visited, others[i].inputs[j].t, trace, barriers_used)) {
 581             report_verify_failure("Shenandoah verification: intrinsic calls should have barriers", n);
 582           }
 583         }
 584         for (uint j = 1; j < stop; j++) {
 585           if (n->in(j) != nullptr && n->in(j)->bottom_type()->make_ptr() &&
 586               n->in(j)->bottom_type()->make_ptr()->make_oopptr()) {
 587             uint k = 0;
 588             for (; k < inputs_len && others[i].inputs[k].pos != (int)j; k++);
 589             if (k == inputs_len) {
 590               fatal("arg %d for node %s not covered", j, n->Name());
 591             }
 592           }
 593         }
 594       } else {
 595         for (uint j = 1; j < stop; j++) {
 596           if (n->in(j) != nullptr && n->in(j)->bottom_type()->make_ptr() &&
 597               n->in(j)->bottom_type()->make_ptr()->make_oopptr()) {
 598             fatal("%s not covered", n->Name());
 599           }
 600         }
 601       }
 602     }
 603 
 604     if (n->is_SafePoint()) {
 605       SafePointNode* sfpt = n->as_SafePoint();
 606       if (verify_no_useless_barrier && sfpt->jvms() != nullptr) {
 607         for (uint i = sfpt->jvms()->scloff(); i < sfpt->jvms()->endoff(); i++) {
 608           if (!verify_helper(sfpt->in(i), phis, visited, ShenandoahLoad, trace, barriers_used)) {
 609             phis.clear();
 610             visited.reset();
 611           }
 612         }
 613       }
 614     }
 615   }
 616 
 617   if (verify_no_useless_barrier) {
 618     for (int i = 0; i < barriers.length(); i++) {
 619       Node* n = barriers.at(i);
 620       if (!barriers_used.member(n)) {
 621         tty->print("XXX useless barrier"); n->dump(-2);
 622         ShouldNotReachHere();
 623       }
 624     }
 625   }
 626 }
 627 #endif
 628 
 629 bool ShenandoahBarrierC2Support::is_anti_dependent_load_at_control(PhaseIdealLoop* phase, Node* maybe_load, Node* store,
 630                                                                    Node* control) {
 631   return maybe_load->is_Load() && phase->C->can_alias(store->adr_type(), phase->C->get_alias_index(maybe_load->adr_type())) &&
 632          phase->ctrl_or_self(maybe_load) == control;
 633 }
 634 
 635 void ShenandoahBarrierC2Support::maybe_push_anti_dependent_loads(PhaseIdealLoop* phase, Node* maybe_store, Node* control, Unique_Node_List &wq) {
 636   if (!maybe_store->is_Store() && !maybe_store->is_LoadStore()) {
 637     return;
 638   }
 639   Node* mem = maybe_store->in(MemNode::Memory);
 640   for (DUIterator_Fast imax, i = mem->fast_outs(imax); i < imax; i++) {
 641     Node* u = mem->fast_out(i);
 642     if (is_anti_dependent_load_at_control(phase, u, maybe_store, control)) {
 643       wq.push(u);
 644     }
 645   }
 646 }
 647 
 648 void ShenandoahBarrierC2Support::push_data_inputs_at_control(PhaseIdealLoop* phase, Node* n, Node* ctrl, Unique_Node_List &wq) {
 649   for (uint i = 0; i < n->req(); i++) {
 650     Node* in = n->in(i);
 651     if (in != nullptr && phase->has_ctrl(in) && phase->get_ctrl(in) == ctrl) {
 652       wq.push(in);
 653     }
 654   }
 655 }
 656 
 657 bool ShenandoahBarrierC2Support::is_dominator_same_ctrl(Node* c, Node* d, Node* n, PhaseIdealLoop* phase) {
 658   // That both nodes have the same control is not sufficient to prove
 659   // domination, verify that there's no path from d to n
 660   ResourceMark rm;
 661   Unique_Node_List wq;
 662   wq.push(d);
 663   for (uint next = 0; next < wq.size(); next++) {
 664     Node *m = wq.at(next);
 665     if (m == n) {
 666       return false;
 667     }
 668     if (m->is_Phi() && m->in(0)->is_Loop()) {
 669       assert(phase->ctrl_or_self(m->in(LoopNode::EntryControl)) != c, "following loop entry should lead to new control");
 670     } else {
 671       // Take anti-dependencies into account
 672       maybe_push_anti_dependent_loads(phase, m, c, wq);
 673       push_data_inputs_at_control(phase, m, c, wq);
 674     }
 675   }
 676   return true;
 677 }
 678 
 679 bool ShenandoahBarrierC2Support::is_dominator(Node* d_c, Node* n_c, Node* d, Node* n, PhaseIdealLoop* phase) {
 680   if (d_c != n_c) {
 681     return phase->is_dominator(d_c, n_c);
 682   }
 683   return is_dominator_same_ctrl(d_c, d, n, phase);
 684 }
 685 
 686 Node* next_mem(Node* mem, int alias) {
 687   Node* res = nullptr;
 688   if (mem->is_Proj()) {
 689     res = mem->in(0);
 690   } else if (mem->is_SafePoint() || mem->is_MemBar()) {
 691     res = mem->in(TypeFunc::Memory);
 692   } else if (mem->is_Phi()) {
 693     res = mem->in(1);
 694   } else if (mem->is_MergeMem()) {
 695     res = mem->as_MergeMem()->memory_at(alias);
 696   } else if (mem->is_Store() || mem->is_LoadStore() || mem->is_ClearArray()) {
 697     assert(alias == Compile::AliasIdxRaw, "following raw memory can't lead to a barrier");
 698     res = mem->in(MemNode::Memory);
 699   } else {
 700 #ifdef ASSERT
 701     mem->dump();
 702 #endif
 703     ShouldNotReachHere();
 704   }
 705   return res;
 706 }
 707 
 708 Node* ShenandoahBarrierC2Support::no_branches(Node* c, Node* dom, bool allow_one_proj, PhaseIdealLoop* phase) {
 709   Node* iffproj = nullptr;
 710   while (c != dom) {
 711     Node* next = phase->idom(c);
 712     assert(next->unique_ctrl_out_or_null() == c || c->is_Proj() || c->is_Region(), "multiple control flow out but no proj or region?");
 713     if (c->is_Region()) {
 714       ResourceMark rm;
 715       Unique_Node_List wq;
 716       wq.push(c);
 717       for (uint i = 0; i < wq.size(); i++) {
 718         Node *n = wq.at(i);
 719         if (n == next) {
 720           continue;
 721         }
 722         if (n->is_Region()) {
 723           for (uint j = 1; j < n->req(); j++) {
 724             wq.push(n->in(j));
 725           }
 726         } else {
 727           wq.push(n->in(0));
 728         }
 729       }
 730       for (uint i = 0; i < wq.size(); i++) {
 731         Node *n = wq.at(i);
 732         assert(n->is_CFG(), "");
 733         if (n->is_Multi()) {
 734           for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
 735             Node* u = n->fast_out(j);
 736             if (u->is_CFG()) {
 737               if (!wq.member(u) && !u->as_Proj()->is_uncommon_trap_proj()) {
 738                 return NodeSentinel;
 739               }
 740             }
 741           }
 742         }
 743       }
 744     } else  if (c->is_Proj()) {
 745       if (c->is_IfProj()) {
 746         if (c->as_Proj()->is_uncommon_trap_if_pattern() != nullptr) {
 747           // continue;
 748         } else {
 749           if (!allow_one_proj) {
 750             return NodeSentinel;
 751           }
 752           if (iffproj == nullptr) {
 753             iffproj = c;
 754           } else {
 755             return NodeSentinel;
 756           }
 757         }
 758       } else if (c->Opcode() == Op_JumpProj) {
 759         return NodeSentinel; // unsupported
 760       } else if (c->Opcode() == Op_CatchProj) {
 761         return NodeSentinel; // unsupported
 762       } else if (c->Opcode() == Op_CProj && next->is_NeverBranch()) {
 763         return NodeSentinel; // unsupported
 764       } else {
 765         assert(next->unique_ctrl_out() == c, "unsupported branch pattern");
 766       }
 767     }
 768     c = next;
 769   }
 770   return iffproj;
 771 }
 772 
 773 Node* ShenandoahBarrierC2Support::dom_mem(Node* mem, Node* ctrl, int alias, Node*& mem_ctrl, PhaseIdealLoop* phase) {
 774   ResourceMark rm;
 775   VectorSet wq;
 776   wq.set(mem->_idx);
 777   mem_ctrl = phase->ctrl_or_self(mem);
 778   while (!phase->is_dominator(mem_ctrl, ctrl) || mem_ctrl == ctrl) {
 779     mem = next_mem(mem, alias);
 780     if (wq.test_set(mem->_idx)) {
 781       return nullptr;
 782     }
 783     mem_ctrl = phase->ctrl_or_self(mem);
 784   }
 785   if (mem->is_MergeMem()) {
 786     mem = mem->as_MergeMem()->memory_at(alias);
 787     mem_ctrl = phase->ctrl_or_self(mem);
 788   }
 789   return mem;
 790 }
 791 
 792 Node* ShenandoahBarrierC2Support::find_bottom_mem(Node* ctrl, PhaseIdealLoop* phase) {
 793   Node* mem = nullptr;
 794   Node* c = ctrl;
 795   do {
 796     if (c->is_Region()) {
 797       for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax && mem == nullptr; i++) {
 798         Node* u = c->fast_out(i);
 799         if (u->is_Phi() && u->bottom_type() == Type::MEMORY) {
 800           if (u->adr_type() == TypePtr::BOTTOM) {
 801             mem = u;
 802           }
 803         }
 804       }
 805     } else {
 806       if (c->is_Call() && c->as_Call()->adr_type() != nullptr) {
 807         CallProjections* projs = c->as_Call()->extract_projections(true, false);
 808         if (projs->fallthrough_memproj != nullptr) {
 809           if (projs->fallthrough_memproj->adr_type() == TypePtr::BOTTOM) {
 810             if (projs->catchall_memproj == nullptr) {
 811               mem = projs->fallthrough_memproj;

 812             } else {
 813               if (phase->is_dominator(projs->fallthrough_catchproj, ctrl)) {
 814                 mem = projs->fallthrough_memproj;
 815               } else {
 816                 assert(phase->is_dominator(projs->catchall_catchproj, ctrl), "one proj must dominate barrier");
 817                 mem = projs->catchall_memproj;
 818               }
 819             }
 820           }
 821         } else {
 822           Node* proj = c->as_Call()->proj_out(TypeFunc::Memory);
 823           if (proj != nullptr &&
 824               proj->adr_type() == TypePtr::BOTTOM) {
 825             mem = proj;
 826           }
 827         }
 828       } else {
 829         for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax; i++) {
 830           Node* u = c->fast_out(i);
 831           if (u->is_Proj() &&
 832               u->bottom_type() == Type::MEMORY &&
 833               u->adr_type() == TypePtr::BOTTOM) {
 834               assert(c->is_SafePoint() || c->is_MemBar() || c->is_Start(), "");
 835               assert(mem == nullptr, "only one proj");
 836               mem = u;
 837           }
 838         }
 839         assert(!c->is_Call() || c->as_Call()->adr_type() != nullptr || mem == nullptr, "no mem projection expected");
 840       }
 841     }
 842     c = phase->idom(c);
 843   } while (mem == nullptr);
 844   return mem;
 845 }
 846 
 847 void ShenandoahBarrierC2Support::follow_barrier_uses(Node* n, Node* ctrl, Unique_Node_List& uses, PhaseIdealLoop* phase) {
 848   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
 849     Node* u = n->fast_out(i);
 850     if (!u->is_CFG() && phase->get_ctrl(u) == ctrl && (!u->is_Phi() || !u->in(0)->is_Loop() || u->in(LoopNode::LoopBackControl) != n)) {
 851       uses.push(u);
 852     }
 853   }
 854 }
 855 
 856 static void hide_strip_mined_loop(OuterStripMinedLoopNode* outer, CountedLoopNode* inner, PhaseIdealLoop* phase) {
 857   OuterStripMinedLoopEndNode* le = inner->outer_loop_end();
 858   Node* new_outer = new LoopNode(outer->in(LoopNode::EntryControl), outer->in(LoopNode::LoopBackControl));
 859   phase->register_control(new_outer, phase->get_loop(outer), outer->in(LoopNode::EntryControl));
 860   Node* new_le = new IfNode(le->in(0), le->in(1), le->_prob, le->_fcnt);
 861   phase->register_control(new_le, phase->get_loop(le), le->in(0));
 862   phase->replace_node_and_forward_ctrl(outer, new_outer);
 863   phase->replace_node_and_forward_ctrl(le, new_le);
 864   inner->clear_strip_mined();
 865 }
 866 
 867 void ShenandoahBarrierC2Support::test_gc_state(Node*& ctrl, Node* raw_mem, Node*& test_fail_ctrl,
 868                                                PhaseIdealLoop* phase, int flags) {
 869   PhaseIterGVN& igvn = phase->igvn();
 870   Node* old_ctrl = ctrl;
 871 
 872   Node* thread          = new ThreadLocalNode();
 873   Node* gc_state_offset = igvn.MakeConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 874   Node* gc_state_addr   = AddPNode::make_off_heap(thread, gc_state_offset);
 875   Node* gc_state        = new LoadBNode(old_ctrl, raw_mem, gc_state_addr,
 876                                         DEBUG_ONLY(phase->C->get_adr_type(Compile::AliasIdxRaw)) NOT_DEBUG(nullptr),
 877                                         TypeInt::BYTE, MemNode::unordered);
 878   Node* gc_state_and    = new AndINode(gc_state, igvn.intcon(flags));
 879   Node* gc_state_cmp    = new CmpINode(gc_state_and, igvn.zerocon(T_INT));
 880   Node* gc_state_bool   = new BoolNode(gc_state_cmp, BoolTest::ne);
 881 
 882   IfNode* gc_state_iff  = new IfNode(old_ctrl, gc_state_bool, PROB_UNLIKELY(0.999), COUNT_UNKNOWN);
 883   ctrl                  = new IfTrueNode(gc_state_iff);
 884   test_fail_ctrl        = new IfFalseNode(gc_state_iff);
 885 
 886   IdealLoopTree* loop = phase->get_loop(old_ctrl);
 887   phase->register_control(gc_state_iff,   loop, old_ctrl);
 888   phase->register_control(ctrl,           loop, gc_state_iff);
 889   phase->register_control(test_fail_ctrl, loop, gc_state_iff);
 890 
 891   phase->register_new_node(thread,        old_ctrl);
 892   phase->register_new_node(gc_state_addr, old_ctrl);
 893   phase->register_new_node(gc_state,      old_ctrl);
 894   phase->register_new_node(gc_state_and,  old_ctrl);
 895   phase->register_new_node(gc_state_cmp,  old_ctrl);
 896   phase->register_new_node(gc_state_bool, old_ctrl);
 897 
 898   phase->set_root_as_ctrl(gc_state_offset);
 899 
 900   assert(is_gc_state_test(gc_state_iff, flags), "Should match the shape");
 901 }
 902 
 903 void ShenandoahBarrierC2Support::test_null(Node*& ctrl, Node* val, Node*& null_ctrl, PhaseIdealLoop* phase) {
 904   Node* old_ctrl = ctrl;
 905   PhaseIterGVN& igvn = phase->igvn();
 906 
 907   const Type* val_t = igvn.type(val);
 908   if (val_t->meet(TypePtr::NULL_PTR) == val_t) {
 909     Node* null_cmp   = new CmpPNode(val, igvn.zerocon(T_OBJECT));
 910     Node* null_test  = new BoolNode(null_cmp, BoolTest::ne);
 911 
 912     IfNode* null_iff = new IfNode(old_ctrl, null_test, PROB_LIKELY(0.999), COUNT_UNKNOWN);
 913     ctrl             = new IfTrueNode(null_iff);
 914     null_ctrl        = new IfFalseNode(null_iff);
 915 
 916     IdealLoopTree* loop = phase->get_loop(old_ctrl);
 917     phase->register_control(null_iff,  loop, old_ctrl);
 918     phase->register_control(ctrl,      loop, null_iff);
 919     phase->register_control(null_ctrl, loop, null_iff);
 920 
 921     phase->register_new_node(null_cmp,  old_ctrl);
 922     phase->register_new_node(null_test, old_ctrl);
 923   }
 924 }
 925 
 926 void ShenandoahBarrierC2Support::test_in_cset(Node*& ctrl, Node*& not_cset_ctrl, Node* val, Node* raw_mem, PhaseIdealLoop* phase) {
 927   Node* old_ctrl = ctrl;
 928   PhaseIterGVN& igvn = phase->igvn();
 929 
 930   Node* raw_val        = new CastP2XNode(old_ctrl, val);
 931   Node* region_size_shift = nullptr;
 932   if (AOTCodeCache::is_on_for_dump()) {
 933     Node* aot_addr = igvn.makecon(TypeRawPtr::make(AOTRuntimeConstants::grain_shift_address()));
 934     region_size_shift = new LoadINode(old_ctrl, raw_mem, aot_addr,
 935                                       DEBUG_ONLY(phase->C->get_adr_type(Compile::AliasIdxRaw)) NOT_DEBUG(nullptr),
 936                                       TypeInt::INT, MemNode::unordered);
 937     phase->register_new_node(region_size_shift, old_ctrl);
 938   } else {
 939     region_size_shift = igvn.intcon(ShenandoahHeapRegion::region_size_bytes_shift_jint());
 940   }
 941   Node* cset_idx       = new URShiftXNode(raw_val, region_size_shift);
 942 
 943   // Figure out the target cset address with raw pointer math.
 944   // This avoids matching AddP+LoadB that would emit inefficient code.
 945   // See JDK-8245465.
 946   Node* cset_addr_ptr = nullptr;
 947   if (AOTCodeCache::is_on_for_dump()) {
 948     Node* aot_addr = igvn.makecon(TypeRawPtr::make(AOTRuntimeConstants::cset_base_address()));
 949     cset_addr_ptr = new LoadPNode(old_ctrl, raw_mem, aot_addr,
 950                                   DEBUG_ONLY(phase->C->get_adr_type(Compile::AliasIdxRaw)) NOT_DEBUG(nullptr),
 951                                   TypeRawPtr::NOTNULL, MemNode::unordered);
 952     phase->register_new_node(cset_addr_ptr, old_ctrl);
 953   } else {
 954     cset_addr_ptr  = igvn.makecon(TypeRawPtr::make(ShenandoahHeap::in_cset_fast_test_addr()));
 955   }
 956   Node* cset_addr      = new CastP2XNode(old_ctrl, cset_addr_ptr);
 957   Node* cset_load_addr = new AddXNode(cset_addr, cset_idx);
 958   Node* cset_load_ptr  = new CastX2PNode(cset_load_addr);
 959 
 960   Node* cset_load      = new LoadBNode(old_ctrl, raw_mem, cset_load_ptr,
 961                                        DEBUG_ONLY(phase->C->get_adr_type(Compile::AliasIdxRaw)) NOT_DEBUG(nullptr),
 962                                        TypeInt::BYTE, MemNode::unordered);
 963   Node* cset_cmp       = new CmpINode(cset_load, igvn.zerocon(T_INT));
 964   Node* cset_bool      = new BoolNode(cset_cmp, BoolTest::ne);
 965 
 966   IfNode* cset_iff     = new IfNode(old_ctrl, cset_bool, PROB_UNLIKELY(0.999), COUNT_UNKNOWN);
 967   ctrl                 = new IfTrueNode(cset_iff);
 968   not_cset_ctrl        = new IfFalseNode(cset_iff);
 969 
 970   IdealLoopTree *loop = phase->get_loop(old_ctrl);
 971   phase->register_control(cset_iff,      loop, old_ctrl);
 972   phase->register_control(ctrl,          loop, cset_iff);
 973   phase->register_control(not_cset_ctrl, loop, cset_iff);
 974 
 975   phase->set_root_as_ctrl(cset_addr_ptr);
 976 
 977   phase->register_new_node(raw_val,        old_ctrl);
 978   phase->register_new_node(cset_idx,       old_ctrl);
 979   phase->register_new_node(cset_addr,      old_ctrl);
 980   phase->register_new_node(cset_load_addr, old_ctrl);
 981   phase->register_new_node(cset_load_ptr,  old_ctrl);
 982   phase->register_new_node(cset_load,      old_ctrl);
 983   phase->register_new_node(cset_cmp,       old_ctrl);
 984   phase->register_new_node(cset_bool,      old_ctrl);
 985 }
 986 
 987 void ShenandoahBarrierC2Support::call_lrb_stub(Node*& ctrl, Node*& val, Node* load_addr,
 988                                                DecoratorSet decorators, PhaseIdealLoop* phase) {
 989   IdealLoopTree*loop = phase->get_loop(ctrl);
 990   const TypePtr* obj_type = phase->igvn().type(val)->is_oopptr();
 991 
 992   address calladdr = nullptr;
 993   const char* name = nullptr;
 994   bool is_strong  = ShenandoahBarrierSet::is_strong_access(decorators);
 995   bool is_weak    = ShenandoahBarrierSet::is_weak_access(decorators);
 996   bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
 997   bool is_native  = ShenandoahBarrierSet::is_native_access(decorators);
 998   bool is_narrow  = UseCompressedOops && !is_native;
 999   if (is_strong) {
1000     if (is_narrow) {
1001       calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow);
1002       name = "load_reference_barrier_strong_narrow";
1003     } else {
1004       calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong);
1005       name = "load_reference_barrier_strong";
1006     }
1007   } else if (is_weak) {
1008     if (is_narrow) {
1009       calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow);
1010       name = "load_reference_barrier_weak_narrow";
1011     } else {
1012       calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak);
1013       name = "load_reference_barrier_weak";
1014     }
1015   } else {
1016     assert(is_phantom, "only remaining strength");
1017     if (is_narrow) {
1018       calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom_narrow);
1019       name = "load_reference_barrier_phantom_narrow";
1020     } else {
1021       calladdr = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom);
1022       name = "load_reference_barrier_phantom";
1023     }
1024   }
1025   Node* call = new CallLeafNode(ShenandoahBarrierSetC2::load_reference_barrier_Type(), calladdr, name, TypeRawPtr::BOTTOM);
1026 
1027   call->init_req(TypeFunc::Control, ctrl);
1028   call->init_req(TypeFunc::I_O, phase->C->top());
1029   call->init_req(TypeFunc::Memory, phase->C->top());
1030   call->init_req(TypeFunc::FramePtr, phase->C->top());
1031   call->init_req(TypeFunc::ReturnAdr, phase->C->top());
1032   call->init_req(TypeFunc::Parms, val);
1033   call->init_req(TypeFunc::Parms+1, load_addr);
1034   phase->register_control(call, loop, ctrl);
1035   ctrl = new ProjNode(call, TypeFunc::Control);
1036   phase->register_control(ctrl, loop, call);
1037   val = new ProjNode(call, TypeFunc::Parms);
1038   phase->register_new_node(val, call);
1039   val = new CheckCastPPNode(ctrl, val, obj_type);
1040   phase->register_new_node(val, ctrl);
1041 }
1042 
1043 void ShenandoahBarrierC2Support::collect_nodes_above_barrier(Unique_Node_List &nodes_above_barrier, PhaseIdealLoop* phase, Node* ctrl, Node* init_raw_mem) {
1044   nodes_above_barrier.clear();
1045   if (phase->has_ctrl(init_raw_mem) && phase->get_ctrl(init_raw_mem) == ctrl && !init_raw_mem->is_Phi()) {
1046     nodes_above_barrier.push(init_raw_mem);
1047   }
1048   for (uint next = 0; next < nodes_above_barrier.size(); next++) {
1049     Node* n = nodes_above_barrier.at(next);
1050     // Take anti-dependencies into account
1051     maybe_push_anti_dependent_loads(phase, n, ctrl, nodes_above_barrier);
1052     push_data_inputs_at_control(phase, n, ctrl, nodes_above_barrier);
1053   }
1054 }
1055 
1056 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) {
1057   Node* ctrl = phase->get_ctrl(barrier);
1058   Node* init_raw_mem = fixer.find_mem(ctrl, barrier);
1059 
1060   // Update the control of all nodes that should be after the
1061   // barrier control flow
1062   uses.clear();
1063   // Every node that is control dependent on the barrier's input
1064   // control will be after the expanded barrier. The raw memory (if
1065   // its memory is control dependent on the barrier's input control)
1066   // must stay above the barrier.
1067   collect_nodes_above_barrier(nodes_above_barrier, phase, ctrl, init_raw_mem);
1068   for (DUIterator_Fast imax, i = ctrl->fast_outs(imax); i < imax; i++) {
1069     Node* u = ctrl->fast_out(i);
1070     if (u->_idx < last &&
1071         u != barrier &&
1072         !u->depends_only_on_test() && // preserve dependency on test
1073         !nodes_above_barrier.member(u) &&
1074         (u->in(0) != ctrl || (!u->is_Region() && !u->is_Phi())) &&
1075         (ctrl->Opcode() != Op_CatchProj || u->Opcode() != Op_CreateEx)) {
1076       Node* old_c = phase->ctrl_or_self(u);
1077       if (old_c != ctrl ||
1078           is_dominator_same_ctrl(old_c, barrier, u, phase) ||
1079           ShenandoahBarrierSetC2::is_shenandoah_state_load(u)) {
1080         phase->igvn().rehash_node_delayed(u);
1081         int nb = u->replace_edge(ctrl, region, &phase->igvn());
1082         if (u->is_CFG()) {
1083           if (phase->idom(u) == ctrl) {
1084             phase->set_idom(u, region, phase->dom_depth(region));
1085           }
1086         } else if (phase->get_ctrl(u) == ctrl) {
1087           assert(u != init_raw_mem, "should leave input raw mem above the barrier");
1088           uses.push(u);
1089         }
1090         assert(nb == 1, "more than 1 ctrl input?");
1091         --i, imax -= nb;
1092       }
1093     }
1094   }
1095 }
1096 
1097 static Node* create_phis_on_call_return(Node* ctrl, Node* c, Node* n, Node* n_clone, const CallProjections* projs, PhaseIdealLoop* phase) {
1098   Node* region = nullptr;
1099   while (c != ctrl) {
1100     if (c->is_Region()) {
1101       region = c;
1102     }
1103     c = phase->idom(c);
1104   }
1105   assert(region != nullptr, "");
1106   Node* phi = new PhiNode(region, n->bottom_type());
1107   for (uint j = 1; j < region->req(); j++) {
1108     Node* in = region->in(j);
1109     if (phase->is_dominator(projs->fallthrough_catchproj, in)) {
1110       phi->init_req(j, n);
1111     } else if (phase->is_dominator(projs->catchall_catchproj, in)) {
1112       phi->init_req(j, n_clone);
1113     } else {
1114       phi->init_req(j, create_phis_on_call_return(ctrl, in, n, n_clone, projs, phase));
1115     }
1116   }
1117   phase->register_new_node(phi, region);
1118   return phi;
1119 }
1120 
1121 void ShenandoahBarrierC2Support::pin_and_expand(PhaseIdealLoop* phase) {
1122   ShenandoahBarrierSetC2State* state = ShenandoahBarrierSetC2::bsc2()->state();
1123 
1124   Unique_Node_List uses;
1125   Node_Stack stack(0);
1126   Node_List clones;
1127   for (int i = state->load_reference_barriers_count() - 1; i >= 0; i--) {
1128     ShenandoahLoadReferenceBarrierNode* lrb = state->load_reference_barrier(i);
1129 
1130     Node* ctrl = phase->get_ctrl(lrb);
1131     Node* val = lrb->in(ShenandoahLoadReferenceBarrierNode::ValueIn);
1132 
1133     CallStaticJavaNode* unc = nullptr;
1134     Node* unc_ctrl = nullptr;
1135     Node* uncasted_val = val;
1136 
1137     for (DUIterator_Fast imax, i = lrb->fast_outs(imax); i < imax; i++) {
1138       Node* u = lrb->fast_out(i);
1139       if (u->Opcode() == Op_CastPP &&
1140           u->in(0) != nullptr &&
1141           phase->is_dominator(u->in(0), ctrl)) {
1142         const Type* u_t = phase->igvn().type(u);
1143 
1144         if (u_t->meet(TypePtr::NULL_PTR) != u_t &&
1145             u->in(0)->Opcode() == Op_IfTrue &&
1146             u->in(0)->as_Proj()->is_uncommon_trap_if_pattern() &&
1147             u->in(0)->in(0)->is_If() &&
1148             u->in(0)->in(0)->in(1)->Opcode() == Op_Bool &&
1149             u->in(0)->in(0)->in(1)->as_Bool()->_test._test == BoolTest::ne &&
1150             u->in(0)->in(0)->in(1)->in(1)->Opcode() == Op_CmpP &&
1151             u->in(0)->in(0)->in(1)->in(1)->in(1) == val &&
1152             u->in(0)->in(0)->in(1)->in(1)->in(2)->bottom_type() == TypePtr::NULL_PTR) {
1153           IdealLoopTree* loop = phase->get_loop(ctrl);
1154           IdealLoopTree* unc_loop = phase->get_loop(u->in(0));
1155 
1156           if (!unc_loop->is_member(loop)) {
1157             continue;
1158           }
1159 
1160           Node* branch = no_branches(ctrl, u->in(0), false, phase);
1161           assert(branch == nullptr || branch == NodeSentinel, "was not looking for a branch");
1162           if (branch == NodeSentinel) {
1163             continue;
1164           }
1165 
1166           Node* iff = u->in(0)->in(0);
1167           Node* bol = iff->in(1)->clone();
1168           Node* cmp = bol->in(1)->clone();
1169           cmp->set_req(1, lrb);
1170           bol->set_req(1, cmp);
1171           phase->igvn().replace_input_of(iff, 1, bol);
1172           phase->set_ctrl(lrb, iff->in(0));
1173           phase->register_new_node(cmp, iff->in(0));
1174           phase->register_new_node(bol, iff->in(0));
1175           break;
1176         }
1177       }
1178     }
1179     // Load barrier on the control output of a call
1180     if ((ctrl->is_Proj() && ctrl->in(0)->is_CallJava()) || ctrl->is_CallJava()) {
1181       CallJavaNode* call = ctrl->is_Proj() ? ctrl->in(0)->as_CallJava() : ctrl->as_CallJava();
1182       if (call->entry_point() == OptoRuntime::rethrow_stub()) {
1183         // The rethrow call may have too many projections to be
1184         // properly handled here. Given there's no reason for a
1185         // barrier to depend on the call, move it above the call
1186         stack.push(lrb, 0);
1187         do {
1188           Node* n = stack.node();
1189           uint idx = stack.index();
1190           if (idx < n->req()) {
1191             Node* in = n->in(idx);
1192             stack.set_index(idx+1);
1193             if (in != nullptr) {
1194               if (phase->has_ctrl(in)) {
1195                 if (phase->is_dominator(call, phase->get_ctrl(in))) {
1196 #ifdef ASSERT
1197                   for (uint i = 0; i < stack.size(); i++) {
1198                     assert(stack.node_at(i) != in, "node shouldn't have been seen yet");
1199                   }
1200 #endif
1201                   stack.push(in, 0);
1202                 }
1203               } else {
1204                 assert(phase->is_dominator(in, call->in(0)), "no dependency on the call");
1205               }
1206             }
1207           } else {
1208             phase->set_ctrl(n, call->in(0));
1209             stack.pop();
1210           }
1211         } while(stack.size() > 0);
1212         continue;
1213       }
1214       CallProjections* projs = call->extract_projections(false, false);

1215 
1216       // If this is a runtime call, it doesn't have an exception handling path
1217       if (projs->fallthrough_catchproj == nullptr) {
1218         assert(call->method() == nullptr, "should be runtime call");
1219         assert(projs->catchall_catchproj == nullptr, "runtime call should not have catch all projection");
1220         continue;
1221       }
1222 
1223       // Otherwise, clone the barrier so there's one for the fallthrough and one for the exception handling path
1224 #ifdef ASSERT
1225       VectorSet cloned;
1226 #endif
1227       Node* lrb_clone = lrb->clone();
1228       phase->register_new_node(lrb_clone, projs->catchall_catchproj);
1229       phase->set_ctrl(lrb, projs->fallthrough_catchproj);
1230 
1231       stack.push(lrb, 0);
1232       clones.push(lrb_clone);
1233 
1234       do {
1235         assert(stack.size() == clones.size(), "");
1236         Node* n = stack.node();
1237 #ifdef ASSERT
1238         if (n->is_Load()) {
1239           Node* mem = n->in(MemNode::Memory);
1240           for (DUIterator_Fast jmax, j = mem->fast_outs(jmax); j < jmax; j++) {
1241             Node* u = mem->fast_out(j);
1242             assert(!u->is_Store() || !u->is_LoadStore() || phase->get_ctrl(u) != ctrl, "anti dependent store?");
1243           }
1244         }
1245 #endif
1246         uint idx = stack.index();
1247         Node* n_clone = clones.at(clones.size()-1);
1248         if (idx < n->outcnt()) {
1249           Node* u = n->raw_out(idx);
1250           Node* c = phase->ctrl_or_self(u);
1251           if (phase->is_dominator(call, c) && phase->is_dominator(c, projs->fallthrough_proj)) {
1252             stack.set_index(idx+1);
1253             assert(!u->is_CFG(), "");
1254             stack.push(u, 0);
1255             assert(!cloned.test_set(u->_idx), "only one clone");
1256             Node* u_clone = u->clone();
1257             int nb = u_clone->replace_edge(n, n_clone, &phase->igvn());
1258             assert(nb > 0, "should have replaced some uses");
1259             phase->register_new_node(u_clone, projs->catchall_catchproj);
1260             clones.push(u_clone);
1261             phase->set_ctrl(u, projs->fallthrough_catchproj);
1262           } else {
1263             bool replaced = false;
1264             if (u->is_Phi()) {
1265               for (uint k = 1; k < u->req(); k++) {
1266                 if (u->in(k) == n) {
1267                   if (phase->is_dominator(projs->catchall_catchproj, u->in(0)->in(k))) {
1268                     phase->igvn().replace_input_of(u, k, n_clone);
1269                     replaced = true;
1270                   } else if (!phase->is_dominator(projs->fallthrough_catchproj, u->in(0)->in(k))) {
1271                     phase->igvn().replace_input_of(u, k, create_phis_on_call_return(ctrl, u->in(0)->in(k), n, n_clone, projs, phase));
1272                     replaced = true;
1273                   }
1274                 }
1275               }
1276             } else {
1277               if (phase->is_dominator(projs->catchall_catchproj, c)) {
1278                 phase->igvn().rehash_node_delayed(u);
1279                 int nb = u->replace_edge(n, n_clone, &phase->igvn());
1280                 assert(nb > 0, "should have replaced some uses");
1281                 replaced = true;
1282               } else if (!phase->is_dominator(projs->fallthrough_catchproj, c)) {
1283                 if (u->is_If()) {
1284                   // Can't break If/Bool/Cmp chain
1285                   assert(n->is_Bool(), "unexpected If shape");
1286                   assert(stack.node_at(stack.size()-2)->is_Cmp(), "unexpected If shape");
1287                   assert(n_clone->is_Bool(), "unexpected clone");
1288                   assert(clones.at(clones.size()-2)->is_Cmp(), "unexpected clone");
1289                   Node* bol_clone = n->clone();
1290                   Node* cmp_clone = stack.node_at(stack.size()-2)->clone();
1291                   bol_clone->set_req(1, cmp_clone);
1292 
1293                   Node* nn = stack.node_at(stack.size()-3);
1294                   Node* nn_clone = clones.at(clones.size()-3);
1295                   assert(nn->Opcode() == nn_clone->Opcode(), "mismatch");
1296 
1297                   int nb = cmp_clone->replace_edge(nn, create_phis_on_call_return(ctrl, c, nn, nn_clone, projs, phase),
1298                                                    &phase->igvn());
1299                   assert(nb > 0, "should have replaced some uses");
1300 
1301                   phase->register_new_node(bol_clone, u->in(0));
1302                   phase->register_new_node(cmp_clone, u->in(0));
1303 
1304                   phase->igvn().replace_input_of(u, 1, bol_clone);
1305 
1306                 } else {
1307                   phase->igvn().rehash_node_delayed(u);
1308                   int nb = u->replace_edge(n, create_phis_on_call_return(ctrl, c, n, n_clone, projs, phase), &phase->igvn());
1309                   assert(nb > 0, "should have replaced some uses");
1310                 }
1311                 replaced = true;
1312               }
1313             }
1314             if (!replaced) {
1315               stack.set_index(idx+1);
1316             }
1317           }
1318         } else {
1319           stack.pop();
1320           clones.pop();
1321         }
1322       } while (stack.size() > 0);
1323       assert(stack.size() == 0 && clones.size() == 0, "");
1324     }
1325   }
1326 
1327   for (int i = 0; i < state->load_reference_barriers_count(); i++) {
1328     ShenandoahLoadReferenceBarrierNode* lrb = state->load_reference_barrier(i);
1329     Node* ctrl = phase->get_ctrl(lrb);
1330     IdealLoopTree* loop = phase->get_loop(ctrl);
1331     Node* head = loop->head();
1332     if (head->is_OuterStripMinedLoop()) {
1333       // Expanding a barrier here will break loop strip mining
1334       // verification. Transform the loop so the loop nest doesn't
1335       // appear as strip mined.
1336       OuterStripMinedLoopNode* outer = head->as_OuterStripMinedLoop();
1337       hide_strip_mined_loop(outer, outer->unique_ctrl_out()->as_CountedLoop(), phase);
1338     }
1339     if (head->is_BaseCountedLoop() && ctrl->is_IfProj() && ctrl->in(0)->is_BaseCountedLoopEnd() &&
1340         head->as_BaseCountedLoop()->loopexit() == ctrl->in(0)) {
1341       Node* entry = head->in(LoopNode::EntryControl);
1342       Node* backedge = head->in(LoopNode::LoopBackControl);
1343       Node* new_head = new LoopNode(entry, backedge);
1344       phase->register_control(new_head, phase->get_loop(entry), entry);
1345       phase->replace_node_and_forward_ctrl(head, new_head);
1346     }
1347   }
1348 
1349   // Expand load-reference-barriers
1350   MemoryGraphFixer fixer(Compile::AliasIdxRaw, true, phase);
1351   Unique_Node_List nodes_above_barriers;
1352   for (int i = state->load_reference_barriers_count() - 1; i >= 0; i--) {
1353     ShenandoahLoadReferenceBarrierNode* lrb = state->load_reference_barrier(i);
1354     uint last = phase->C->unique();
1355     Node* ctrl = phase->get_ctrl(lrb);
1356     Node* val = lrb->in(ShenandoahLoadReferenceBarrierNode::ValueIn);
1357 
1358     Node* orig_ctrl = ctrl;
1359 
1360     Node* raw_mem = fixer.find_mem(ctrl, lrb);
1361     Node* raw_mem_for_ctrl = fixer.find_mem(ctrl, nullptr);
1362 
1363     IdealLoopTree *loop = phase->get_loop(ctrl);
1364 
1365     Node* heap_stable_ctrl = nullptr;
1366     Node* null_ctrl = nullptr;
1367 
1368     assert(val->bottom_type()->make_oopptr(), "need oop");
1369     assert(val->bottom_type()->make_oopptr()->const_oop() == nullptr, "expect non-constant");
1370 
1371     enum { _heap_stable = 1, _evac_path, _not_cset, PATH_LIMIT };
1372     Node* region = new RegionNode(PATH_LIMIT);
1373     Node* val_phi = new PhiNode(region, val->bottom_type()->is_oopptr());
1374 
1375     // Stable path.
1376     int flags = ShenandoahHeap::HAS_FORWARDED;
1377     if (!ShenandoahBarrierSet::is_strong_access(lrb->decorators())) {
1378       flags |= ShenandoahHeap::WEAK_ROOTS;
1379     }
1380     test_gc_state(ctrl, raw_mem, heap_stable_ctrl, phase, flags);
1381     IfNode* heap_stable_iff = heap_stable_ctrl->in(0)->as_If();
1382 
1383     // Heap stable case
1384     region->init_req(_heap_stable, heap_stable_ctrl);
1385     val_phi->init_req(_heap_stable, val);
1386 
1387     // Test for in-cset, unless it's a native-LRB. Native LRBs need to return null
1388     // even for non-cset objects to prevent resurrection of such objects.
1389     // Wires !in_cset(obj) to slot 2 of region and phis
1390     Node* not_cset_ctrl = nullptr;
1391     if (ShenandoahBarrierSet::is_strong_access(lrb->decorators())) {
1392       test_in_cset(ctrl, not_cset_ctrl, val, raw_mem, phase);
1393     }
1394     if (not_cset_ctrl != nullptr) {
1395       region->init_req(_not_cset, not_cset_ctrl);
1396       val_phi->init_req(_not_cset, val);
1397     } else {
1398       region->del_req(_not_cset);
1399       val_phi->del_req(_not_cset);
1400     }
1401 
1402     // Resolve object when orig-value is in cset.
1403     // Make the unconditional resolve for fwdptr.
1404 
1405     // Call lrb-stub and wire up that path in slots 4
1406     Node* result_mem = nullptr;
1407 
1408     Node* addr;
1409     {
1410       VectorSet visited;
1411       addr = get_load_addr(phase, visited, lrb);
1412     }
1413     if (addr->Opcode() == Op_AddP) {
1414       Node* orig_base = addr->in(AddPNode::Base);
1415       Node* base = new CheckCastPPNode(ctrl, orig_base, orig_base->bottom_type(), ConstraintCastNode::DependencyType::NonFloatingNarrowing);
1416       phase->register_new_node(base, ctrl);
1417       if (addr->in(AddPNode::Base) == addr->in((AddPNode::Address))) {
1418         // Field access
1419         addr = addr->clone();
1420         addr->set_req(AddPNode::Base, base);
1421         addr->set_req(AddPNode::Address, base);
1422         phase->register_new_node(addr, ctrl);
1423       } else {
1424         Node* addr2 = addr->in(AddPNode::Address);
1425         if (addr2->Opcode() == Op_AddP && addr2->in(AddPNode::Base) == addr2->in(AddPNode::Address) &&
1426               addr2->in(AddPNode::Base) == orig_base) {
1427           addr2 = addr2->clone();
1428           addr2->set_req(AddPNode::Base, base);
1429           addr2->set_req(AddPNode::Address, base);
1430           phase->register_new_node(addr2, ctrl);
1431           addr = addr->clone();
1432           addr->set_req(AddPNode::Base, base);
1433           addr->set_req(AddPNode::Address, addr2);
1434           phase->register_new_node(addr, ctrl);
1435         }
1436       }
1437     }
1438     call_lrb_stub(ctrl, val, addr, lrb->decorators(), phase);
1439     region->init_req(_evac_path, ctrl);
1440     val_phi->init_req(_evac_path, val);
1441 
1442     phase->register_control(region, loop, heap_stable_iff);
1443     Node* out_val = val_phi;
1444     phase->register_new_node(val_phi, region);
1445 
1446     fix_ctrl(lrb, region, fixer, uses, nodes_above_barriers, last, phase);
1447 
1448     ctrl = orig_ctrl;
1449 
1450     phase->igvn().replace_node(lrb, out_val);
1451 
1452     follow_barrier_uses(out_val, ctrl, uses, phase);
1453 
1454     for(uint next = 0; next < uses.size(); next++ ) {
1455       Node *n = uses.at(next);
1456       assert(phase->get_ctrl(n) == ctrl, "bad control");
1457       assert(n != raw_mem, "should leave input raw mem above the barrier");
1458       phase->set_ctrl(n, region);
1459       follow_barrier_uses(n, ctrl, uses, phase);
1460     }
1461     fixer.record_new_ctrl(ctrl, region, raw_mem, raw_mem_for_ctrl);
1462   }
1463   // Done expanding load-reference-barriers.
1464   assert(ShenandoahBarrierSetC2::bsc2()->state()->load_reference_barriers_count() == 0, "all load reference barrier nodes should have been replaced");
1465 }
1466 
1467 Node* ShenandoahBarrierC2Support::get_load_addr(PhaseIdealLoop* phase, VectorSet& visited, Node* in) {
1468   if (visited.test_set(in->_idx)) {
1469     return nullptr;
1470   }
1471   switch (in->Opcode()) {
1472     case Op_Proj:
1473       return get_load_addr(phase, visited, in->in(0));
1474     case Op_CastPP:
1475     case Op_CheckCastPP:
1476     case Op_DecodeN:
1477     case Op_EncodeP:
1478       return get_load_addr(phase, visited, in->in(1));
1479     case Op_LoadN:
1480     case Op_LoadP:
1481       return in->in(MemNode::Address);
1482     case Op_CompareAndExchangeN:
1483     case Op_CompareAndExchangeP:
1484     case Op_GetAndSetN:
1485     case Op_GetAndSetP:
1486     case Op_ShenandoahCompareAndExchangeP:
1487     case Op_ShenandoahCompareAndExchangeN:
1488       // Those instructions would just have stored a different
1489       // value into the field. No use to attempt to fix it at this point.
1490       return phase->igvn().zerocon(T_OBJECT);
1491     case Op_CMoveP:
1492     case Op_CMoveN: {
1493       Node* t = get_load_addr(phase, visited, in->in(CMoveNode::IfTrue));
1494       Node* f = get_load_addr(phase, visited, in->in(CMoveNode::IfFalse));
1495       // Handle unambiguous cases: single address reported on both branches.
1496       if (t != nullptr && f == nullptr) return t;
1497       if (t == nullptr && f != nullptr) return f;
1498       if (t != nullptr && t == f)    return t;
1499       // Ambiguity.
1500       return phase->igvn().zerocon(T_OBJECT);
1501     }
1502     case Op_Phi: {
1503       Node* addr = nullptr;
1504       for (uint i = 1; i < in->req(); i++) {
1505         Node* addr1 = get_load_addr(phase, visited, in->in(i));
1506         if (addr == nullptr) {
1507           addr = addr1;
1508         }
1509         if (addr != addr1) {
1510           return phase->igvn().zerocon(T_OBJECT);
1511         }
1512       }
1513       return addr;
1514     }
1515     case Op_ShenandoahLoadReferenceBarrier:
1516       return get_load_addr(phase, visited, in->in(ShenandoahLoadReferenceBarrierNode::ValueIn));
1517     case Op_CallDynamicJava:
1518     case Op_CallLeaf:
1519     case Op_CallStaticJava:
1520     case Op_ConN:
1521     case Op_ConP:
1522     case Op_Parm:
1523     case Op_CreateEx:
1524       return phase->igvn().zerocon(T_OBJECT);
1525     default:
1526 #ifdef ASSERT
1527       fatal("Unknown node in get_load_addr: %s", NodeClassNames[in->Opcode()]);
1528 #endif
1529       return phase->igvn().zerocon(T_OBJECT);
1530   }
1531 
1532 }
1533 
1534 #ifdef ASSERT
1535 static bool has_never_branch(Node* root) {
1536   for (uint i = 1; i < root->req(); i++) {
1537     Node* in = root->in(i);
1538     if (in != nullptr && in->Opcode() == Op_Halt && in->in(0)->is_Proj() && in->in(0)->in(0)->is_NeverBranch()) {
1539       return true;
1540     }
1541   }
1542   return false;
1543 }
1544 #endif
1545 
1546 void MemoryGraphFixer::collect_memory_nodes() {
1547   Node_Stack stack(0);
1548   VectorSet visited;
1549   Node_List regions;
1550 
1551   // Walk the raw memory graph and create a mapping from CFG node to
1552   // memory node. Exclude phis for now.
1553   stack.push(_phase->C->root(), 1);
1554   do {
1555     Node* n = stack.node();
1556     int opc = n->Opcode();
1557     uint i = stack.index();
1558     if (i < n->req()) {
1559       Node* mem = nullptr;
1560       if (opc == Op_Root) {
1561         Node* in = n->in(i);
1562         int in_opc = in->Opcode();
1563         if (in_opc == Op_Return || in_opc == Op_Rethrow) {
1564           mem = in->in(TypeFunc::Memory);
1565         } else if (in_opc == Op_Halt) {
1566           if (in->in(0)->is_Region()) {
1567             Node* r = in->in(0);
1568             for (uint j = 1; j < r->req(); j++) {
1569               assert(!r->in(j)->is_NeverBranch(), "");
1570             }
1571           } else {
1572             Node* proj = in->in(0);
1573             assert(proj->is_Proj(), "");
1574             Node* in = proj->in(0);
1575             assert(in->is_CallStaticJava() || in->is_NeverBranch() || in->Opcode() == Op_Catch || proj->is_IfProj(), "");
1576             if (in->is_CallStaticJava()) {
1577               mem = in->in(TypeFunc::Memory);
1578             } else if (in->Opcode() == Op_Catch) {
1579               Node* call = in->in(0)->in(0);
1580               assert(call->is_Call(), "");
1581               mem = call->in(TypeFunc::Memory);
1582             } else if (in->is_NeverBranch()) {
1583               mem = collect_memory_for_infinite_loop(in);
1584             }
1585           }
1586         } else {
1587 #ifdef ASSERT
1588           n->dump();
1589           in->dump();
1590 #endif
1591           ShouldNotReachHere();
1592         }
1593       } else {
1594         assert(n->is_Phi() && n->bottom_type() == Type::MEMORY, "");
1595         assert(n->adr_type() == TypePtr::BOTTOM || _phase->C->get_alias_index(n->adr_type()) == _alias, "");
1596         mem = n->in(i);
1597       }
1598       i++;
1599       stack.set_index(i);
1600       if (mem == nullptr) {
1601         continue;
1602       }
1603       for (;;) {
1604         if (visited.test_set(mem->_idx) || mem->is_Start()) {
1605           break;
1606         }
1607         if (mem->is_Phi()) {
1608           stack.push(mem, 2);
1609           mem = mem->in(1);
1610         } else if (mem->is_Proj()) {
1611           stack.push(mem, mem->req());
1612           mem = mem->in(0);
1613         } else if (mem->is_SafePoint() || mem->is_MemBar()) {
1614           mem = mem->in(TypeFunc::Memory);
1615         } else if (mem->is_MergeMem()) {
1616           MergeMemNode* mm = mem->as_MergeMem();
1617           mem = mm->memory_at(_alias);
1618         } else if (mem->is_Store() || mem->is_LoadStore() || mem->is_ClearArray()) {
1619           assert(_alias == Compile::AliasIdxRaw, "");
1620           stack.push(mem, mem->req());
1621           mem = mem->in(MemNode::Memory);
1622         } else {
1623 #ifdef ASSERT
1624           mem->dump();
1625 #endif
1626           ShouldNotReachHere();
1627         }
1628       }
1629     } else {
1630       if (n->is_Phi()) {
1631         // Nothing
1632       } else if (!n->is_Root()) {
1633         Node* c = get_ctrl(n);
1634         _memory_nodes.map(c->_idx, n);
1635       }
1636       stack.pop();
1637     }
1638   } while(stack.is_nonempty());
1639 
1640   // Iterate over CFG nodes in rpo and propagate memory state to
1641   // compute memory state at regions, creating new phis if needed.
1642   Node_List rpo_list;
1643   visited.clear();
1644   _phase->rpo(_phase->C->root(), stack, visited, rpo_list);
1645   Node* root = rpo_list.pop();
1646   assert(root == _phase->C->root(), "");
1647 
1648   const bool trace = false;
1649 #ifdef ASSERT
1650   if (trace) {
1651     for (int i = rpo_list.size() - 1; i >= 0; i--) {
1652       Node* c = rpo_list.at(i);
1653       if (_memory_nodes[c->_idx] != nullptr) {
1654         tty->print("X %d", c->_idx);  _memory_nodes[c->_idx]->dump();
1655       }
1656     }
1657   }
1658 #endif
1659   uint last = _phase->C->unique();
1660 
1661 #ifdef ASSERT
1662   uint16_t max_depth = 0;
1663   for (LoopTreeIterator iter(_phase->ltree_root()); !iter.done(); iter.next()) {
1664     IdealLoopTree* lpt = iter.current();
1665     max_depth = MAX2(max_depth, lpt->_nest);
1666   }
1667 #endif
1668 
1669   bool progress = true;
1670   int iteration = 0;
1671   Node_List dead_phis;
1672   while (progress) {
1673     progress = false;
1674     iteration++;
1675     assert(iteration <= 2+max_depth || _phase->C->has_irreducible_loop() || has_never_branch(_phase->C->root()), "");
1676     if (trace) { tty->print_cr("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); }
1677 
1678     for (int i = rpo_list.size() - 1; i >= 0; i--) {
1679       Node* c = rpo_list.at(i);
1680 
1681       Node* prev_mem = _memory_nodes[c->_idx];
1682       if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) {
1683         Node* prev_region = regions[c->_idx];
1684         Node* unique = nullptr;
1685         for (uint j = 1; j < c->req() && unique != NodeSentinel; j++) {
1686           Node* m = _memory_nodes[c->in(j)->_idx];
1687           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");
1688           if (m != nullptr) {
1689             if (m == prev_region && ((c->is_Loop() && j == LoopNode::LoopBackControl) || (prev_region->is_Phi() && prev_region->in(0) == c))) {
1690               assert((c->is_Loop() && j == LoopNode::LoopBackControl) || _phase->C->has_irreducible_loop() || has_never_branch(_phase->C->root()), "");
1691               // continue
1692             } else if (unique == nullptr) {
1693               unique = m;
1694             } else if (m == unique) {
1695               // continue
1696             } else {
1697               unique = NodeSentinel;
1698             }
1699           }
1700         }
1701         assert(unique != nullptr, "empty phi???");
1702         if (unique != NodeSentinel) {
1703           if (prev_region != nullptr && prev_region->is_Phi() && prev_region->in(0) == c) {
1704             dead_phis.push(prev_region);
1705           }
1706           regions.map(c->_idx, unique);
1707         } else {
1708           Node* phi = nullptr;
1709           if (prev_region != nullptr && prev_region->is_Phi() && prev_region->in(0) == c && prev_region->_idx >= last) {
1710             phi = prev_region;
1711             for (uint k = 1; k < c->req(); k++) {
1712               Node* m = _memory_nodes[c->in(k)->_idx];
1713               assert(m != nullptr, "expect memory state");
1714               phi->set_req(k, m);
1715             }
1716           } else {
1717             for (DUIterator_Fast jmax, j = c->fast_outs(jmax); j < jmax && phi == nullptr; j++) {
1718               Node* u = c->fast_out(j);
1719               if (u->is_Phi() && u->bottom_type() == Type::MEMORY &&
1720                   (u->adr_type() == TypePtr::BOTTOM || _phase->C->get_alias_index(u->adr_type()) == _alias)) {
1721                 phi = u;
1722                 for (uint k = 1; k < c->req() && phi != nullptr; k++) {
1723                   Node* m = _memory_nodes[c->in(k)->_idx];
1724                   assert(m != nullptr, "expect memory state");
1725                   if (u->in(k) != m) {
1726                     phi = NodeSentinel;
1727                   }
1728                 }
1729               }
1730             }
1731             if (phi == NodeSentinel) {
1732               phi = new PhiNode(c, Type::MEMORY, _phase->C->get_adr_type(_alias));
1733               for (uint k = 1; k < c->req(); k++) {
1734                 Node* m = _memory_nodes[c->in(k)->_idx];
1735                 assert(m != nullptr, "expect memory state");
1736                 phi->init_req(k, m);
1737               }
1738             }
1739           }
1740           if (phi != nullptr) {
1741             regions.map(c->_idx, phi);
1742           } else {
1743             assert(c->unique_ctrl_out()->Opcode() == Op_Halt, "expected memory state");
1744           }
1745         }
1746         Node* current_region = regions[c->_idx];
1747         if (current_region != prev_region) {
1748           progress = true;
1749           if (prev_region == prev_mem) {
1750             _memory_nodes.map(c->_idx, current_region);
1751           }
1752         }
1753       } else if (prev_mem == nullptr || prev_mem->is_Phi() || ctrl_or_self(prev_mem) != c) {
1754         Node* m = _memory_nodes[_phase->idom(c)->_idx];
1755         assert(m != nullptr || c->Opcode() == Op_Halt, "expect memory state");
1756         if (m != prev_mem) {
1757           _memory_nodes.map(c->_idx, m);
1758           progress = true;
1759         }
1760       }
1761 #ifdef ASSERT
1762       if (trace) { tty->print("X %d", c->_idx);  _memory_nodes[c->_idx]->dump(); }
1763 #endif
1764     }
1765   }
1766 
1767   // Replace existing phi with computed memory state for that region
1768   // if different (could be a new phi or a dominating memory node if
1769   // that phi was found to be useless).
1770   while (dead_phis.size() > 0) {
1771     Node* n = dead_phis.pop();
1772     n->replace_by(_phase->C->top());
1773     n->destruct(&_phase->igvn());
1774   }
1775   for (int i = rpo_list.size() - 1; i >= 0; i--) {
1776     Node* c = rpo_list.at(i);
1777     if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) {
1778       Node* n = regions[c->_idx];
1779       assert(n != nullptr || c->unique_ctrl_out()->Opcode() == Op_Halt, "expected memory state");
1780       if (n != nullptr && n->is_Phi() && n->_idx >= last && n->in(0) == c) {
1781         _phase->register_new_node(n, c);
1782       }
1783     }
1784   }
1785   for (int i = rpo_list.size() - 1; i >= 0; i--) {
1786     Node* c = rpo_list.at(i);
1787     if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) {
1788       Node* n = regions[c->_idx];
1789       assert(n != nullptr || c->unique_ctrl_out()->Opcode() == Op_Halt, "expected memory state");
1790       for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax; i++) {
1791         Node* u = c->fast_out(i);
1792         if (u->is_Phi() && u->bottom_type() == Type::MEMORY &&
1793             u != n) {
1794           assert(c->unique_ctrl_out()->Opcode() != Op_Halt, "expected memory state");
1795           if (u->adr_type() == TypePtr::BOTTOM) {
1796             fix_memory_uses(u, n, n, c);
1797           } else if (_phase->C->get_alias_index(u->adr_type()) == _alias) {
1798             _phase->igvn().replace_node(u, n);
1799             --i; --imax;
1800           }
1801         }
1802       }
1803     }
1804   }
1805 }
1806 
1807 Node* MemoryGraphFixer::collect_memory_for_infinite_loop(const Node* in) {
1808   Node* mem = nullptr;
1809   Node* head = in->in(0);
1810   assert(head->is_Region(), "unexpected infinite loop graph shape");
1811 
1812   Node* phi_mem = nullptr;
1813   for (DUIterator_Fast jmax, j = head->fast_outs(jmax); j < jmax; j++) {
1814     Node* u = head->fast_out(j);
1815     if (u->is_Phi() && u->bottom_type() == Type::MEMORY) {
1816       if (_phase->C->get_alias_index(u->adr_type()) == _alias) {
1817         assert(phi_mem == nullptr || phi_mem->adr_type() == TypePtr::BOTTOM, "");
1818         phi_mem = u;
1819       } else if (u->adr_type() == TypePtr::BOTTOM) {
1820         assert(phi_mem == nullptr || _phase->C->get_alias_index(phi_mem->adr_type()) == _alias, "");
1821         if (phi_mem == nullptr) {
1822           phi_mem = u;
1823         }
1824       }
1825     }
1826   }
1827   if (phi_mem == nullptr) {
1828     ResourceMark rm;
1829     Node_Stack stack(0);
1830     stack.push(head, 1);
1831     do {
1832       Node* n = stack.node();
1833       uint i = stack.index();
1834       if (i >= n->req()) {
1835         stack.pop();
1836       } else {
1837         stack.set_index(i + 1);
1838         Node* c = n->in(i);
1839         assert(c != head, "should have found a safepoint on the way");
1840         if (stack.size() != 1 || _phase->is_dominator(head, c)) {
1841           for (;;) {
1842             if (c->is_Region()) {
1843               stack.push(c, 1);
1844               break;
1845             } else if (c->is_SafePoint() && !c->is_CallLeaf()) {
1846               Node* m = c->in(TypeFunc::Memory);
1847               if (m->is_MergeMem()) {
1848                 m = m->as_MergeMem()->memory_at(_alias);
1849               }
1850               assert(mem == nullptr || mem == m, "several memory states");
1851               mem = m;
1852               break;
1853             } else {
1854               assert(c != c->in(0), "");
1855               c = c->in(0);
1856             }
1857           }
1858         }
1859       }
1860     } while (stack.size() > 0);
1861     assert(mem != nullptr, "should have found safepoint");
1862   } else {
1863     mem = phi_mem;
1864   }
1865   return mem;
1866 }
1867 
1868 Node* MemoryGraphFixer::get_ctrl(Node* n) const {
1869   Node* c = _phase->get_ctrl(n);
1870   if (n->is_Proj() && n->in(0) != nullptr && n->in(0)->is_Call()) {
1871     assert(c == n->in(0), "");
1872     CallNode* call = c->as_Call();
1873     CallProjections* projs = call->extract_projections(true, false);
1874     if (projs->catchall_memproj != nullptr) {
1875       if (projs->fallthrough_memproj == n) {
1876         c = projs->fallthrough_catchproj;

1877       } else {
1878         assert(projs->catchall_memproj == n, "");
1879         c = projs->catchall_catchproj;
1880       }
1881     }
1882   }
1883   return c;
1884 }
1885 
1886 Node* MemoryGraphFixer::ctrl_or_self(Node* n) const {
1887   if (_phase->has_ctrl(n))
1888     return get_ctrl(n);
1889   else {
1890     assert (n->is_CFG(), "must be a CFG node");
1891     return n;
1892   }
1893 }
1894 
1895 bool MemoryGraphFixer::mem_is_valid(Node* m, Node* c) const {
1896   return m != nullptr && get_ctrl(m) == c;
1897 }
1898 
1899 Node* MemoryGraphFixer::find_mem(Node* ctrl, Node* n) const {
1900   assert(n == nullptr || _phase->ctrl_or_self(n) == ctrl, "");
1901   assert(!ctrl->is_Call() || ctrl == n, "projection expected");
1902 #ifdef ASSERT
1903   if ((ctrl->is_Proj() && ctrl->in(0)->is_Call()) ||
1904       (ctrl->is_Catch() && ctrl->in(0)->in(0)->is_Call())) {
1905     CallNode* call = ctrl->is_Proj() ? ctrl->in(0)->as_Call() : ctrl->in(0)->in(0)->as_Call();
1906     int mems = 0;
1907     for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) {
1908       Node* u = call->fast_out(i);
1909       if (u->bottom_type() == Type::MEMORY) {
1910         mems++;
1911       }
1912     }
1913     assert(mems <= 1, "No node right after call if multiple mem projections");
1914   }
1915 #endif
1916   Node* mem = _memory_nodes[ctrl->_idx];
1917   Node* c = ctrl;
1918   while (!mem_is_valid(mem, c) &&
1919          (!c->is_CatchProj() || mem == nullptr || c->in(0)->in(0)->in(0) != get_ctrl(mem))) {
1920     c = _phase->idom(c);
1921     mem = _memory_nodes[c->_idx];
1922   }
1923   if (n != nullptr && mem_is_valid(mem, c)) {
1924     while (!ShenandoahBarrierC2Support::is_dominator_same_ctrl(c, mem, n, _phase) && _phase->ctrl_or_self(mem) == ctrl) {
1925       mem = next_mem(mem, _alias);
1926     }
1927     if (mem->is_MergeMem()) {
1928       mem = mem->as_MergeMem()->memory_at(_alias);
1929     }
1930     if (!mem_is_valid(mem, c)) {
1931       do {
1932         c = _phase->idom(c);
1933         mem = _memory_nodes[c->_idx];
1934       } while (!mem_is_valid(mem, c) &&
1935                (!c->is_CatchProj() || mem == nullptr || c->in(0)->in(0)->in(0) != get_ctrl(mem)));
1936     }
1937   }
1938   assert(mem->bottom_type() == Type::MEMORY, "");
1939   return mem;
1940 }
1941 
1942 bool MemoryGraphFixer::has_mem_phi(Node* region) const {
1943   for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
1944     Node* use = region->fast_out(i);
1945     if (use->is_Phi() && use->bottom_type() == Type::MEMORY &&
1946         (_phase->C->get_alias_index(use->adr_type()) == _alias)) {
1947       return true;
1948     }
1949   }
1950   return false;
1951 }
1952 
1953 void MemoryGraphFixer::fix_mem(Node* ctrl, Node* new_ctrl, Node* mem, Node* mem_for_ctrl, Node* new_mem, Unique_Node_List& uses) {
1954   assert(_phase->ctrl_or_self(new_mem) == new_ctrl, "");
1955   const bool trace = false;
1956   DEBUG_ONLY(if (trace) { tty->print("ZZZ control is"); ctrl->dump(); });
1957   DEBUG_ONLY(if (trace) { tty->print("ZZZ mem is"); mem->dump(); });
1958   GrowableArray<Node*> phis;
1959   if (mem_for_ctrl != mem) {
1960     Node* old = mem_for_ctrl;
1961     Node* prev = nullptr;
1962     while (old != mem) {
1963       prev = old;
1964       if (old->is_Store() || old->is_ClearArray() || old->is_LoadStore()) {
1965         assert(_alias == Compile::AliasIdxRaw, "");
1966         old = old->in(MemNode::Memory);
1967       } else if (old->Opcode() == Op_SCMemProj) {
1968         assert(_alias == Compile::AliasIdxRaw, "");
1969         old = old->in(0);
1970       } else {
1971         ShouldNotReachHere();
1972       }
1973     }
1974     assert(prev != nullptr, "");
1975     if (new_ctrl != ctrl) {
1976       _memory_nodes.map(ctrl->_idx, mem);
1977       _memory_nodes.map(new_ctrl->_idx, mem_for_ctrl);
1978     }
1979     uint input = (uint)MemNode::Memory;
1980     _phase->igvn().replace_input_of(prev, input, new_mem);
1981   } else {
1982     uses.clear();
1983     _memory_nodes.map(new_ctrl->_idx, new_mem);
1984     uses.push(new_ctrl);
1985     for(uint next = 0; next < uses.size(); next++ ) {
1986       Node *n = uses.at(next);
1987       assert(n->is_CFG(), "");
1988       DEBUG_ONLY(if (trace) { tty->print("ZZZ ctrl"); n->dump(); });
1989       for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1990         Node* u = n->fast_out(i);
1991         if (!u->is_Root() && u->is_CFG() && u != n) {
1992           Node* m = _memory_nodes[u->_idx];
1993           if (u->is_Region() && (!u->is_OuterStripMinedLoop() || _include_lsm) &&
1994               !has_mem_phi(u) &&
1995               u->unique_ctrl_out()->Opcode() != Op_Halt) {
1996             DEBUG_ONLY(if (trace) { tty->print("ZZZ region"); u->dump(); });
1997             DEBUG_ONLY(if (trace && m != nullptr) { tty->print("ZZZ mem"); m->dump(); });
1998 
1999             if (!mem_is_valid(m, u) || !m->is_Phi()) {
2000               bool push = true;
2001               bool create_phi = true;
2002               if (_phase->is_dominator(new_ctrl, u)) {
2003                 create_phi = false;
2004               }
2005               if (create_phi) {
2006                 Node* phi = new PhiNode(u, Type::MEMORY, _phase->C->get_adr_type(_alias));
2007                 _phase->register_new_node(phi, u);
2008                 phis.push(phi);
2009                 DEBUG_ONLY(if (trace) { tty->print("ZZZ new phi"); phi->dump(); });
2010                 if (!mem_is_valid(m, u)) {
2011                   DEBUG_ONLY(if (trace) { tty->print("ZZZ setting mem"); phi->dump(); });
2012                   _memory_nodes.map(u->_idx, phi);
2013                 } else {
2014                   DEBUG_ONLY(if (trace) { tty->print("ZZZ NOT setting mem"); m->dump(); });
2015                   for (;;) {
2016                     assert(m->is_Mem() || m->is_LoadStore() || m->is_Proj(), "");
2017                     Node* next = nullptr;
2018                     if (m->is_Proj()) {
2019                       next = m->in(0);
2020                     } else {
2021                       assert(m->is_Mem() || m->is_LoadStore(), "");
2022                       assert(_alias == Compile::AliasIdxRaw, "");
2023                       next = m->in(MemNode::Memory);
2024                     }
2025                     if (_phase->get_ctrl(next) != u) {
2026                       break;
2027                     }
2028                     if (next->is_MergeMem()) {
2029                       assert(_phase->get_ctrl(next->as_MergeMem()->memory_at(_alias)) != u, "");
2030                       break;
2031                     }
2032                     if (next->is_Phi()) {
2033                       assert(next->adr_type() == TypePtr::BOTTOM && next->in(0) == u, "");
2034                       break;
2035                     }
2036                     m = next;
2037                   }
2038 
2039                   DEBUG_ONLY(if (trace) { tty->print("ZZZ setting to phi"); m->dump(); });
2040                   assert(m->is_Mem() || m->is_LoadStore(), "");
2041                   uint input = (uint)MemNode::Memory;
2042                   _phase->igvn().replace_input_of(m, input, phi);
2043                   push = false;
2044                 }
2045               } else {
2046                 DEBUG_ONLY(if (trace) { tty->print("ZZZ skipping region"); u->dump(); });
2047               }
2048               if (push) {
2049                 uses.push(u);
2050               }
2051             }
2052           } else if (!mem_is_valid(m, u) &&
2053                      !(u->Opcode() == Op_CProj && u->in(0)->is_NeverBranch() && u->as_Proj()->_con == 1)) {
2054             uses.push(u);
2055           }
2056         }
2057       }
2058     }
2059     for (int i = 0; i < phis.length(); i++) {
2060       Node* n = phis.at(i);
2061       Node* r = n->in(0);
2062       DEBUG_ONLY(if (trace) { tty->print("ZZZ fixing new phi"); n->dump(); });
2063       for (uint j = 1; j < n->req(); j++) {
2064         Node* m = find_mem(r->in(j), nullptr);
2065         _phase->igvn().replace_input_of(n, j, m);
2066         DEBUG_ONLY(if (trace) { tty->print("ZZZ fixing new phi: %d", j); m->dump(); });
2067       }
2068     }
2069   }
2070   uint last = _phase->C->unique();
2071   MergeMemNode* mm = nullptr;
2072   int alias = _alias;
2073   DEBUG_ONLY(if (trace) { tty->print("ZZZ raw mem is"); mem->dump(); });
2074   // Process loads first to not miss an anti-dependency: if the memory
2075   // edge of a store is updated before a load is processed then an
2076   // anti-dependency may be missed.
2077   for (DUIterator i = mem->outs(); mem->has_out(i); i++) {
2078     Node* u = mem->out(i);
2079     if (u->_idx < last && u->is_Load() && _phase->C->get_alias_index(u->adr_type()) == alias) {
2080       Node* m = find_mem(_phase->get_ctrl(u), u);
2081       if (m != mem) {
2082         DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); });
2083         _phase->igvn().replace_input_of(u, MemNode::Memory, m);
2084         --i;
2085       }
2086     }
2087   }
2088   for (DUIterator i = mem->outs(); mem->has_out(i); i++) {
2089     Node* u = mem->out(i);
2090     if (u->_idx < last) {
2091       if (u->is_Mem()) {
2092         if (_phase->C->get_alias_index(u->adr_type()) == alias) {
2093           Node* m = find_mem(_phase->get_ctrl(u), u);
2094           if (m != mem) {
2095             DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); });
2096             _phase->igvn().replace_input_of(u, MemNode::Memory, m);
2097             --i;
2098           }
2099         }
2100       } else if (u->is_MergeMem()) {
2101         MergeMemNode* u_mm = u->as_MergeMem();
2102         if (u_mm->memory_at(alias) == mem) {
2103           MergeMemNode* newmm = nullptr;
2104           for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) {
2105             Node* uu = u->fast_out(j);
2106             assert(!uu->is_MergeMem(), "chain of MergeMems?");
2107             if (uu->is_Phi()) {
2108               assert(uu->adr_type() == TypePtr::BOTTOM, "");
2109               Node* region = uu->in(0);
2110               int nb = 0;
2111               for (uint k = 1; k < uu->req(); k++) {
2112                 if (uu->in(k) == u) {
2113                   Node* m = find_mem(region->in(k), nullptr);
2114                   if (m != mem) {
2115                     DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of phi %d", k); uu->dump(); });
2116                     newmm = clone_merge_mem(u, mem, m, _phase->ctrl_or_self(m), i);
2117                     if (newmm != u) {
2118                       _phase->igvn().replace_input_of(uu, k, newmm);
2119                       nb++;
2120                       --jmax;
2121                     }
2122                   }
2123                 }
2124               }
2125               if (nb > 0) {
2126                 --j;
2127               }
2128             } else {
2129               Node* m = find_mem(_phase->ctrl_or_self(uu), uu);
2130               if (m != mem) {
2131                 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); uu->dump(); });
2132                 newmm = clone_merge_mem(u, mem, m, _phase->ctrl_or_self(m), i);
2133                 if (newmm != u) {
2134                   _phase->igvn().replace_input_of(uu, uu->find_edge(u), newmm);
2135                   --j, --jmax;
2136                 }
2137               }
2138             }
2139           }
2140         }
2141       } else if (u->is_Phi()) {
2142         assert(u->bottom_type() == Type::MEMORY, "what else?");
2143         if (_phase->C->get_alias_index(u->adr_type()) == alias || u->adr_type() == TypePtr::BOTTOM) {
2144           Node* region = u->in(0);
2145           bool replaced = false;
2146           for (uint j = 1; j < u->req(); j++) {
2147             if (u->in(j) == mem) {
2148               Node* m = find_mem(region->in(j), nullptr);
2149               Node* nnew = m;
2150               if (m != mem) {
2151                 if (u->adr_type() == TypePtr::BOTTOM) {
2152                   mm = allocate_merge_mem(mem, m, _phase->ctrl_or_self(m));
2153                   nnew = mm;
2154                 }
2155                 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of phi %d", j); u->dump(); });
2156                 _phase->igvn().replace_input_of(u, j, nnew);
2157                 replaced = true;
2158               }
2159             }
2160           }
2161           if (replaced) {
2162             --i;
2163           }
2164         }
2165       } else if ((u->adr_type() == TypePtr::BOTTOM && u->Opcode() != Op_StrInflatedCopy) ||
2166                  u->adr_type() == nullptr) {
2167         assert(u->adr_type() != nullptr ||
2168                u->Opcode() == Op_Rethrow ||
2169                u->Opcode() == Op_Return ||
2170                u->Opcode() == Op_SafePoint ||
2171                (u->is_CallStaticJava() && u->as_CallStaticJava()->uncommon_trap_request() != 0) ||
2172                (u->is_CallStaticJava() && u->as_CallStaticJava()->_entry_point == OptoRuntime::rethrow_stub()) ||
2173                u->Opcode() == Op_CallLeaf, "");
2174         Node* m = find_mem(_phase->ctrl_or_self(u), u);
2175         if (m != mem) {
2176           mm = allocate_merge_mem(mem, m, _phase->get_ctrl(m));
2177           _phase->igvn().replace_input_of(u, u->find_edge(mem), mm);
2178           --i;
2179         }
2180       } else if (_phase->C->get_alias_index(u->adr_type()) == alias) {
2181         Node* m = find_mem(_phase->ctrl_or_self(u), u);
2182         if (m != mem) {
2183           DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); });
2184           _phase->igvn().replace_input_of(u, u->find_edge(mem), m);
2185           --i;
2186         }
2187       } else if (u->adr_type() != TypePtr::BOTTOM &&
2188                  _memory_nodes[_phase->ctrl_or_self(u)->_idx] == u) {
2189         Node* m = find_mem(_phase->ctrl_or_self(u), u);
2190         assert(m != mem, "");
2191         // u is on the wrong slice...
2192         assert(u->is_ClearArray(), "");
2193         DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); });
2194         _phase->igvn().replace_input_of(u, u->find_edge(mem), m);
2195         --i;
2196       }
2197     }
2198   }
2199 #ifdef ASSERT
2200   assert(new_mem->outcnt() > 0, "");
2201   for (int i = 0; i < phis.length(); i++) {
2202     Node* n = phis.at(i);
2203     assert(n->outcnt() > 0, "new phi must have uses now");
2204   }
2205 #endif
2206 }
2207 
2208 void MemoryGraphFixer::record_new_ctrl(Node* ctrl, Node* new_ctrl, Node* mem, Node* mem_for_ctrl) {
2209   if (mem_for_ctrl != mem && new_ctrl != ctrl) {
2210     _memory_nodes.map(ctrl->_idx, mem);
2211     _memory_nodes.map(new_ctrl->_idx, mem_for_ctrl);
2212   }
2213 }
2214 
2215 MergeMemNode* MemoryGraphFixer::allocate_merge_mem(Node* mem, Node* rep_proj, Node* rep_ctrl) const {
2216   MergeMemNode* mm = MergeMemNode::make(mem);
2217   mm->set_memory_at(_alias, rep_proj);
2218   _phase->register_new_node(mm, rep_ctrl);
2219   return mm;
2220 }
2221 
2222 MergeMemNode* MemoryGraphFixer::clone_merge_mem(Node* u, Node* mem, Node* rep_proj, Node* rep_ctrl, DUIterator& i) const {
2223   MergeMemNode* newmm = nullptr;
2224   MergeMemNode* u_mm = u->as_MergeMem();
2225   Node* c = _phase->get_ctrl(u);
2226   if (_phase->is_dominator(c, rep_ctrl)) {
2227     c = rep_ctrl;
2228   } else {
2229     assert(_phase->is_dominator(rep_ctrl, c), "one must dominate the other");
2230   }
2231   if (u->outcnt() == 1) {
2232     if (u->req() > (uint)_alias && u->in(_alias) == mem) {
2233       _phase->igvn().replace_input_of(u, _alias, rep_proj);
2234       --i;
2235     } else {
2236       _phase->igvn().rehash_node_delayed(u);
2237       u_mm->set_memory_at(_alias, rep_proj);
2238     }
2239     newmm = u_mm;
2240     _phase->set_ctrl_and_loop(u, c);
2241   } else {
2242     // can't simply clone u and then change one of its input because
2243     // it adds and then removes an edge which messes with the
2244     // DUIterator
2245     newmm = MergeMemNode::make(u_mm->base_memory());
2246     for (uint j = 0; j < u->req(); j++) {
2247       if (j < newmm->req()) {
2248         if (j == (uint)_alias) {
2249           newmm->set_req(j, rep_proj);
2250         } else if (newmm->in(j) != u->in(j)) {
2251           newmm->set_req(j, u->in(j));
2252         }
2253       } else if (j == (uint)_alias) {
2254         newmm->add_req(rep_proj);
2255       } else {
2256         newmm->add_req(u->in(j));
2257       }
2258     }
2259     if ((uint)_alias >= u->req()) {
2260       newmm->set_memory_at(_alias, rep_proj);
2261     }
2262     _phase->register_new_node(newmm, c);
2263   }
2264   return newmm;
2265 }
2266 
2267 bool MemoryGraphFixer::should_process_phi(Node* phi) const {
2268   if (phi->adr_type() == TypePtr::BOTTOM) {
2269     Node* region = phi->in(0);
2270     for (DUIterator_Fast jmax, j = region->fast_outs(jmax); j < jmax; j++) {
2271       Node* uu = region->fast_out(j);
2272       if (uu->is_Phi() && uu != phi && uu->bottom_type() == Type::MEMORY && _phase->C->get_alias_index(uu->adr_type()) == _alias) {
2273         return false;
2274       }
2275     }
2276     return true;
2277   }
2278   return _phase->C->get_alias_index(phi->adr_type()) == _alias;
2279 }
2280 
2281 void MemoryGraphFixer::fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl) const {
2282   uint last = _phase-> C->unique();
2283   MergeMemNode* mm = nullptr;
2284   assert(mem->bottom_type() == Type::MEMORY, "");
2285   for (DUIterator i = mem->outs(); mem->has_out(i); i++) {
2286     Node* u = mem->out(i);
2287     if (u != replacement && u->_idx < last) {
2288       if (u->is_MergeMem()) {
2289         MergeMemNode* u_mm = u->as_MergeMem();
2290         if (u_mm->memory_at(_alias) == mem) {
2291           MergeMemNode* newmm = nullptr;
2292           for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) {
2293             Node* uu = u->fast_out(j);
2294             assert(!uu->is_MergeMem(), "chain of MergeMems?");
2295             if (uu->is_Phi()) {
2296               if (should_process_phi(uu)) {
2297                 Node* region = uu->in(0);
2298                 int nb = 0;
2299                 for (uint k = 1; k < uu->req(); k++) {
2300                   if (uu->in(k) == u && _phase->is_dominator(rep_ctrl, region->in(k))) {
2301                     if (newmm == nullptr) {
2302                       newmm = clone_merge_mem(u, mem, rep_proj, rep_ctrl, i);
2303                     }
2304                     if (newmm != u) {
2305                       _phase->igvn().replace_input_of(uu, k, newmm);
2306                       nb++;
2307                       --jmax;
2308                     }
2309                   }
2310                 }
2311                 if (nb > 0) {
2312                   --j;
2313                 }
2314               }
2315             } else {
2316               if (rep_ctrl != uu && ShenandoahBarrierC2Support::is_dominator(rep_ctrl, _phase->ctrl_or_self(uu), replacement, uu, _phase)) {
2317                 if (newmm == nullptr) {
2318                   newmm = clone_merge_mem(u, mem, rep_proj, rep_ctrl, i);
2319                 }
2320                 if (newmm != u) {
2321                   _phase->igvn().replace_input_of(uu, uu->find_edge(u), newmm);
2322                   --j, --jmax;
2323                 }
2324               }
2325             }
2326           }
2327         }
2328       } else if (u->is_Phi()) {
2329         assert(u->bottom_type() == Type::MEMORY, "what else?");
2330         Node* region = u->in(0);
2331         if (should_process_phi(u)) {
2332           bool replaced = false;
2333           for (uint j = 1; j < u->req(); j++) {
2334             if (u->in(j) == mem && _phase->is_dominator(rep_ctrl, region->in(j))) {
2335               Node* nnew = rep_proj;
2336               if (u->adr_type() == TypePtr::BOTTOM) {
2337                 if (mm == nullptr) {
2338                   mm = allocate_merge_mem(mem, rep_proj, rep_ctrl);
2339                 }
2340                 nnew = mm;
2341               }
2342               _phase->igvn().replace_input_of(u, j, nnew);
2343               replaced = true;
2344             }
2345           }
2346           if (replaced) {
2347             --i;
2348           }
2349 
2350         }
2351       } else if ((u->adr_type() == TypePtr::BOTTOM && u->Opcode() != Op_StrInflatedCopy) ||
2352                  u->adr_type() == nullptr) {
2353         assert(u->adr_type() != nullptr ||
2354                u->Opcode() == Op_Rethrow ||
2355                u->Opcode() == Op_Return ||
2356                u->Opcode() == Op_SafePoint ||
2357                (u->is_CallStaticJava() && u->as_CallStaticJava()->uncommon_trap_request() != 0) ||
2358                (u->is_CallStaticJava() && u->as_CallStaticJava()->_entry_point == OptoRuntime::rethrow_stub()) ||
2359                u->Opcode() == Op_CallLeaf, "%s", u->Name());
2360         if (ShenandoahBarrierC2Support::is_dominator(rep_ctrl, _phase->ctrl_or_self(u), replacement, u, _phase)) {
2361           if (mm == nullptr) {
2362             mm = allocate_merge_mem(mem, rep_proj, rep_ctrl);
2363           }
2364           _phase->igvn().replace_input_of(u, u->find_edge(mem), mm);
2365           --i;
2366         }
2367       } else if (_phase->C->get_alias_index(u->adr_type()) == _alias) {
2368         if (ShenandoahBarrierC2Support::is_dominator(rep_ctrl, _phase->ctrl_or_self(u), replacement, u, _phase)) {
2369           _phase->igvn().replace_input_of(u, u->find_edge(mem), rep_proj);
2370           --i;
2371         }
2372       }
2373     }
2374   }
2375 }
2376 
2377 ShenandoahLoadReferenceBarrierNode::ShenandoahLoadReferenceBarrierNode(Node* ctrl, Node* obj, DecoratorSet decorators)
2378 : Node(ctrl, obj), _decorators(decorators) {
2379   ShenandoahBarrierSetC2::bsc2()->state()->add_load_reference_barrier(this);
2380 }
2381 
2382 DecoratorSet ShenandoahLoadReferenceBarrierNode::decorators() const {
2383   return _decorators;
2384 }
2385 
2386 uint ShenandoahLoadReferenceBarrierNode::size_of() const {
2387   return sizeof(*this);
2388 }
2389 
2390 static DecoratorSet mask_decorators(DecoratorSet decorators) {
2391   return decorators & (ON_STRONG_OOP_REF | ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF | ON_UNKNOWN_OOP_REF | IN_NATIVE);
2392 }
2393 
2394 uint ShenandoahLoadReferenceBarrierNode::hash() const {
2395   uint hash = Node::hash();
2396   hash += mask_decorators(_decorators);
2397   return hash;
2398 }
2399 
2400 bool ShenandoahLoadReferenceBarrierNode::cmp( const Node &n ) const {
2401   return Node::cmp(n) && n.Opcode() == Op_ShenandoahLoadReferenceBarrier &&
2402          mask_decorators(_decorators) == mask_decorators(((const ShenandoahLoadReferenceBarrierNode&)n)._decorators);
2403 }
2404 
2405 const Type* ShenandoahLoadReferenceBarrierNode::bottom_type() const {
2406   if (in(ValueIn) == nullptr || in(ValueIn)->is_top()) {
2407     return Type::TOP;
2408   }
2409   const Type* t = in(ValueIn)->bottom_type();
2410   if (t == TypePtr::NULL_PTR) {
2411     return t;
2412   }
2413 
2414   if (ShenandoahBarrierSet::is_strong_access(decorators())) {
2415     return t;
2416   }
2417 
2418   return t->meet(TypePtr::NULL_PTR);
2419 }
2420 
2421 const Type* ShenandoahLoadReferenceBarrierNode::Value(PhaseGVN* phase) const {
2422   // Either input is TOP ==> the result is TOP
2423   const Type *t2 = phase->type(in(ValueIn));
2424   if( t2 == Type::TOP ) return Type::TOP;
2425 
2426   if (t2 == TypePtr::NULL_PTR) {
2427     return t2;
2428   }
2429 
2430   if (ShenandoahBarrierSet::is_strong_access(decorators())) {
2431     return t2;
2432   }
2433 
2434   return t2->meet(TypePtr::NULL_PTR);
2435 }
2436 
2437 Node* ShenandoahLoadReferenceBarrierNode::Identity(PhaseGVN* phase) {
2438   Node* value = in(ValueIn);
2439   if (!needs_barrier(phase, value)) {
2440     return value;
2441   }
2442   return this;
2443 }
2444 
2445 bool ShenandoahLoadReferenceBarrierNode::needs_barrier(PhaseGVN* phase, Node* n) {
2446   Unique_Node_List visited;
2447   return needs_barrier_impl(phase, n, visited);
2448 }
2449 
2450 bool ShenandoahLoadReferenceBarrierNode::needs_barrier_impl(PhaseGVN* phase, Node* n, Unique_Node_List &visited) {
2451   if (n == nullptr) return false;
2452   if (visited.member(n)) {
2453     return false; // Been there.
2454   }
2455   visited.push(n);
2456 
2457   if (n->is_Allocate()) {
2458     // tty->print_cr("optimize barrier on alloc");
2459     return false;
2460   }
2461   if (n->is_Call()) {
2462     // tty->print_cr("optimize barrier on call");
2463     return false;
2464   }
2465 
2466   const Type* type = phase->type(n);
2467   if (type == Type::TOP) {
2468     return false;
2469   }
2470   if (type->make_ptr()->higher_equal(TypePtr::NULL_PTR)) {
2471     // tty->print_cr("optimize barrier on null");
2472     return false;
2473   }
2474   if (type->make_oopptr() && type->make_oopptr()->const_oop() != nullptr) {
2475     // tty->print_cr("optimize barrier on constant");
2476     return false;
2477   }
2478 
2479   switch (n->Opcode()) {
2480     case Op_AddP:
2481       return true; // TODO: Can refine?
2482     case Op_LoadP:
2483     case Op_ShenandoahCompareAndExchangeN:
2484     case Op_ShenandoahCompareAndExchangeP:
2485     case Op_CompareAndExchangeN:
2486     case Op_CompareAndExchangeP:
2487     case Op_GetAndSetN:
2488     case Op_GetAndSetP:
2489       return true;
2490     case Op_Phi: {
2491       for (uint i = 1; i < n->req(); i++) {
2492         if (needs_barrier_impl(phase, n->in(i), visited)) return true;
2493       }
2494       return false;
2495     }
2496     case Op_CheckCastPP:
2497     case Op_CastPP:
2498       return needs_barrier_impl(phase, n->in(1), visited);
2499     case Op_Proj:
2500       return needs_barrier_impl(phase, n->in(0), visited);
2501     case Op_ShenandoahLoadReferenceBarrier:
2502       // tty->print_cr("optimize barrier on barrier");
2503       return false;
2504     case Op_Parm:
2505       // tty->print_cr("optimize barrier on input arg");
2506       return false;
2507     case Op_DecodeN:
2508     case Op_EncodeP:
2509       return needs_barrier_impl(phase, n->in(1), visited);
2510     case Op_LoadN:
2511       return true;
2512     case Op_CMoveN:
2513     case Op_CMoveP:
2514       return needs_barrier_impl(phase, n->in(2), visited) ||
2515              needs_barrier_impl(phase, n->in(3), visited);
2516     case Op_CreateEx:
2517       return false;
2518     default:
2519       break;
2520   }
2521 #ifdef ASSERT
2522   tty->print("need barrier on?: ");
2523   tty->print_cr("ins:");
2524   n->dump(2);
2525   tty->print_cr("outs:");
2526   n->dump(-2);
2527   ShouldNotReachHere();
2528 #endif
2529   return true;
2530 }
--- EOF ---