1 /*
   2  * Copyright (c) 2015, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 
  26 #include "gc/shenandoah/c2/shenandoahSupport.hpp"
  27 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
  28 #include "gc/shenandoah/brooksPointer.hpp"
  29 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  30 #include "gc/shenandoah/shenandoahHeap.hpp"
  31 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  32 #include "gc/shenandoah/shenandoahRuntime.hpp"
  33 #include "opto/arraycopynode.hpp"
  34 #include "opto/block.hpp"
  35 #include "opto/callnode.hpp"
  36 #include "opto/castnode.hpp"
  37 #include "opto/movenode.hpp"
  38 #include "opto/phaseX.hpp"
  39 #include "opto/rootnode.hpp"
  40 #include "opto/runtime.hpp"
  41 #include "opto/subnode.hpp"
  42 
  43 Node* ShenandoahBarrierNode::skip_through_barrier(Node* n) {
  44   if (n == NULL) {
  45     return NULL;
  46   }
  47   if (n->Opcode() == Op_ShenandoahEnqueueBarrier) {
  48     n = n->in(1);
  49   }
  50 
  51   if (n->is_ShenandoahBarrier()) {
  52     return n->in(ValueIn);
  53   } else if (n->is_Phi() &&
  54              n->req() == 3 &&
  55              n->in(1) != NULL &&
  56              n->in(1)->is_ShenandoahBarrier() &&
  57              n->in(2) != NULL &&
  58              n->in(2)->bottom_type() == TypePtr::NULL_PTR &&
  59              n->in(0) != NULL &&
  60              n->in(0)->in(1) != NULL &&
  61              n->in(0)->in(1)->is_IfProj() &&
  62              n->in(0)->in(2) != NULL &&
  63              n->in(0)->in(2)->is_IfProj() &&
  64              n->in(0)->in(1)->in(0) != NULL &&
  65              n->in(0)->in(1)->in(0) == n->in(0)->in(2)->in(0) &&
  66              n->in(1)->in(ValueIn)->Opcode() == Op_CastPP) {
  67     Node* iff = n->in(0)->in(1)->in(0);
  68     Node* res = n->in(1)->in(ValueIn)->in(1);
  69     if (iff->is_If() &&
  70         iff->in(1) != NULL &&
  71         iff->in(1)->is_Bool() &&
  72         iff->in(1)->as_Bool()->_test._test == BoolTest::ne &&
  73         iff->in(1)->in(1) != NULL &&
  74         iff->in(1)->in(1)->Opcode() == Op_CmpP &&
  75         iff->in(1)->in(1)->in(1) != NULL &&
  76         iff->in(1)->in(1)->in(1) == res &&
  77         iff->in(1)->in(1)->in(2) != NULL &&
  78         iff->in(1)->in(1)->in(2)->bottom_type() == TypePtr::NULL_PTR) {
  79       return res;
  80     }
  81   }
  82   return n;
  83 }
  84 
  85 bool ShenandoahBarrierNode::needs_barrier(PhaseGVN* phase, ShenandoahBarrierNode* orig, Node* n, Node* rb_mem, bool allow_fromspace) {
  86   Unique_Node_List visited;
  87   return needs_barrier_impl(phase, orig, n, rb_mem, allow_fromspace, visited);
  88 }
  89 
  90 bool ShenandoahBarrierNode::needs_barrier_impl(PhaseGVN* phase, ShenandoahBarrierNode* orig, Node* n, Node* rb_mem, bool allow_fromspace, Unique_Node_List &visited) {
  91   if (visited.member(n)) {
  92     return false; // Been there.
  93   }
  94   visited.push(n);
  95 
  96   if (n->is_Allocate()) {
  97     return false;
  98   }
  99 
 100   if (n->is_CallJava() || n->Opcode() == Op_CallLeafNoFP) {
 101     return true;
 102   }
 103 
 104   const Type* type = phase->type(n);
 105   if (type == Type::TOP) {
 106     return false;
 107   }
 108   if (type->make_ptr()->higher_equal(TypePtr::NULL_PTR)) {
 109     return false;
 110   }
 111   if (type->make_oopptr() && type->make_oopptr()->const_oop() != NULL) {
 112     return false;
 113   }
 114 
 115   if (ShenandoahOptimizeStableFinals) {
 116     const TypeAryPtr* ary = type->isa_aryptr();
 117     if (ary && ary->is_stable() && allow_fromspace) {
 118       return false;
 119     }
 120   }
 121 
 122   if (n->is_CheckCastPP() || n->is_ConstraintCast() || n->Opcode() == Op_ShenandoahEnqueueBarrier) {
 123     return needs_barrier_impl(phase, orig, n->in(1), rb_mem, allow_fromspace, visited);
 124   }
 125   if (n->is_Parm()) {
 126     return true;
 127   }
 128   if (n->is_Proj()) {
 129     return needs_barrier_impl(phase, orig, n->in(0), rb_mem, allow_fromspace, visited);
 130   }
 131 
 132   if (n->Opcode() == Op_ShenandoahWBMemProj) {
 133     return needs_barrier_impl(phase, orig, n->in(ShenandoahWBMemProjNode::WriteBarrier), rb_mem, allow_fromspace, visited);
 134   }
 135   if (n->is_Phi()) {
 136     bool need_barrier = false;
 137     for (uint i = 1; i < n->req() && ! need_barrier; i++) {
 138       Node* input = n->in(i);
 139       if (input == NULL) {
 140         need_barrier = true; // Phi not complete yet?
 141       } else if (needs_barrier_impl(phase, orig, input, rb_mem, allow_fromspace, visited)) {
 142         need_barrier = true;
 143       }
 144     }
 145     return need_barrier;
 146   }
 147   if (n->is_CMove()) {
 148     return needs_barrier_impl(phase, orig, n->in(CMoveNode::IfFalse), rb_mem, allow_fromspace, visited) ||
 149            needs_barrier_impl(phase, orig, n->in(CMoveNode::IfTrue ), rb_mem, allow_fromspace, visited);
 150   }
 151   if (n->Opcode() == Op_CreateEx) {
 152     return true;
 153   }
 154   if (n->Opcode() == Op_ShenandoahWriteBarrier) {
 155     return false;
 156   }
 157   if (n->Opcode() == Op_ShenandoahReadBarrier) {
 158     if (rb_mem == n->in(Memory)) {
 159       return false;
 160     } else {
 161       return true;
 162     }
 163   }
 164 
 165   if (n->Opcode() == Op_LoadP ||
 166       n->Opcode() == Op_LoadN ||
 167       n->Opcode() == Op_GetAndSetP ||
 168       n->Opcode() == Op_CompareAndExchangeP ||
 169       n->Opcode() == Op_ShenandoahCompareAndExchangeP ||
 170       n->Opcode() == Op_GetAndSetN ||
 171       n->Opcode() == Op_CompareAndExchangeN ||
 172       n->Opcode() == Op_ShenandoahCompareAndExchangeN) {
 173     return true;
 174   }
 175   if (n->Opcode() == Op_DecodeN ||
 176       n->Opcode() == Op_EncodeP) {
 177     return needs_barrier_impl(phase, orig, n->in(1), rb_mem, allow_fromspace, visited);
 178   }
 179 
 180 #ifdef ASSERT
 181   tty->print("need barrier on?: "); n->dump();
 182   ShouldNotReachHere();
 183 #endif
 184   return true;
 185 }
 186 
 187 bool ShenandoahReadBarrierNode::dominates_memory_rb_impl(PhaseGVN* phase,
 188                                                          Node* b1,
 189                                                          Node* b2,
 190                                                          Node* current,
 191                                                          bool linear) {
 192   ResourceMark rm;
 193   VectorSet visited(Thread::current()->resource_area());
 194   Node_Stack phis(0);
 195 
 196   for(int i = 0; i < 10; i++) {
 197     if (current == NULL) {
 198       return false;
 199     } else if (visited.test_set(current->_idx) || current->is_top() || current == b1) {
 200       current = NULL;
 201       while (phis.is_nonempty() && current == NULL) {
 202         uint idx = phis.index();
 203         Node* phi = phis.node();
 204         if (idx >= phi->req()) {
 205           phis.pop();
 206         } else {
 207           current = phi->in(idx);
 208           phis.set_index(idx+1);
 209         }
 210       }
 211       if (current == NULL) {
 212         return true;
 213       }
 214     } else if (current == phase->C->immutable_memory()) {
 215       return false;
 216     } else if (current->isa_Phi()) {
 217       if (!linear) {
 218         return false;
 219       }
 220       phis.push(current, 2);
 221       current = current->in(1);
 222     } else if (current->Opcode() == Op_ShenandoahWriteBarrier) {
 223       const Type* in_type = current->bottom_type();
 224       const Type* this_type = b2->bottom_type();
 225       if (is_independent(in_type, this_type)) {
 226         current = current->in(Memory);
 227       } else {
 228         return false;
 229       }
 230     } else if (current->Opcode() == Op_ShenandoahWBMemProj) {
 231       current = current->in(ShenandoahWBMemProjNode::WriteBarrier);
 232     } else if (current->is_Proj()) {
 233       current = current->in(0);
 234     } else if (current->is_Call()) {
 235       return false; // TODO: Maybe improve by looking at the call's memory effects?
 236     } else if (current->is_MemBar()) {
 237       return false; // TODO: Do we need to stop at *any* membar?
 238     } else if (current->is_MergeMem()) {
 239       const TypePtr* adr_type = brooks_pointer_type(phase->type(b2));
 240       uint alias_idx = phase->C->get_alias_index(adr_type);
 241       current = current->as_MergeMem()->memory_at(alias_idx);
 242     } else {
 243 #ifdef ASSERT
 244       current->dump();
 245 #endif
 246       ShouldNotReachHere();
 247       return false;
 248     }
 249   }
 250   return false;
 251 }
 252 
 253 bool ShenandoahReadBarrierNode::is_independent(Node* mem) {
 254   if (mem->is_Phi() || mem->is_Proj() || mem->is_MergeMem()) {
 255     return true;
 256   } else if (mem->Opcode() == Op_ShenandoahWBMemProj) {
 257     return true;
 258   } else if (mem->Opcode() == Op_ShenandoahWriteBarrier) {
 259     const Type* mem_type = mem->bottom_type();
 260     const Type* this_type = bottom_type();
 261     if (is_independent(mem_type, this_type)) {
 262       return true;
 263     } else {
 264       return false;
 265     }
 266   } else if (mem->is_Call() || mem->is_MemBar()) {
 267     return false;
 268   }
 269 #ifdef ASSERT
 270   mem->dump();
 271 #endif
 272   ShouldNotReachHere();
 273   return true;
 274 }
 275 
 276 bool ShenandoahReadBarrierNode::dominates_memory_rb(PhaseGVN* phase, Node* b1, Node* b2, bool linear) {
 277   return dominates_memory_rb_impl(phase, b1->in(Memory), b2, b2->in(Memory), linear);
 278 }
 279 
 280 bool ShenandoahReadBarrierNode::is_independent(const Type* in_type, const Type* this_type) {
 281   assert(in_type->isa_oopptr(), "expect oop ptr");
 282   assert(this_type->isa_oopptr(), "expect oop ptr");
 283 
 284   ciKlass* in_kls = in_type->is_oopptr()->klass();
 285   ciKlass* this_kls = this_type->is_oopptr()->klass();
 286   if (in_kls != NULL && this_kls != NULL &&
 287       in_kls->is_loaded() && this_kls->is_loaded() &&
 288       (!in_kls->is_subclass_of(this_kls)) &&
 289       (!this_kls->is_subclass_of(in_kls))) {
 290     return true;
 291   }
 292   return false;
 293 }
 294 
 295 Node* ShenandoahReadBarrierNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 296   if (! can_reshape) {
 297     return NULL;
 298   }
 299 
 300   if (in(Memory) == phase->C->immutable_memory()) return NULL;
 301 
 302   // If memory input is a MergeMem, take the appropriate slice out of it.
 303   Node* mem_in = in(Memory);
 304   if (mem_in->isa_MergeMem()) {
 305     const TypePtr* adr_type = brooks_pointer_type(bottom_type());
 306     uint alias_idx = phase->C->get_alias_index(adr_type);
 307     mem_in = mem_in->as_MergeMem()->memory_at(alias_idx);
 308     set_req(Memory, mem_in);
 309     return this;
 310   }
 311 
 312   Node* input = in(Memory);
 313   if (input->Opcode() == Op_ShenandoahWBMemProj) {
 314     ResourceMark rm;
 315     VectorSet seen(Thread::current()->resource_area());
 316     Node* n = in(Memory);
 317     while (n->Opcode() == Op_ShenandoahWBMemProj &&
 318            n->in(ShenandoahWBMemProjNode::WriteBarrier) != NULL &&
 319            n->in(ShenandoahWBMemProjNode::WriteBarrier)->Opcode() == Op_ShenandoahWriteBarrier &&
 320            n->in(ShenandoahWBMemProjNode::WriteBarrier)->in(Memory) != NULL) {
 321       if (seen.test_set(n->_idx)) {
 322         return NULL; // loop
 323       }
 324       n = n->in(ShenandoahWBMemProjNode::WriteBarrier)->in(Memory);
 325     }
 326 
 327     Node* wb = input->in(ShenandoahWBMemProjNode::WriteBarrier);
 328     const Type* in_type = phase->type(wb);
 329     // is_top() test not sufficient here: we can come here after CCP
 330     // in a dead branch of the graph that has not yet been removed.
 331     if (in_type == Type::TOP) return NULL; // Dead path.
 332     assert(wb->Opcode() == Op_ShenandoahWriteBarrier, "expect write barrier");
 333     if (is_independent(in_type, _type)) {
 334       phase->igvn_rehash_node_delayed(wb);
 335       set_req(Memory, wb->in(Memory));
 336       if (can_reshape && input->outcnt() == 0) {
 337         phase->is_IterGVN()->_worklist.push(input);
 338       }
 339       return this;
 340     }
 341   }
 342   return NULL;
 343 }
 344 
 345 ShenandoahWriteBarrierNode::ShenandoahWriteBarrierNode(Compile* C, Node* ctrl, Node* mem, Node* obj)
 346   : ShenandoahBarrierNode(ctrl, mem, obj, false) {
 347   assert(UseShenandoahGC && ShenandoahWriteBarrier, "should be enabled");
 348   ShenandoahBarrierSetC2::bsc2()->state()->add_shenandoah_barrier(this);
 349 }
 350 
 351 Node* ShenandoahWriteBarrierNode::Identity(PhaseGVN* phase) {
 352   assert(in(0) != NULL, "should have control");
 353   PhaseIterGVN* igvn = phase->is_IterGVN();
 354   Node* mem_in = in(Memory);
 355   Node* mem_proj = NULL;
 356 
 357   if (igvn != NULL) {
 358     mem_proj = find_out_with(Op_ShenandoahWBMemProj);
 359     if (mem_in == mem_proj) {
 360       return this;
 361     }
 362   }
 363 
 364   Node* replacement = Identity_impl(phase);
 365   if (igvn != NULL) {
 366     if (replacement != NULL && replacement != this && mem_proj != NULL) {
 367       igvn->replace_node(mem_proj, mem_in);
 368     }
 369   }
 370   return replacement;
 371 }
 372 
 373 Node* ShenandoahWriteBarrierNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 374   assert(in(0) != NULL, "should have control");
 375   if (!can_reshape) {
 376     return NULL;
 377   }
 378 
 379   Node* mem_in = in(Memory);
 380 
 381   if (mem_in->isa_MergeMem()) {
 382     const TypePtr* adr_type = brooks_pointer_type(bottom_type());
 383     uint alias_idx = phase->C->get_alias_index(adr_type);
 384     mem_in = mem_in->as_MergeMem()->memory_at(alias_idx);
 385     set_req(Memory, mem_in);
 386     return this;
 387   }
 388 
 389   Node* val = in(ValueIn);
 390   if (val->is_ShenandoahBarrier()) {
 391     set_req(ValueIn, val->in(ValueIn));
 392     return this;
 393   }
 394 
 395   return NULL;
 396 }
 397 
 398 bool ShenandoahWriteBarrierNode::expand(Compile* C, PhaseIterGVN& igvn, int& loop_opts_cnt) {
 399   if (UseShenandoahGC) {
 400     if (ShenandoahBarrierSetC2::bsc2()->state()->shenandoah_barriers_count() > 0 || (!ShenandoahWriteBarrier && ShenandoahStoreValEnqueueBarrier)) {
 401       bool attempt_more_loopopts = ShenandoahLoopOptsAfterExpansion;
 402       C->clear_major_progress();
 403       PhaseIdealLoop ideal_loop(igvn, LoopOptsShenandoahExpand);
 404       if (C->failing()) return false;
 405       PhaseIdealLoop::verify(igvn);
 406       DEBUG_ONLY(ShenandoahBarrierNode::verify_raw_mem(C->root());)
 407       if (attempt_more_loopopts) {
 408         C->set_major_progress();
 409         if (!C->optimize_loops(loop_opts_cnt, igvn, LoopOptsShenandoahPostExpand)) {
 410           return false;
 411         }
 412         C->clear_major_progress();
 413       }
 414     }
 415   }
 416   return true;
 417 }
 418 
 419 bool ShenandoahWriteBarrierNode::is_heap_state_test(Node* iff, int mask) {
 420   if (!UseShenandoahGC) {
 421     return false;
 422   }
 423   assert(iff->is_If(), "bad input");
 424   if (iff->Opcode() != Op_If) {
 425     return false;
 426   }
 427   Node* bol = iff->in(1);
 428   if (!bol->is_Bool() || bol->as_Bool()->_test._test != BoolTest::ne) {
 429     return false;
 430   }
 431   Node* cmp = bol->in(1);
 432   if (cmp->Opcode() != Op_CmpI) {
 433     return false;
 434   }
 435   Node* in1 = cmp->in(1);
 436   Node* in2 = cmp->in(2);
 437   if (in2->find_int_con(-1) != 0) {
 438     return false;
 439   }
 440   if (in1->Opcode() != Op_AndI) {
 441     return false;
 442   }
 443   in2 = in1->in(2);
 444   if (in2->find_int_con(-1) != mask) {
 445     return false;
 446   }
 447   in1 = in1->in(1);
 448 
 449   return is_gc_state_load(in1);
 450 }
 451 
 452 bool ShenandoahWriteBarrierNode::is_heap_stable_test(Node* iff) {
 453   return is_heap_state_test(iff, ShenandoahHeap::HAS_FORWARDED);
 454 }
 455 
 456 bool ShenandoahWriteBarrierNode::is_gc_state_load(Node *n) {
 457   if (!UseShenandoahGC) {
 458     return false;
 459   }
 460   if (n->Opcode() != Op_LoadB) {
 461     return false;
 462   }
 463   Node* addp = n->in(MemNode::Address);
 464   if (!addp->is_AddP()) {
 465     return false;
 466   }
 467   Node* base = addp->in(AddPNode::Address);
 468   Node* off = addp->in(AddPNode::Offset);
 469   if (base->Opcode() != Op_ThreadLocal) {
 470     return false;
 471   }
 472   if (off->find_intptr_t_con(-1) != in_bytes(ShenandoahThreadLocalData::gc_state_offset())) {
 473     return false;
 474   }
 475   return true;
 476 }
 477 
 478 bool ShenandoahWriteBarrierNode::has_safepoint_between(Node* start, Node* stop, PhaseIdealLoop *phase) {
 479   assert(phase->is_dominator(stop, start), "bad inputs");
 480   ResourceMark rm;
 481   Unique_Node_List wq;
 482   wq.push(start);
 483   for (uint next = 0; next < wq.size(); next++) {
 484     Node *m = wq.at(next);
 485     if (m == stop) {
 486       continue;
 487     }
 488     if (m->is_SafePoint() && !m->is_CallLeaf()) {
 489       return true;
 490     }
 491     if (m->is_Region()) {
 492       for (uint i = 1; i < m->req(); i++) {
 493         wq.push(m->in(i));
 494       }
 495     } else {
 496       wq.push(m->in(0));
 497     }
 498   }
 499   return false;
 500 }
 501 
 502 bool ShenandoahWriteBarrierNode::try_common_gc_state_load(Node *n, PhaseIdealLoop *phase) {
 503   assert(is_gc_state_load(n), "inconsistent");
 504   Node* addp = n->in(MemNode::Address);
 505   Node* dominator = NULL;
 506   for (DUIterator_Fast imax, i = addp->fast_outs(imax); i < imax; i++) {
 507     Node* u = addp->fast_out(i);
 508     assert(is_gc_state_load(u), "inconsistent");
 509     if (u != n && phase->is_dominator(u->in(0), n->in(0))) {
 510       if (dominator == NULL) {
 511         dominator = u;
 512       } else {
 513         if (phase->dom_depth(u->in(0)) < phase->dom_depth(dominator->in(0))) {
 514           dominator = u;
 515         }
 516       }
 517     }
 518   }
 519   if (dominator == NULL || has_safepoint_between(n->in(0), dominator->in(0), phase)) {
 520     return false;
 521   }
 522   phase->igvn().replace_node(n, dominator);
 523 
 524   return true;
 525 }
 526 
 527 bool ShenandoahBarrierNode::dominates_memory_impl(PhaseGVN* phase,
 528                                                   Node* b1,
 529                                                   Node* b2,
 530                                                   Node* current,
 531                                                   bool linear) {
 532   ResourceMark rm;
 533   VectorSet visited(Thread::current()->resource_area());
 534   Node_Stack phis(0);
 535 
 536   for(int i = 0; i < 10; i++) {
 537     if (current == NULL) {
 538       return false;
 539     } else if (visited.test_set(current->_idx) || current->is_top() || current == b1) {
 540       current = NULL;
 541       while (phis.is_nonempty() && current == NULL) {
 542         uint idx = phis.index();
 543         Node* phi = phis.node();
 544         if (idx >= phi->req()) {
 545           phis.pop();
 546         } else {
 547           current = phi->in(idx);
 548           phis.set_index(idx+1);
 549         }
 550       }
 551       if (current == NULL) {
 552         return true;
 553       }
 554     } else if (current == b2) {
 555       return false;
 556     } else if (current == phase->C->immutable_memory()) {
 557       return false;
 558     } else if (current->isa_Phi()) {
 559       if (!linear) {
 560         return false;
 561       }
 562       phis.push(current, 2);
 563       current = current->in(1);
 564     } else if (current->Opcode() == Op_ShenandoahWriteBarrier) {
 565       current = current->in(Memory);
 566     } else if (current->Opcode() == Op_ShenandoahWBMemProj) {
 567       current = current->in(ShenandoahWBMemProjNode::WriteBarrier);
 568     } else if (current->is_Proj()) {
 569       current = current->in(0);
 570     } else if (current->is_Call()) {
 571       current = current->in(TypeFunc::Memory);
 572     } else if (current->is_MemBar()) {
 573       current = current->in(TypeFunc::Memory);
 574     } else if (current->is_MergeMem()) {
 575       const TypePtr* adr_type = brooks_pointer_type(phase->type(b2));
 576       uint alias_idx = phase->C->get_alias_index(adr_type);
 577       current = current->as_MergeMem()->memory_at(alias_idx);
 578     } else {
 579 #ifdef ASSERT
 580       current->dump();
 581 #endif
 582       ShouldNotReachHere();
 583       return false;
 584     }
 585   }
 586   return false;
 587 }
 588 
 589 /**
 590  * Determines if b1 dominates b2 through memory inputs. It returns true if:
 591  * - b1 can be reached by following each branch in b2's memory input (through phis, etc)
 592  * - or we get back to b2 (i.e. through a loop) without seeing b1
 593  * In all other cases, (in particular, if we reach immutable_memory without having seen b1)
 594  * we return false.
 595  */
 596 bool ShenandoahBarrierNode::dominates_memory(PhaseGVN* phase, Node* b1, Node* b2, bool linear) {
 597   return dominates_memory_impl(phase, b1, b2, b2->in(Memory), linear);
 598 }
 599 
 600 Node* ShenandoahBarrierNode::Identity_impl(PhaseGVN* phase) {
 601   Node* n = in(ValueIn);
 602 
 603   Node* rb_mem = Opcode() == Op_ShenandoahReadBarrier ? in(Memory) : NULL;
 604   if (! needs_barrier(phase, this, n, rb_mem, _allow_fromspace)) {
 605     return n;
 606   }
 607 
 608   // Try to find a write barrier sibling with identical inputs that we can fold into.
 609   for (DUIterator i = n->outs(); n->has_out(i); i++) {
 610     Node* sibling = n->out(i);
 611     if (sibling == this) {
 612       continue;
 613     }
 614     if (sibling->Opcode() != Op_ShenandoahWriteBarrier) {
 615       continue;
 616     }
 617 
 618     assert(sibling->in(ValueIn) == in(ValueIn), "sanity");
 619     assert(sibling->Opcode() == Op_ShenandoahWriteBarrier, "sanity");
 620 
 621     if (dominates_memory(phase, sibling, this, phase->is_IterGVN() == NULL)) {
 622       return sibling;
 623     }
 624   }
 625   return this;
 626 }
 627 
 628 #ifndef PRODUCT
 629 void ShenandoahBarrierNode::dump_spec(outputStream *st) const {
 630   const TypePtr* adr = adr_type();
 631   if (adr == NULL) {
 632     return;
 633   }
 634   st->print(" @");
 635   adr->dump_on(st);
 636   st->print(" (");
 637   Compile::current()->alias_type(adr)->adr_type()->dump_on(st);
 638   st->print(") ");
 639 }
 640 #endif
 641 
 642 Node* ShenandoahReadBarrierNode::Identity(PhaseGVN* phase) {
 643   Node* id = Identity_impl(phase);
 644 
 645   if (id == this && phase->is_IterGVN()) {
 646     Node* n = in(ValueIn);
 647     // No success in super call. Try to combine identical read barriers.
 648     for (DUIterator i = n->outs(); n->has_out(i); i++) {
 649       Node* sibling = n->out(i);
 650       if (sibling == this || sibling->Opcode() != Op_ShenandoahReadBarrier) {
 651         continue;
 652       }
 653       assert(sibling->in(ValueIn)  == in(ValueIn), "sanity");
 654       if (phase->is_IterGVN()->hash_find(sibling) &&
 655           sibling->bottom_type() == bottom_type() &&
 656           sibling->in(Control) == in(Control) &&
 657           dominates_memory_rb(phase, sibling, this, phase->is_IterGVN() == NULL)) {
 658         return sibling;
 659       }
 660     }
 661   }
 662   return id;
 663 }
 664 
 665 const Type* ShenandoahBarrierNode::Value(PhaseGVN* phase) const {
 666   // Either input is TOP ==> the result is TOP
 667   const Type *t1 = phase->type(in(Memory));
 668   if (t1 == Type::TOP) return Type::TOP;
 669   const Type *t2 = phase->type(in(ValueIn));
 670   if( t2 == Type::TOP ) return Type::TOP;
 671 
 672   if (t2 == TypePtr::NULL_PTR) {
 673     return _type;
 674   }
 675 
 676   const Type* type = t2->is_oopptr()->cast_to_nonconst();
 677   return type;
 678 }
 679 
 680 uint ShenandoahBarrierNode::hash() const {
 681   return TypeNode::hash() + _allow_fromspace;
 682 }
 683 
 684 uint ShenandoahBarrierNode::cmp(const Node& n) const {
 685   return _allow_fromspace == ((ShenandoahBarrierNode&) n)._allow_fromspace
 686     && TypeNode::cmp(n);
 687 }
 688 
 689 uint ShenandoahBarrierNode::size_of() const {
 690   return sizeof(*this);
 691 }
 692 
 693 Node* ShenandoahWBMemProjNode::Identity(PhaseGVN* phase) {
 694   Node* wb = in(WriteBarrier);
 695   if (wb->is_top()) return phase->C->top(); // Dead path.
 696 
 697   assert(wb->Opcode() == Op_ShenandoahWriteBarrier, "expect write barrier");
 698   PhaseIterGVN* igvn = phase->is_IterGVN();
 699   // We can't do the below unless the graph is fully constructed.
 700   if (igvn == NULL) {
 701     return this;
 702   }
 703 
 704   // If the mem projection has no barrier users, it's not needed anymore.
 705   if (wb->outcnt() == 1) {
 706     return wb->in(ShenandoahBarrierNode::Memory);
 707   }
 708 
 709   return this;
 710 }
 711 
 712 #ifdef ASSERT
 713 bool ShenandoahBarrierNode::verify_helper(Node* in, Node_Stack& phis, VectorSet& visited, verify_type t, bool trace, Unique_Node_List& barriers_used) {
 714   assert(phis.size() == 0, "");
 715 
 716   while (true) {
 717     if (in->bottom_type() == TypePtr::NULL_PTR) {
 718       if (trace) {tty->print_cr("NULL");}
 719     } else if (!in->bottom_type()->make_ptr()->make_oopptr()) {
 720       if (trace) {tty->print_cr("Non oop");}
 721     } else if (t == ShenandoahLoad && ShenandoahOptimizeStableFinals &&
 722                in->bottom_type()->make_ptr()->isa_aryptr() &&
 723                in->bottom_type()->make_ptr()->is_aryptr()->is_stable()) {
 724       if (trace) {tty->print_cr("Stable array load");}
 725     } else {
 726       if (in->is_ConstraintCast()) {
 727         in = in->in(1);
 728         continue;
 729       } else if (in->is_AddP()) {
 730         assert(!in->in(AddPNode::Address)->is_top(), "no raw memory access");
 731         in = in->in(AddPNode::Address);
 732         continue;
 733       } else if (in->is_Con()) {
 734         if (trace) {tty->print("Found constant"); in->dump();}
 735       } else if (in->is_ShenandoahBarrier()) {
 736         if (t == ShenandoahOopStore) {
 737           if (in->Opcode() != Op_ShenandoahWriteBarrier) {
 738             return false;
 739           }
 740           uint i = 0;
 741           for (; i < phis.size(); i++) {
 742             Node* n = phis.node_at(i);
 743             if (n->Opcode() == Op_ShenandoahEnqueueBarrier) {
 744               break;
 745             }
 746           }
 747           if (i == phis.size()) {
 748             return false;
 749           }
 750         } else if (t == ShenandoahStore && in->Opcode() != Op_ShenandoahWriteBarrier) {
 751           return false;
 752         }
 753         barriers_used.push(in);
 754         if (trace) {tty->print("Found barrier"); in->dump();}
 755       } else if (in->Opcode() == Op_ShenandoahEnqueueBarrier) {
 756         if (t != ShenandoahOopStore) {
 757           return false;
 758         }
 759         if (trace) {tty->print("Found enqueue barrier"); in->dump();}
 760         phis.push(in, in->req());
 761         in = in->in(1);
 762         continue;
 763       } else if (in->is_Proj() && in->in(0)->is_Allocate()) {
 764         if (trace) {tty->print("Found alloc"); in->in(0)->dump();}
 765       } else if (in->is_Phi()) {
 766         if (!visited.test_set(in->_idx)) {
 767           if (trace) {tty->print("Pushed phi:"); in->dump();}
 768           phis.push(in, 2);
 769           in = in->in(1);
 770           continue;
 771         }
 772         if (trace) {tty->print("Already seen phi:"); in->dump();}
 773       } else if (in->Opcode() == Op_CMoveP || in->Opcode() == Op_CMoveN) {
 774         if (!visited.test_set(in->_idx)) {
 775           if (trace) {tty->print("Pushed cmovep:"); in->dump();}
 776           phis.push(in, CMoveNode::IfTrue);
 777           in = in->in(CMoveNode::IfFalse);
 778           continue;
 779         }
 780         if (trace) {tty->print("Already seen cmovep:"); in->dump();}
 781       } else if (in->Opcode() == Op_EncodeP || in->Opcode() == Op_DecodeN) {
 782         in = in->in(1);
 783         continue;
 784       } else {
 785         return false;
 786       }
 787     }
 788     bool cont = false;
 789     while (phis.is_nonempty()) {
 790       uint idx = phis.index();
 791       Node* phi = phis.node();
 792       if (idx >= phi->req()) {
 793         if (trace) {tty->print("Popped phi:"); phi->dump();}
 794         phis.pop();
 795         continue;
 796       }
 797       if (trace) {tty->print("Next entry(%d) for phi:", idx); phi->dump();}
 798       in = phi->in(idx);
 799       phis.set_index(idx+1);
 800       cont = true;
 801       break;
 802     }
 803     if (!cont) {
 804       break;
 805     }
 806   }
 807   return true;
 808 }
 809 
 810 void ShenandoahBarrierNode::report_verify_failure(const char *msg, Node *n1, Node *n2) {
 811   if (n1 != NULL) {
 812     n1->dump(+10);
 813   }
 814   if (n2 != NULL) {
 815     n2->dump(+10);
 816   }
 817   fatal("%s", msg);
 818 }
 819 
 820 void ShenandoahBarrierNode::verify(RootNode* root) {
 821   ResourceMark rm;
 822   Unique_Node_List wq;
 823   GrowableArray<Node*> barriers;
 824   Unique_Node_List barriers_used;
 825   Node_Stack phis(0);
 826   VectorSet visited(Thread::current()->resource_area());
 827   const bool trace = false;
 828   const bool verify_no_useless_barrier = false;
 829 
 830   wq.push(root);
 831   for (uint next = 0; next < wq.size(); next++) {
 832     Node *n = wq.at(next);
 833     if (n->is_Load()) {
 834       const bool trace = false;
 835       if (trace) {tty->print("Verifying"); n->dump();}
 836       if (n->Opcode() == Op_LoadRange || n->Opcode() == Op_LoadKlass || n->Opcode() == Op_LoadNKlass) {
 837         if (trace) {tty->print_cr("Load range/klass");}
 838       } else {
 839         const TypePtr* adr_type = n->as_Load()->adr_type();
 840 
 841         if (adr_type->isa_oopptr() && adr_type->is_oopptr()->offset() == oopDesc::mark_offset_in_bytes()) {
 842           if (trace) {tty->print_cr("Mark load");}
 843         } else if (adr_type->isa_instptr() &&
 844                    adr_type->is_instptr()->klass()->is_subtype_of(Compile::current()->env()->Reference_klass()) &&
 845                    adr_type->is_instptr()->offset() == java_lang_ref_Reference::referent_offset) {
 846           if (trace) {tty->print_cr("Reference.get()");}
 847         } else {
 848           bool verify = true;
 849           if (adr_type->isa_instptr()) {
 850             const TypeInstPtr* tinst = adr_type->is_instptr();
 851             ciKlass* k = tinst->klass();
 852             assert(k->is_instance_klass(), "");
 853             ciInstanceKlass* ik = (ciInstanceKlass*)k;
 854             int offset = adr_type->offset();
 855 
 856             if ((ik->debug_final_field_at(offset) && ShenandoahOptimizeInstanceFinals) ||
 857                 (ik->debug_stable_field_at(offset) && ShenandoahOptimizeStableFinals)) {
 858               if (trace) {tty->print_cr("Final/stable");}
 859               verify = false;
 860             } else if (k == ciEnv::current()->Class_klass() &&
 861                        tinst->const_oop() != NULL &&
 862                        tinst->offset() >= (ik->size_helper() * wordSize)) {
 863               ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
 864               ciField* field = k->get_field_by_offset(tinst->offset(), true);
 865               if ((ShenandoahOptimizeStaticFinals && field->is_final()) ||
 866                   (ShenandoahOptimizeStableFinals && field->is_stable())) {
 867                 verify = false;
 868               }
 869             }
 870           }
 871 
 872           if (verify && !ShenandoahBarrierNode::verify_helper(n->in(MemNode::Address), phis, visited, ShenandoahLoad, trace, barriers_used)) {
 873             report_verify_failure("Shenandoah verification: Load should have barriers", n);
 874           }
 875         }
 876       }
 877     } else if (n->is_Store()) {
 878       const bool trace = false;
 879 
 880       if (trace) {tty->print("Verifying"); n->dump();}
 881       if (n->in(MemNode::ValueIn)->bottom_type()->make_oopptr()) {
 882         Node* adr = n->in(MemNode::Address);
 883         bool verify = true;
 884 
 885         if (adr->is_AddP() && adr->in(AddPNode::Base)->is_top()) {
 886           adr = adr->in(AddPNode::Address);
 887           if (adr->is_AddP()) {
 888             assert(adr->in(AddPNode::Base)->is_top(), "");
 889             adr = adr->in(AddPNode::Address);
 890             if (adr->Opcode() == Op_LoadP &&
 891                 adr->in(MemNode::Address)->in(AddPNode::Base)->is_top() &&
 892                 adr->in(MemNode::Address)->in(AddPNode::Address)->Opcode() == Op_ThreadLocal &&
 893                 adr->in(MemNode::Address)->in(AddPNode::Offset)->find_intptr_t_con(-1) == in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset())) {
 894               if (trace) {tty->print_cr("SATB prebarrier");}
 895               verify = false;
 896             }
 897           }
 898         }
 899 
 900         if (verify && !ShenandoahBarrierNode::verify_helper(n->in(MemNode::ValueIn), phis, visited, ShenandoahStoreValEnqueueBarrier ? ShenandoahOopStore : ShenandoahValue, trace, barriers_used)) {
 901           report_verify_failure("Shenandoah verification: Store should have barriers", n);
 902         }
 903       }
 904       if (!ShenandoahBarrierNode::verify_helper(n->in(MemNode::Address), phis, visited, ShenandoahStore, trace, barriers_used)) {
 905         report_verify_failure("Shenandoah verification: Store (address) should have barriers", n);
 906       }
 907     } else if (n->Opcode() == Op_CmpP) {
 908       const bool trace = false;
 909 
 910       Node* in1 = n->in(1);
 911       Node* in2 = n->in(2);
 912       if (in1->bottom_type()->isa_oopptr()) {
 913         if (trace) {tty->print("Verifying"); n->dump();}
 914 
 915         bool mark_inputs = false;
 916         if (in1->bottom_type() == TypePtr::NULL_PTR || in2->bottom_type() == TypePtr::NULL_PTR ||
 917             (in1->is_Con() || in2->is_Con())) {
 918           if (trace) {tty->print_cr("Comparison against a constant");}
 919           mark_inputs = true;
 920         } else if ((in1->is_CheckCastPP() && in1->in(1)->is_Proj() && in1->in(1)->in(0)->is_Allocate()) ||
 921                    (in2->is_CheckCastPP() && in2->in(1)->is_Proj() && in2->in(1)->in(0)->is_Allocate())) {
 922           if (trace) {tty->print_cr("Comparison with newly alloc'ed object");}
 923           mark_inputs = true;
 924         } else {
 925           assert(in2->bottom_type()->isa_oopptr(), "");
 926 
 927           if (!ShenandoahBarrierNode::verify_helper(in1, phis, visited, ShenandoahStore, trace, barriers_used) ||
 928               !ShenandoahBarrierNode::verify_helper(in2, phis, visited, ShenandoahStore, trace, barriers_used)) {
 929             report_verify_failure("Shenandoah verification: Cmp should have barriers", n);
 930           }
 931         }
 932         if (verify_no_useless_barrier &&
 933             mark_inputs &&
 934             (!ShenandoahBarrierNode::verify_helper(in1, phis, visited, ShenandoahValue, trace, barriers_used) ||
 935              !ShenandoahBarrierNode::verify_helper(in2, phis, visited, ShenandoahValue, trace, barriers_used))) {
 936           phis.clear();
 937           visited.Reset();
 938         }
 939       }
 940     } else if (n->is_LoadStore()) {
 941       if (n->in(MemNode::ValueIn)->bottom_type()->make_ptr() &&
 942           !ShenandoahBarrierNode::verify_helper(n->in(MemNode::ValueIn), phis, visited, ShenandoahStoreValEnqueueBarrier ? ShenandoahOopStore : ShenandoahValue, trace, barriers_used)) {
 943         report_verify_failure("Shenandoah verification: LoadStore (value) should have barriers", n);
 944       }
 945 
 946       if (n->in(MemNode::Address)->bottom_type()->make_oopptr() && !ShenandoahBarrierNode::verify_helper(n->in(MemNode::Address), phis, visited, ShenandoahStore, trace, barriers_used)) {
 947         report_verify_failure("Shenandoah verification: LoadStore (address) should have barriers", n);
 948       }
 949     } else if (n->Opcode() == Op_CallLeafNoFP || n->Opcode() == Op_CallLeaf) {
 950       CallNode* call = n->as_Call();
 951 
 952       static struct {
 953         const char* name;
 954         struct {
 955           int pos;
 956           verify_type t;
 957         } args[6];
 958       } calls[] = {
 959         "aescrypt_encryptBlock",
 960         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahStore },  { TypeFunc::Parms+2, ShenandoahLoad },
 961           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 962         "aescrypt_decryptBlock",
 963         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahStore },  { TypeFunc::Parms+2, ShenandoahLoad },
 964           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 965         "multiplyToLen",
 966         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+2, ShenandoahLoad },   { TypeFunc::Parms+4, ShenandoahStore },
 967           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 968         "squareToLen",
 969         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+2, ShenandoahLoad },   { -1,  ShenandoahNone},
 970           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 971         "montgomery_multiply",
 972         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahLoad },   { TypeFunc::Parms+2, ShenandoahLoad },
 973           { TypeFunc::Parms+6, ShenandoahStore }, { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 974         "montgomery_square",
 975         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahLoad },   { TypeFunc::Parms+5, ShenandoahStore },
 976           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 977         "mulAdd",
 978         { { TypeFunc::Parms, ShenandoahStore },  { TypeFunc::Parms+1, ShenandoahLoad },   { -1,  ShenandoahNone},
 979           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 980         "vectorizedMismatch",
 981         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahLoad },   { -1,  ShenandoahNone},
 982           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 983         "updateBytesCRC32",
 984         { { TypeFunc::Parms+1, ShenandoahLoad }, { -1,  ShenandoahNone},                  { -1,  ShenandoahNone},
 985           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 986         "updateBytesAdler32",
 987         { { TypeFunc::Parms+1, ShenandoahLoad }, { -1,  ShenandoahNone},                  { -1,  ShenandoahNone},
 988           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 989         "updateBytesCRC32C",
 990         { { TypeFunc::Parms+1, ShenandoahLoad }, { TypeFunc::Parms+3, ShenandoahLoad},    { -1,  ShenandoahNone},
 991           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 992         "counterMode_AESCrypt",
 993         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahStore },  { TypeFunc::Parms+2, ShenandoahLoad },
 994           { TypeFunc::Parms+3, ShenandoahStore }, { TypeFunc::Parms+5, ShenandoahStore }, { TypeFunc::Parms+6, ShenandoahStore } },
 995         "cipherBlockChaining_encryptAESCrypt",
 996         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahStore },  { TypeFunc::Parms+2, ShenandoahLoad },
 997           { TypeFunc::Parms+3, ShenandoahLoad },  { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
 998         "cipherBlockChaining_decryptAESCrypt",
 999         { { TypeFunc::Parms, ShenandoahLoad },   { TypeFunc::Parms+1, ShenandoahStore },  { TypeFunc::Parms+2, ShenandoahLoad },
1000           { TypeFunc::Parms+3, ShenandoahLoad },  { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
1001         "shenandoah_clone_barrier",
1002         { { TypeFunc::Parms, ShenandoahLoad },   { -1,  ShenandoahNone},                  { -1,  ShenandoahNone},
1003           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
1004         "ghash_processBlocks",
1005         { { TypeFunc::Parms, ShenandoahStore },  { TypeFunc::Parms+1, ShenandoahLoad },   { TypeFunc::Parms+2, ShenandoahLoad },
1006           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
1007         "sha1_implCompress",
1008         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
1009           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
1010         "sha256_implCompress",
1011         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
1012           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
1013         "sha512_implCompress",
1014         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
1015           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
1016         "sha1_implCompressMB",
1017         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
1018           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
1019         "sha256_implCompressMB",
1020         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
1021           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
1022         "sha512_implCompressMB",
1023         { { TypeFunc::Parms, ShenandoahLoad },  { TypeFunc::Parms+1, ShenandoahStore },   { -1, ShenandoahNone },
1024           { -1,  ShenandoahNone},                 { -1,  ShenandoahNone},                 { -1,  ShenandoahNone} },
1025       };
1026 
1027       if (call->is_call_to_arraycopystub()) {
1028         Node* dest = NULL;
1029         const TypeTuple* args = n->as_Call()->_tf->domain();
1030         for (uint i = TypeFunc::Parms, j = 0; i < args->cnt(); i++) {
1031           if (args->field_at(i)->isa_ptr()) {
1032             j++;
1033             if (j == 2) {
1034               dest = n->in(i);
1035               break;
1036             }
1037           }
1038         }
1039         if (!ShenandoahBarrierNode::verify_helper(n->in(TypeFunc::Parms), phis, visited, ShenandoahLoad, trace, barriers_used) ||
1040             !ShenandoahBarrierNode::verify_helper(dest, phis, visited, ShenandoahStore, trace, barriers_used)) {
1041           report_verify_failure("Shenandoah verification: ArrayCopy should have barriers", n);
1042         }
1043       } else if (strlen(call->_name) > 5 &&
1044                  !strcmp(call->_name + strlen(call->_name) - 5, "_fill")) {
1045         if (!ShenandoahBarrierNode::verify_helper(n->in(TypeFunc::Parms), phis, visited, ShenandoahStore, trace, barriers_used)) {
1046           report_verify_failure("Shenandoah verification: _fill should have barriers", n);
1047         }
1048       } else if (!strcmp(call->_name, "shenandoah_wb_pre")) {
1049         // skip
1050       } else {
1051         const int calls_len = sizeof(calls) / sizeof(calls[0]);
1052         int i = 0;
1053         for (; i < calls_len; i++) {
1054           if (!strcmp(calls[i].name, call->_name)) {
1055             break;
1056           }
1057         }
1058         if (i != calls_len) {
1059           const uint args_len = sizeof(calls[0].args) / sizeof(calls[0].args[0]);
1060           for (uint j = 0; j < args_len; j++) {
1061             int pos = calls[i].args[j].pos;
1062             if (pos == -1) {
1063               break;
1064             }
1065             if (!ShenandoahBarrierNode::verify_helper(call->in(pos), phis, visited, calls[i].args[j].t, trace, barriers_used)) {
1066               report_verify_failure("Shenandoah verification: intrinsic calls should have barriers", n);
1067             }
1068           }
1069           for (uint j = TypeFunc::Parms; j < call->req(); j++) {
1070             if (call->in(j)->bottom_type()->make_ptr() &&
1071                 call->in(j)->bottom_type()->make_ptr()->isa_oopptr()) {
1072               uint k = 0;
1073               for (; k < args_len && calls[i].args[k].pos != (int)j; k++);
1074               if (k == args_len) {
1075                 fatal("arg %d for call %s not covered", j, call->_name);
1076               }
1077             }
1078           }
1079         } else {
1080           for (uint j = TypeFunc::Parms; j < call->req(); j++) {
1081             if (call->in(j)->bottom_type()->make_ptr() &&
1082                 call->in(j)->bottom_type()->make_ptr()->isa_oopptr()) {
1083               fatal("%s not covered", call->_name);
1084             }
1085           }
1086         }
1087       }
1088     } else if (n->is_ShenandoahBarrier()) {
1089       assert(!barriers.contains(n), "");
1090       assert(n->Opcode() != Op_ShenandoahWriteBarrier || n->find_out_with(Op_ShenandoahWBMemProj) != NULL, "bad shenandoah write barrier");
1091       assert(n->Opcode() != Op_ShenandoahWriteBarrier || n->outcnt() > 1, "bad shenandoah write barrier");
1092       barriers.push(n);
1093     } else if (n->Opcode() == Op_ShenandoahEnqueueBarrier) {
1094       // skip
1095     } else if (n->Opcode() == Op_ShenandoahWBMemProj) {
1096       assert(n->in(0) == NULL && n->in(ShenandoahWBMemProjNode::WriteBarrier)->Opcode() == Op_ShenandoahWriteBarrier, "strange ShenandoahWBMemProj");
1097     } else if (n->is_AddP()
1098                || n->is_Phi()
1099                || n->is_ConstraintCast()
1100                || n->Opcode() == Op_Return
1101                || n->Opcode() == Op_CMoveP
1102                || n->Opcode() == Op_CMoveN
1103                || n->Opcode() == Op_Rethrow
1104                || n->is_MemBar()
1105                || n->Opcode() == Op_Conv2B
1106                || n->Opcode() == Op_SafePoint
1107                || n->is_CallJava()
1108                || n->Opcode() == Op_Unlock
1109                || n->Opcode() == Op_EncodeP
1110                || n->Opcode() == Op_DecodeN) {
1111       // nothing to do
1112     } else {
1113       static struct {
1114         int opcode;
1115         struct {
1116           int pos;
1117           verify_type t;
1118         } inputs[2];
1119       } others[] = {
1120         Op_FastLock,
1121         { { 1, ShenandoahLoad },                  { -1, ShenandoahNone} },
1122         Op_Lock,
1123         { { TypeFunc::Parms, ShenandoahLoad },    { -1, ShenandoahNone} },
1124         Op_ArrayCopy,
1125         { { ArrayCopyNode::Src, ShenandoahLoad }, { ArrayCopyNode::Dest, ShenandoahStore } },
1126         Op_StrCompressedCopy,
1127         { { 2, ShenandoahLoad },                  { 3, ShenandoahStore } },
1128         Op_StrInflatedCopy,
1129         { { 2, ShenandoahLoad },                  { 3, ShenandoahStore } },
1130         Op_AryEq,
1131         { { 2, ShenandoahLoad },                  { 3, ShenandoahLoad } },
1132         Op_StrIndexOf,
1133         { { 2, ShenandoahLoad },                  { 4, ShenandoahLoad } },
1134         Op_StrComp,
1135         { { 2, ShenandoahLoad },                  { 4, ShenandoahLoad } },
1136         Op_StrEquals,
1137         { { 2, ShenandoahLoad },                  { 3, ShenandoahLoad } },
1138         Op_EncodeISOArray,
1139         { { 2, ShenandoahLoad },                  { 3, ShenandoahStore } },
1140         Op_HasNegatives,
1141         { { 2, ShenandoahLoad },                  { -1, ShenandoahNone} },
1142         Op_CastP2X,
1143         { { 1, ShenandoahLoad },                  { -1, ShenandoahNone} },
1144         Op_StrIndexOfChar,
1145         { { 2, ShenandoahLoad },                  { -1, ShenandoahNone } },
1146       };
1147 
1148       const int others_len = sizeof(others) / sizeof(others[0]);
1149       int i = 0;
1150       for (; i < others_len; i++) {
1151         if (others[i].opcode == n->Opcode()) {
1152           break;
1153         }
1154       }
1155       uint stop = n->is_Call() ? n->as_Call()->tf()->domain()->cnt() : n->req();
1156       if (i != others_len) {
1157         const uint inputs_len = sizeof(others[0].inputs) / sizeof(others[0].inputs[0]);
1158         for (uint j = 0; j < inputs_len; j++) {
1159           int pos = others[i].inputs[j].pos;
1160           if (pos == -1) {
1161             break;
1162           }
1163           if (!ShenandoahBarrierNode::verify_helper(n->in(pos), phis, visited, others[i].inputs[j].t, trace, barriers_used)) {
1164             report_verify_failure("Shenandoah verification: intrinsic calls should have barriers", n);
1165           }
1166         }
1167         for (uint j = 1; j < stop; j++) {
1168           if (n->in(j) != NULL && n->in(j)->bottom_type()->make_ptr() &&
1169               n->in(j)->bottom_type()->make_ptr()->make_oopptr()) {
1170             uint k = 0;
1171             for (; k < inputs_len && others[i].inputs[k].pos != (int)j; k++);
1172             if (k == inputs_len) {
1173               fatal("arg %d for node %s not covered", j, n->Name());
1174             }
1175           }
1176         }
1177       } else {
1178         for (uint j = 1; j < stop; j++) {
1179           if (n->in(j) != NULL && n->in(j)->bottom_type()->make_ptr() &&
1180               n->in(j)->bottom_type()->make_ptr()->make_oopptr()) {
1181             fatal("%s not covered", n->Name());
1182           }
1183         }
1184       }
1185     }
1186 
1187     if (n->is_SafePoint()) {
1188       SafePointNode* sfpt = n->as_SafePoint();
1189       if (verify_no_useless_barrier && sfpt->jvms() != NULL) {
1190         for (uint i = sfpt->jvms()->scloff(); i < sfpt->jvms()->endoff(); i++) {
1191           if (!ShenandoahBarrierNode::verify_helper(sfpt->in(i), phis, visited, ShenandoahLoad, trace, barriers_used)) {
1192             phis.clear();
1193             visited.Reset();
1194           }
1195         }
1196       }
1197     }
1198     for( uint i = 0; i < n->len(); ++i ) {
1199       Node *m = n->in(i);
1200       if (m == NULL) continue;
1201 
1202       // In most cases, inputs should be known to be non null. If it's
1203       // not the case, it could be a missing cast_not_null() in an
1204       // intrinsic or support might be needed in AddPNode::Ideal() to
1205       // avoid a NULL+offset input.
1206       if (!(n->is_Phi() ||
1207             (n->is_SafePoint() && (!n->is_CallRuntime() || !strcmp(n->as_Call()->_name, "shenandoah_wb_pre") || !strcmp(n->as_Call()->_name, "unsafe_arraycopy"))) ||
1208             n->Opcode() == Op_CmpP ||
1209             n->Opcode() == Op_CmpN ||
1210             (n->Opcode() == Op_StoreP && i == StoreNode::ValueIn) ||
1211             (n->Opcode() == Op_StoreN && i == StoreNode::ValueIn) ||
1212             n->is_ConstraintCast() ||
1213             n->Opcode() == Op_Return ||
1214             n->Opcode() == Op_Conv2B ||
1215             n->is_AddP() ||
1216             n->Opcode() == Op_CMoveP ||
1217             n->Opcode() == Op_CMoveN ||
1218             n->Opcode() == Op_Rethrow ||
1219             n->is_MemBar() ||
1220             n->is_Mem() ||
1221             n->Opcode() == Op_AryEq ||
1222             n->Opcode() == Op_SCMemProj ||
1223             n->Opcode() == Op_EncodeP ||
1224             n->Opcode() == Op_DecodeN ||
1225             n->Opcode() == Op_ShenandoahWriteBarrier ||
1226             n->Opcode() == Op_ShenandoahWBMemProj ||
1227             n->Opcode() == Op_ShenandoahEnqueueBarrier)) {
1228         if (m->bottom_type()->make_oopptr() && m->bottom_type()->make_oopptr()->meet(TypePtr::NULL_PTR) == m->bottom_type()) {
1229           report_verify_failure("Shenandoah verification: null input", n, m);
1230         }
1231       }
1232 
1233       wq.push(m);
1234     }
1235   }
1236 
1237   if (verify_no_useless_barrier) {
1238     for (int i = 0; i < barriers.length(); i++) {
1239       Node* n = barriers.at(i);
1240       if (!barriers_used.member(n)) {
1241         tty->print("XXX useless barrier"); n->dump(-2);
1242         ShouldNotReachHere();
1243       }
1244     }
1245   }
1246 }
1247 #endif
1248 
1249 bool ShenandoahBarrierNode::is_dominator_same_ctrl(Node*c, Node* d, Node* n, PhaseIdealLoop* phase) {
1250   // That both nodes have the same control is not sufficient to prove
1251   // domination, verify that there's no path from d to n
1252   ResourceMark rm;
1253   Unique_Node_List wq;
1254   wq.push(d);
1255   for (uint next = 0; next < wq.size(); next++) {
1256     Node *m = wq.at(next);
1257     if (m == n) {
1258       return false;
1259     }
1260     if (m->is_Phi() && m->in(0)->is_Loop()) {
1261       assert(phase->ctrl_or_self(m->in(LoopNode::EntryControl)) != c, "following loop entry should lead to new control");
1262     } else {
1263       for (uint i = 0; i < m->req(); i++) {
1264         if (m->in(i) != NULL && phase->ctrl_or_self(m->in(i)) == c) {
1265           wq.push(m->in(i));
1266         }
1267       }
1268     }
1269   }
1270   return true;
1271 }
1272 
1273 bool ShenandoahBarrierNode::is_dominator(Node *d_c, Node *n_c, Node* d, Node* n, PhaseIdealLoop* phase) {
1274   if (d_c != n_c) {
1275     return phase->is_dominator(d_c, n_c);
1276   }
1277   return is_dominator_same_ctrl(d_c, d, n, phase);
1278 }
1279 
1280 Node* next_mem(Node* mem, int alias) {
1281   Node* res = NULL;
1282   if (mem->is_Proj()) {
1283     res = mem->in(0);
1284   } else if (mem->is_SafePoint() || mem->is_MemBar()) {
1285     res = mem->in(TypeFunc::Memory);
1286   } else if (mem->is_Phi()) {
1287     res = mem->in(1);
1288   } else if (mem->is_ShenandoahBarrier()) {
1289     res = mem->in(ShenandoahBarrierNode::Memory);
1290   } else if (mem->is_MergeMem()) {
1291     res = mem->as_MergeMem()->memory_at(alias);
1292   } else if (mem->is_Store() || mem->is_LoadStore() || mem->is_ClearArray()) {
1293     assert(alias = Compile::AliasIdxRaw, "following raw memory can't lead to a barrier");
1294     res = mem->in(MemNode::Memory);
1295   } else if (mem->Opcode() == Op_ShenandoahWBMemProj) {
1296     res = mem->in(ShenandoahWBMemProjNode::WriteBarrier);
1297   } else {
1298 #ifdef ASSERT
1299     mem->dump();
1300 #endif
1301     ShouldNotReachHere();
1302   }
1303   return res;
1304 }
1305 
1306 Node* ShenandoahBarrierNode::no_branches(Node* c, Node* dom, bool allow_one_proj, PhaseIdealLoop* phase) {
1307   Node* iffproj = NULL;
1308   while (c != dom) {
1309     Node* next = phase->idom(c);
1310     assert(next->unique_ctrl_out() == c || c->is_Proj() || c->is_Region(), "multiple control flow out but no proj or region?");
1311     if (c->is_Region()) {
1312       ResourceMark rm;
1313       Unique_Node_List wq;
1314       wq.push(c);
1315       for (uint i = 0; i < wq.size(); i++) {
1316         Node *n = wq.at(i);
1317         if (n == next) {
1318           continue;
1319         }
1320         if (n->is_Region()) {
1321           for (uint j = 1; j < n->req(); j++) {
1322             wq.push(n->in(j));
1323           }
1324         } else {
1325           wq.push(n->in(0));
1326         }
1327       }
1328       for (uint i = 0; i < wq.size(); i++) {
1329         Node *n = wq.at(i);
1330         assert(n->is_CFG(), "");
1331         if (n->is_Multi()) {
1332           for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
1333             Node* u = n->fast_out(j);
1334             if (u->is_CFG()) {
1335               if (!wq.member(u) && !u->as_Proj()->is_uncommon_trap_proj(Deoptimization::Reason_none)) {
1336                 return NodeSentinel;
1337               }
1338             }
1339           }
1340         }
1341       }
1342     } else  if (c->is_Proj()) {
1343       if (c->is_IfProj()) {
1344         if (c->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none) != NULL) {
1345           // continue;
1346         } else {
1347           if (!allow_one_proj) {
1348             return NodeSentinel;
1349           }
1350           if (iffproj == NULL) {
1351             iffproj = c;
1352           } else {
1353             return NodeSentinel;
1354           }
1355         }
1356       } else if (c->Opcode() == Op_JumpProj) {
1357         return NodeSentinel; // unsupported
1358       } else if (c->Opcode() == Op_CatchProj) {
1359         return NodeSentinel; // unsupported
1360       } else if (c->Opcode() == Op_CProj && next->Opcode() == Op_NeverBranch) {
1361         return NodeSentinel; // unsupported
1362       } else {
1363         assert(next->unique_ctrl_out() == c, "unsupported branch pattern");
1364       }
1365     }
1366     c = next;
1367   }
1368   return iffproj;
1369 }
1370 
1371 #ifdef ASSERT
1372 void ShenandoahWriteBarrierNode::memory_dominates_all_paths_helper(Node* c, Node* rep_ctrl, Unique_Node_List& controls, PhaseIdealLoop* phase) {
1373   const bool trace = false;
1374   if (trace) { tty->print("X control is"); c->dump(); }
1375 
1376   uint start = controls.size();
1377   controls.push(c);
1378   for (uint i = start; i < controls.size(); i++) {
1379     Node *n = controls.at(i);
1380 
1381     if (trace) { tty->print("X from"); n->dump(); }
1382 
1383     if (n == rep_ctrl) {
1384       continue;
1385     }
1386 
1387     if (n->is_Proj()) {
1388       Node* n_dom = n->in(0);
1389       IdealLoopTree* n_dom_loop = phase->get_loop(n_dom);
1390       if (n->is_IfProj() && n_dom->outcnt() == 2) {
1391         n_dom_loop = phase->get_loop(n_dom->as_If()->proj_out(n->as_Proj()->_con == 0 ? 1 : 0));
1392       }
1393       if (n_dom_loop != phase->ltree_root()) {
1394         Node* tail = n_dom_loop->tail();
1395         if (tail->is_Region()) {
1396           for (uint j = 1; j < tail->req(); j++) {
1397             if (phase->is_dominator(n_dom, tail->in(j)) && !phase->is_dominator(n, tail->in(j))) {
1398               assert(phase->is_dominator(rep_ctrl, tail->in(j)), "why are we here?");
1399               // entering loop from below, mark backedge
1400               if (trace) { tty->print("X pushing backedge"); tail->in(j)->dump(); }
1401               controls.push(tail->in(j));
1402               //assert(n->in(0) == n_dom, "strange flow control");
1403             }
1404           }
1405         } else if (phase->get_loop(n) != n_dom_loop && phase->is_dominator(n_dom, tail)) {
1406           // entering loop from below, mark backedge
1407           if (trace) { tty->print("X pushing backedge"); tail->dump(); }
1408           controls.push(tail);
1409           //assert(n->in(0) == n_dom, "strange flow control");
1410         }
1411       }
1412     }
1413 
1414     if (n->is_Loop()) {
1415       Node* c = n->in(LoopNode::EntryControl);
1416       if (trace) { tty->print("X pushing"); c->dump(); }
1417       controls.push(c);
1418     } else if (n->is_Region()) {
1419       for (uint i = 1; i < n->req(); i++) {
1420         Node* c = n->in(i);
1421         if (trace) { tty->print("X pushing"); c->dump(); }
1422         controls.push(c);
1423       }
1424     } else {
1425       Node* c = n->in(0);
1426       if (trace) { tty->print("X pushing"); c->dump(); }
1427       controls.push(c);
1428     }
1429   }
1430 }
1431 
1432 bool ShenandoahWriteBarrierNode::memory_dominates_all_paths(Node* mem, Node* rep_ctrl, int alias, PhaseIdealLoop* phase) {
1433   const bool trace = false;
1434   if (trace) {
1435     tty->print("XXX mem is"); mem->dump();
1436     tty->print("XXX rep ctrl is"); rep_ctrl->dump();
1437     tty->print_cr("XXX alias is %d", alias);
1438   }
1439   ResourceMark rm;
1440   Unique_Node_List wq;
1441   Unique_Node_List controls;
1442   wq.push(mem);
1443   for (uint next = 0; next < wq.size(); next++) {
1444     Node *nn = wq.at(next);
1445     if (trace) { tty->print("XX from mem"); nn->dump(); }
1446     assert(nn->bottom_type() == Type::MEMORY, "memory only");
1447 
1448     if (nn->is_Phi()) {
1449       Node* r = nn->in(0);
1450       for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) {
1451         Node* u = r->fast_out(j);
1452         if (u->is_Phi() && u->bottom_type() == Type::MEMORY && u != nn &&
1453             (u->adr_type() == TypePtr::BOTTOM || phase->C->get_alias_index(u->adr_type()) == alias)) {
1454           if (trace) { tty->print("XX Next mem (other phi)"); u->dump(); }
1455           wq.push(u);
1456         }
1457       }
1458     }
1459 
1460     for (DUIterator_Fast imax, i = nn->fast_outs(imax); i < imax; i++) {
1461       Node* use = nn->fast_out(i);
1462 
1463       if (trace) { tty->print("XX use %p", use->adr_type()); use->dump(); }
1464       if (use->is_CFG() && use->in(TypeFunc::Memory) == nn) {
1465         Node* c = use->in(0);
1466         if (phase->is_dominator(rep_ctrl, c)) {
1467           memory_dominates_all_paths_helper(c, rep_ctrl, controls, phase);
1468         } else if (use->is_CallStaticJava() && use->as_CallStaticJava()->uncommon_trap_request() != 0 && c->is_Region()) {
1469           Node* region = c;
1470           if (trace) { tty->print("XX unc region"); region->dump(); }
1471           for (uint j = 1; j < region->req(); j++) {
1472             if (phase->is_dominator(rep_ctrl, region->in(j))) {
1473               if (trace) { tty->print("XX unc follows"); region->in(j)->dump(); }
1474               memory_dominates_all_paths_helper(region->in(j), rep_ctrl, controls, phase);
1475             }
1476           }
1477         }
1478         //continue;
1479       } else if (use->is_Phi()) {
1480         assert(use->bottom_type() == Type::MEMORY, "bad phi");
1481         if ((use->adr_type() == TypePtr::BOTTOM) ||
1482             phase->C->get_alias_index(use->adr_type()) == alias) {
1483           for (uint j = 1; j < use->req(); j++) {
1484             if (use->in(j) == nn) {
1485               Node* c = use->in(0)->in(j);
1486               if (phase->is_dominator(rep_ctrl, c)) {
1487                 memory_dominates_all_paths_helper(c, rep_ctrl, controls, phase);
1488               }
1489             }
1490           }
1491         }
1492         //        continue;
1493       }
1494 
1495       if (use->is_MergeMem()) {
1496         if (use->as_MergeMem()->memory_at(alias) == nn) {
1497           if (trace) { tty->print("XX Next mem"); use->dump(); }
1498           // follow the memory edges
1499           wq.push(use);
1500         }
1501       } else if (use->is_Phi()) {
1502         assert(use->bottom_type() == Type::MEMORY, "bad phi");
1503         if ((use->adr_type() == TypePtr::BOTTOM) ||
1504             phase->C->get_alias_index(use->adr_type()) == alias) {
1505           if (trace) { tty->print("XX Next mem"); use->dump(); }
1506           // follow the memory edges
1507           wq.push(use);
1508         }
1509       } else if (use->bottom_type() == Type::MEMORY &&
1510                  (use->adr_type() == TypePtr::BOTTOM || phase->C->get_alias_index(use->adr_type()) == alias)) {
1511         if (trace) { tty->print("XX Next mem"); use->dump(); }
1512         // follow the memory edges
1513         wq.push(use);
1514       } else if ((use->is_SafePoint() || use->is_MemBar()) &&
1515                  (use->adr_type() == TypePtr::BOTTOM || phase->C->get_alias_index(use->adr_type()) == alias)) {
1516         for (DUIterator_Fast jmax, j = use->fast_outs(jmax); j < jmax; j++) {
1517           Node* u = use->fast_out(j);
1518           if (u->bottom_type() == Type::MEMORY) {
1519             if (trace) { tty->print("XX Next mem"); u->dump(); }
1520             // follow the memory edges
1521             wq.push(u);
1522           }
1523         }
1524       } else if (use->Opcode() == Op_ShenandoahWriteBarrier && phase->C->get_alias_index(use->adr_type()) == alias) {
1525         Node* m = use->find_out_with(Op_ShenandoahWBMemProj);
1526         if (m != NULL) {
1527           if (trace) { tty->print("XX Next mem"); m->dump(); }
1528           // follow the memory edges
1529           wq.push(m);
1530         }
1531       }
1532     }
1533   }
1534 
1535   if (controls.size() == 0) {
1536     return false;
1537   }
1538 
1539   for (uint i = 0; i < controls.size(); i++) {
1540     Node *n = controls.at(i);
1541 
1542     if (trace) { tty->print("X checking"); n->dump(); }
1543 
1544     if (n->unique_ctrl_out() != NULL) {
1545       continue;
1546     }
1547 
1548     if (n->Opcode() == Op_NeverBranch) {
1549       Node* taken = n->as_Multi()->proj_out(0);
1550       if (!controls.member(taken)) {
1551         if (trace) { tty->print("X not seen"); taken->dump(); }
1552         return false;
1553       }
1554       continue;
1555     }
1556 
1557     for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
1558       Node* u = n->fast_out(j);
1559 
1560       if (u->is_CFG()) {
1561         if (!controls.member(u)) {
1562           if (u->is_Proj() && u->as_Proj()->is_uncommon_trap_proj(Deoptimization::Reason_none)) {
1563             if (trace) { tty->print("X not seen but unc"); u->dump(); }
1564           } else {
1565             Node* c = u;
1566             do {
1567               c = c->unique_ctrl_out();
1568             } while (c != NULL && c->is_Region());
1569             if (c != NULL && c->Opcode() == Op_Halt) {
1570               if (trace) { tty->print("X not seen but halt"); c->dump(); }
1571             } else {
1572               if (trace) { tty->print("X not seen"); u->dump(); }
1573               return false;
1574             }
1575           }
1576         } else {
1577           if (trace) { tty->print("X seen"); u->dump(); }
1578         }
1579       }
1580     }
1581   }
1582   return true;
1583 }
1584 #endif
1585 
1586 Node* ShenandoahBarrierNode::dom_mem(Node* mem, Node*& mem_ctrl, Node* n, Node* rep_ctrl, int alias, PhaseIdealLoop* phase) {
1587   ResourceMark rm;
1588   VectorSet wq(Thread::current()->resource_area());
1589   wq.set(mem->_idx);
1590   mem_ctrl = phase->get_ctrl(mem);
1591   while (!is_dominator(mem_ctrl, rep_ctrl, mem, n, phase)) {
1592     mem = next_mem(mem, alias);
1593     if (wq.test_set(mem->_idx)) {
1594       return NULL; // hit an unexpected loop
1595     }
1596     mem_ctrl = phase->ctrl_or_self(mem);
1597   }
1598   if (mem->is_MergeMem()) {
1599     mem = mem->as_MergeMem()->memory_at(alias);
1600     mem_ctrl = phase->ctrl_or_self(mem);
1601   }
1602   return mem;
1603 }
1604 
1605 Node* ShenandoahBarrierNode::dom_mem(Node* mem, Node* ctrl, int alias, Node*& mem_ctrl, PhaseIdealLoop* phase) {
1606   ResourceMark rm;
1607   VectorSet wq(Thread::current()->resource_area());
1608   wq.set(mem->_idx);
1609   mem_ctrl = phase->ctrl_or_self(mem);
1610   while (!phase->is_dominator(mem_ctrl, ctrl) || mem_ctrl == ctrl) {
1611     mem = next_mem(mem, alias);
1612     if (wq.test_set(mem->_idx)) {
1613       return NULL;
1614     }
1615     mem_ctrl = phase->ctrl_or_self(mem);
1616   }
1617   if (mem->is_MergeMem()) {
1618     mem = mem->as_MergeMem()->memory_at(alias);
1619     mem_ctrl = phase->ctrl_or_self(mem);
1620   }
1621   return mem;
1622 }
1623 
1624 static void disconnect_barrier_mem(Node* wb, PhaseIterGVN& igvn) {
1625   Node* mem_in = wb->in(ShenandoahBarrierNode::Memory);
1626   Node* proj = wb->find_out_with(Op_ShenandoahWBMemProj);
1627 
1628   for (DUIterator_Last imin, i = proj->last_outs(imin); i >= imin; ) {
1629     Node* u = proj->last_out(i);
1630     igvn.rehash_node_delayed(u);
1631     int nb = u->replace_edge(proj, mem_in);
1632     assert(nb > 0, "no replacement?");
1633     i -= nb;
1634   }
1635 }
1636 
1637 Node* ShenandoahWriteBarrierNode::move_above_predicates(LoopNode* cl, Node* val_ctrl, PhaseIdealLoop* phase) {
1638   Node* entry = cl->skip_strip_mined(-1)->in(LoopNode::EntryControl);
1639   Node* above_pred = phase->skip_all_loop_predicates(entry);
1640   Node* ctrl = entry;
1641   while (ctrl != above_pred) {
1642     Node* next = ctrl->in(0);
1643     if (!phase->is_dominator(val_ctrl, next)) {
1644       break;
1645     }
1646     ctrl = next;
1647   }
1648   return ctrl;
1649 }
1650 
1651 static MemoryGraphFixer* find_fixer(GrowableArray<MemoryGraphFixer*>& memory_graph_fixers, int alias) {
1652   for (int i = 0; i < memory_graph_fixers.length(); i++) {
1653     if (memory_graph_fixers.at(i)->alias() == alias) {
1654       return memory_graph_fixers.at(i);
1655     }
1656   }
1657   return NULL;
1658 }
1659 
1660 static MemoryGraphFixer* create_fixer(GrowableArray<MemoryGraphFixer*>& memory_graph_fixers, int alias, PhaseIdealLoop* phase, bool include_lsm) {
1661   assert(find_fixer(memory_graph_fixers, alias) == NULL, "none should exist yet");
1662   MemoryGraphFixer* fixer = new MemoryGraphFixer(alias, include_lsm, phase);
1663   memory_graph_fixers.push(fixer);
1664   return fixer;
1665 }
1666 
1667 void ShenandoahWriteBarrierNode::try_move_before_loop_helper(LoopNode* cl, Node* val_ctrl, GrowableArray<MemoryGraphFixer*>& memory_graph_fixers, PhaseIdealLoop* phase, bool include_lsm, Unique_Node_List& uses) {
1668   assert(cl->is_Loop(), "bad control");
1669   Node* ctrl = move_above_predicates(cl, val_ctrl, phase);
1670   Node* mem_ctrl = NULL;
1671   int alias = phase->C->get_alias_index(adr_type());
1672 
1673   MemoryGraphFixer* fixer = find_fixer(memory_graph_fixers, alias);
1674   if (fixer == NULL) {
1675     fixer = create_fixer(memory_graph_fixers, alias, phase, include_lsm);
1676   }
1677 
1678   Node* proj = find_out_with(Op_ShenandoahWBMemProj);
1679 
1680   fixer->remove(proj);
1681   Node* mem = fixer->find_mem(ctrl, NULL);
1682 
1683   assert(!ShenandoahVerifyOptoBarriers || memory_dominates_all_paths(mem, ctrl, alias, phase), "can't fix the memory graph");
1684 
1685   phase->set_ctrl_and_loop(this, ctrl);
1686   phase->igvn().replace_input_of(this, Control, ctrl);
1687 
1688   disconnect_barrier_mem(this, phase->igvn());
1689 
1690   phase->igvn().replace_input_of(this, Memory, mem);
1691   phase->set_ctrl_and_loop(proj, ctrl);
1692 
1693   fixer->fix_mem(ctrl, ctrl, mem, mem, proj, uses);
1694   assert(proj->outcnt() > 0, "disconnected write barrier");
1695 }
1696 
1697 LoopNode* ShenandoahWriteBarrierNode::try_move_before_pre_loop(Node* c, Node* val_ctrl, PhaseIdealLoop* phase) {
1698   // A write barrier between a pre and main loop can get in the way of
1699   // vectorization. Move it above the pre loop if possible
1700   CountedLoopNode* cl = NULL;
1701   if (c->is_IfFalse() &&
1702       c->in(0)->is_CountedLoopEnd()) {
1703     cl = c->in(0)->as_CountedLoopEnd()->loopnode();
1704   } else if (c->is_IfProj() &&
1705              c->in(0)->is_If() &&
1706              c->in(0)->in(0)->is_IfFalse() &&
1707              c->in(0)->in(0)->in(0)->is_CountedLoopEnd()) {
1708     cl = c->in(0)->in(0)->in(0)->as_CountedLoopEnd()->loopnode();
1709   }
1710   if (cl != NULL &&
1711       cl->is_pre_loop() &&
1712       val_ctrl != cl &&
1713       phase->is_dominator(val_ctrl, cl)) {
1714     return cl;
1715   }
1716   return NULL;
1717 }
1718 
1719 void ShenandoahWriteBarrierNode::try_move_before_loop(GrowableArray<MemoryGraphFixer*>& memory_graph_fixers, PhaseIdealLoop* phase, bool include_lsm, Unique_Node_List& uses) {
1720   Node *n_ctrl = phase->get_ctrl(this);
1721   IdealLoopTree *n_loop = phase->get_loop(n_ctrl);
1722   Node* val = in(ValueIn);
1723   Node* val_ctrl = phase->get_ctrl(val);
1724   if (n_loop != phase->ltree_root() && !n_loop->_irreducible) {
1725     IdealLoopTree *val_loop = phase->get_loop(val_ctrl);
1726     Node* mem = in(Memory);
1727     IdealLoopTree *mem_loop = phase->get_loop(phase->get_ctrl(mem));
1728     if (!n_loop->is_member(val_loop) &&
1729         n_loop->is_member(mem_loop)) {
1730       Node* n_loop_head = n_loop->_head;
1731 
1732       if (n_loop_head->is_Loop()) {
1733         LoopNode* loop = n_loop_head->as_Loop();
1734         if (n_loop_head->is_CountedLoop() && n_loop_head->as_CountedLoop()->is_main_loop()) {
1735           LoopNode* res = try_move_before_pre_loop(n_loop_head->in(LoopNode::EntryControl), val_ctrl, phase);
1736           if (res != NULL) {
1737             loop = res;
1738           }
1739         }
1740 
1741         try_move_before_loop_helper(loop, val_ctrl, memory_graph_fixers, phase, include_lsm, uses);
1742       }
1743     }
1744   }
1745   LoopNode* ctrl = try_move_before_pre_loop(in(0), val_ctrl, phase);
1746   if (ctrl != NULL) {
1747     try_move_before_loop_helper(ctrl, val_ctrl, memory_graph_fixers, phase, include_lsm, uses);
1748   }
1749 }
1750 
1751 Node* ShenandoahWriteBarrierNode::would_subsume(ShenandoahBarrierNode* other, PhaseIdealLoop* phase) {
1752   Node* val = in(ValueIn);
1753   Node* val_ctrl = phase->get_ctrl(val);
1754   Node* other_mem = other->in(Memory);
1755   Node* other_ctrl = phase->get_ctrl(other);
1756   Node* this_ctrl = phase->get_ctrl(this);
1757   IdealLoopTree* this_loop = phase->get_loop(this_ctrl);
1758   IdealLoopTree* other_loop = phase->get_loop(other_ctrl);
1759 
1760   Node* ctrl = phase->dom_lca(other_ctrl, this_ctrl);
1761 
1762   if (ctrl->is_Proj() &&
1763       ctrl->in(0)->is_Call() &&
1764       ctrl->unique_ctrl_out() != NULL &&
1765       ctrl->unique_ctrl_out()->Opcode() == Op_Catch &&
1766       !phase->is_dominator(val_ctrl, ctrl->in(0)->in(0))) {
1767     return NULL;
1768   }
1769 
1770   IdealLoopTree* loop = phase->get_loop(ctrl);
1771 
1772   // We don't want to move a write barrier in a loop
1773   // If the LCA is in a inner loop, try a control out of loop if possible
1774   while (!loop->is_member(this_loop) && (other->Opcode() != Op_ShenandoahWriteBarrier || !loop->is_member(other_loop))) {
1775     ctrl = phase->idom(ctrl);
1776     if (ctrl->is_MultiBranch()) {
1777       ctrl = ctrl->in(0);
1778     }
1779     if (ctrl != val_ctrl && phase->is_dominator(ctrl, val_ctrl)) {
1780       return NULL;
1781     }
1782     loop = phase->get_loop(ctrl);
1783   }
1784 
1785   if (ShenandoahDontIncreaseWBFreq) {
1786     Node* this_iffproj = no_branches(this_ctrl, ctrl, true, phase);
1787     if (other->Opcode() == Op_ShenandoahWriteBarrier) {
1788       Node* other_iffproj = no_branches(other_ctrl, ctrl, true, phase);
1789       if (other_iffproj == NULL || this_iffproj == NULL) {
1790         return ctrl;
1791       } else if (other_iffproj != NodeSentinel && this_iffproj != NodeSentinel &&
1792                  other_iffproj->in(0) == this_iffproj->in(0)) {
1793         return ctrl;
1794       }
1795     } else if (this_iffproj == NULL) {
1796       return ctrl;
1797     }
1798     return NULL;
1799   }
1800 
1801   return ctrl;
1802 }
1803 
1804 void ShenandoahWriteBarrierNode::optimize_before_expansion(PhaseIdealLoop* phase, GrowableArray<MemoryGraphFixer*> memory_graph_fixers, bool include_lsm) {
1805   bool progress = false;
1806   Unique_Node_List uses;
1807   do {
1808     progress = false;
1809     for (int i = 0; i < ShenandoahBarrierSetC2::bsc2()->state()->shenandoah_barriers_count(); i++) {
1810       ShenandoahWriteBarrierNode* wb = ShenandoahBarrierSetC2::bsc2()->state()->shenandoah_barrier(i);
1811 
1812       wb->try_move_before_loop(memory_graph_fixers, phase, include_lsm, uses);
1813 
1814       Node* val = wb->in(ValueIn);
1815 
1816       for (DUIterator_Fast jmax, j = val->fast_outs(jmax); j < jmax; j++) {
1817         Node* u = val->fast_out(j);
1818         if (u != wb && u->is_ShenandoahBarrier()) {
1819           Node* rep_ctrl = wb->would_subsume(u->as_ShenandoahBarrier(), phase);
1820 
1821           if (rep_ctrl != NULL) {
1822             Node* other = u;
1823             Node* val_ctrl = phase->get_ctrl(val);
1824             if (rep_ctrl->is_Proj() &&
1825                 rep_ctrl->in(0)->is_Call() &&
1826                 rep_ctrl->unique_ctrl_out() != NULL &&
1827                 rep_ctrl->unique_ctrl_out()->Opcode() == Op_Catch) {
1828               rep_ctrl = rep_ctrl->in(0)->in(0);
1829 
1830               assert(phase->is_dominator(val_ctrl, rep_ctrl), "bad control");
1831             } else {
1832               LoopNode* c = ShenandoahWriteBarrierNode::try_move_before_pre_loop(rep_ctrl, val_ctrl, phase);
1833               if (c != NULL) {
1834                 rep_ctrl = ShenandoahWriteBarrierNode::move_above_predicates(c, val_ctrl, phase);
1835               } else {
1836                 while (rep_ctrl->is_IfProj()) {
1837                   CallStaticJavaNode* unc = rep_ctrl->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1838                   if (unc != NULL) {
1839                     int req = unc->uncommon_trap_request();
1840                     Deoptimization::DeoptReason trap_reason = Deoptimization::trap_request_reason(req);
1841                     if ((trap_reason == Deoptimization::Reason_loop_limit_check ||
1842                          trap_reason == Deoptimization::Reason_predicate ||
1843                          trap_reason == Deoptimization::Reason_profile_predicate) &&
1844                         phase->is_dominator(val_ctrl, rep_ctrl->in(0)->in(0))) {
1845                       rep_ctrl = rep_ctrl->in(0)->in(0);
1846                       continue;
1847                     }
1848                   }
1849                   break;
1850                 }
1851               }
1852             }
1853 
1854             Node* wb_ctrl = phase->get_ctrl(wb);
1855             Node* other_ctrl = phase->get_ctrl(other);
1856             int alias = phase->C->get_alias_index(wb->adr_type());
1857             MemoryGraphFixer* fixer = find_fixer(memory_graph_fixers, alias);;
1858             if (!is_dominator(wb_ctrl, other_ctrl, wb, other, phase)) {
1859               if (fixer == NULL) {
1860                 fixer = create_fixer(memory_graph_fixers, alias, phase, include_lsm);
1861               }
1862               Node* mem = fixer->find_mem(rep_ctrl, phase->get_ctrl(other) == rep_ctrl ? other : NULL);
1863 
1864               if (mem->has_out_with(Op_Lock) || mem->has_out_with(Op_Unlock)) {
1865                 continue;
1866               }
1867 
1868               Node* wb_proj = wb->find_out_with(Op_ShenandoahWBMemProj);
1869               fixer->remove(wb_proj);
1870               Node* mem_for_ctrl = fixer->find_mem(rep_ctrl, NULL);
1871 
1872               if (wb->in(Memory) != mem) {
1873                 disconnect_barrier_mem(wb, phase->igvn());
1874                 phase->igvn().replace_input_of(wb, Memory, mem);
1875               }
1876               if (rep_ctrl != wb_ctrl) {
1877                 phase->set_ctrl_and_loop(wb, rep_ctrl);
1878                 phase->igvn().replace_input_of(wb, Control, rep_ctrl);
1879                 phase->set_ctrl_and_loop(wb_proj, rep_ctrl);
1880                 progress = true;
1881               }
1882 
1883               fixer->fix_mem(rep_ctrl, rep_ctrl, mem, mem_for_ctrl, wb_proj, uses);
1884 
1885               assert(!ShenandoahVerifyOptoBarriers || ShenandoahWriteBarrierNode::memory_dominates_all_paths(mem, rep_ctrl, alias, phase), "can't fix the memory graph");
1886             }
1887 
1888             if (other->Opcode() == Op_ShenandoahWriteBarrier) {
1889               Node* other_proj = other->find_out_with(Op_ShenandoahWBMemProj);
1890               if (fixer != NULL) {
1891                 fixer->remove(other_proj);
1892               }
1893               phase->igvn().replace_node(other_proj, other->in(Memory));
1894             }
1895             phase->igvn().replace_node(other, wb);
1896             --j; --jmax;
1897           }
1898         }
1899       }
1900     }
1901   } while(progress);
1902 }
1903 
1904 void ShenandoahReadBarrierNode::try_move(Node *n_ctrl, PhaseIdealLoop* phase) {
1905   Node* mem = in(MemNode::Memory);
1906   int alias = phase->C->get_alias_index(adr_type());
1907   const bool trace = false;
1908 
1909 #ifdef ASSERT
1910   if (trace) { tty->print("Trying to move mem of"); dump(); }
1911 #endif
1912 
1913   Node* new_mem = mem;
1914 
1915   ResourceMark rm;
1916   VectorSet seen(Thread::current()->resource_area());
1917   Node_List phis;
1918 
1919   for (;;) {
1920 #ifdef ASSERT
1921     if (trace) { tty->print("Looking for dominator from"); mem->dump(); }
1922 #endif
1923     if (mem->is_Proj() && mem->in(0)->is_Start()) {
1924       if (new_mem != in(MemNode::Memory)) {
1925 #ifdef ASSERT
1926         if (trace) { tty->print("XXX Setting mem to"); new_mem->dump(); tty->print(" for "); dump(); }
1927 #endif
1928         phase->igvn().replace_input_of(this, MemNode::Memory, new_mem);
1929       }
1930       return;
1931     }
1932 
1933     Node* candidate = mem;
1934     do {
1935       if (!is_independent(mem)) {
1936         if (trace) { tty->print_cr("Not independent"); }
1937         if (new_mem != in(MemNode::Memory)) {
1938 #ifdef ASSERT
1939           if (trace) { tty->print("XXX Setting mem to"); new_mem->dump(); tty->print(" for "); dump(); }
1940 #endif
1941           phase->igvn().replace_input_of(this, MemNode::Memory, new_mem);
1942         }
1943         return;
1944       }
1945       if (seen.test_set(mem->_idx)) {
1946         if (trace) { tty->print_cr("Already seen"); }
1947         ShouldNotReachHere();
1948         // Strange graph
1949         if (new_mem != in(MemNode::Memory)) {
1950 #ifdef ASSERT
1951           if (trace) { tty->print("XXX Setting mem to"); new_mem->dump(); tty->print(" for "); dump(); }
1952 #endif
1953           phase->igvn().replace_input_of(this, MemNode::Memory, new_mem);
1954         }
1955         return;
1956       }
1957       if (mem->is_Phi()) {
1958         phis.push(mem);
1959       }
1960       mem = next_mem(mem, alias);
1961       if (mem->bottom_type() == Type::MEMORY) {
1962         candidate = mem;
1963       }
1964       assert(is_dominator(phase->ctrl_or_self(mem), n_ctrl, mem, this, phase) == phase->is_dominator(phase->ctrl_or_self(mem), n_ctrl), "strange dominator");
1965 #ifdef ASSERT
1966       if (trace) { tty->print("Next mem is"); mem->dump(); }
1967 #endif
1968     } while (mem->bottom_type() != Type::MEMORY || !phase->is_dominator(phase->ctrl_or_self(mem), n_ctrl));
1969 
1970     assert(mem->bottom_type() == Type::MEMORY, "bad mem");
1971 
1972     bool not_dom = false;
1973     for (uint i = 0; i < phis.size() && !not_dom; i++) {
1974       Node* nn = phis.at(i);
1975 
1976 #ifdef ASSERT
1977       if (trace) { tty->print("Looking from phi"); nn->dump(); }
1978 #endif
1979       assert(nn->is_Phi(), "phis only");
1980       for (uint j = 2; j < nn->req() && !not_dom; j++) {
1981         Node* m = nn->in(j);
1982 #ifdef ASSERT
1983         if (trace) { tty->print("Input %d is", j); m->dump(); }
1984 #endif
1985         while (m != mem && !seen.test_set(m->_idx)) {
1986           if (is_dominator(phase->ctrl_or_self(m), phase->ctrl_or_self(mem), m, mem, phase)) {
1987             not_dom = true;
1988             // Scheduling anomaly
1989 #ifdef ASSERT
1990             if (trace) { tty->print("Giving up"); m->dump(); }
1991 #endif
1992             break;
1993           }
1994           if (!is_independent(m)) {
1995             if (trace) { tty->print_cr("Not independent"); }
1996             if (new_mem != in(MemNode::Memory)) {
1997 #ifdef ASSERT
1998               if (trace) { tty->print("XXX Setting mem to"); new_mem->dump(); tty->print(" for "); dump(); }
1999 #endif
2000               phase->igvn().replace_input_of(this, MemNode::Memory, new_mem);
2001             }
2002             return;
2003           }
2004           if (m->is_Phi()) {
2005             phis.push(m);
2006           }
2007           m = next_mem(m, alias);
2008 #ifdef ASSERT
2009           if (trace) { tty->print("Next mem is"); m->dump(); }
2010 #endif
2011         }
2012       }
2013     }
2014     if (!not_dom) {
2015       new_mem = mem;
2016       phis.clear();
2017     } else {
2018       seen.Clear();
2019     }
2020   }
2021 }
2022 
2023 CallStaticJavaNode* ShenandoahWriteBarrierNode::pin_and_expand_null_check(PhaseIterGVN& igvn) {
2024   Node* val = in(ValueIn);
2025 
2026   const Type* val_t = igvn.type(val);
2027 
2028   if (val_t->meet(TypePtr::NULL_PTR) != val_t &&
2029       val->Opcode() == Op_CastPP &&
2030       val->in(0) != NULL &&
2031       val->in(0)->Opcode() == Op_IfTrue &&
2032       val->in(0)->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none) &&
2033       val->in(0)->in(0)->is_If() &&
2034       val->in(0)->in(0)->in(1)->Opcode() == Op_Bool &&
2035       val->in(0)->in(0)->in(1)->as_Bool()->_test._test == BoolTest::ne &&
2036       val->in(0)->in(0)->in(1)->in(1)->Opcode() == Op_CmpP &&
2037       val->in(0)->in(0)->in(1)->in(1)->in(1) == val->in(1) &&
2038       val->in(0)->in(0)->in(1)->in(1)->in(2)->bottom_type() == TypePtr::NULL_PTR) {
2039     assert(val->in(0)->in(0)->in(1)->in(1)->in(1) == val->in(1), "");
2040     CallStaticJavaNode* unc = val->in(0)->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
2041     return unc;
2042   }
2043   return NULL;
2044 }
2045 
2046 void ShenandoahWriteBarrierNode::pin_and_expand_move_barrier(PhaseIdealLoop* phase, GrowableArray<MemoryGraphFixer*>& memory_graph_fixers, Unique_Node_List& uses) {
2047   Node* unc = pin_and_expand_null_check(phase->igvn());
2048   Node* val = in(ValueIn);
2049 
2050   if (unc != NULL) {
2051     Node* ctrl = phase->get_ctrl(this);
2052     Node* unc_ctrl = val->in(0);
2053 
2054     // Don't move write barrier in a loop
2055     IdealLoopTree* loop = phase->get_loop(ctrl);
2056     IdealLoopTree* unc_loop = phase->get_loop(unc_ctrl);
2057 
2058     if (!unc_loop->is_member(loop)) {
2059       return;
2060     }
2061 
2062     Node* branch = no_branches(ctrl, unc_ctrl, false, phase);
2063     assert(branch == NULL || branch == NodeSentinel, "was not looking for a branch");
2064     if (branch == NodeSentinel) {
2065       return;
2066     }
2067 
2068     RegionNode* r = new RegionNode(3);
2069     IfNode* iff = unc_ctrl->in(0)->as_If();
2070 
2071     Node* ctrl_use = unc_ctrl->unique_ctrl_out();
2072     Node* unc_ctrl_clone = unc_ctrl->clone();
2073     phase->register_control(unc_ctrl_clone, loop, iff);
2074     Node* c = unc_ctrl_clone;
2075     Node* new_cast = clone_null_check(c, val, unc_ctrl_clone, phase);
2076     r->init_req(1, new_cast->in(0)->in(0)->as_If()->proj_out(0));
2077 
2078     phase->igvn().replace_input_of(unc_ctrl, 0, c->in(0));
2079     phase->set_idom(unc_ctrl, c->in(0), phase->dom_depth(unc_ctrl));
2080     phase->lazy_replace(c, unc_ctrl);
2081     c = NULL;;
2082     phase->igvn().replace_input_of(val, 0, unc_ctrl_clone);
2083     phase->set_ctrl(val, unc_ctrl_clone);
2084 
2085     IfNode* new_iff = new_cast->in(0)->in(0)->as_If();
2086     fix_null_check(unc, unc_ctrl_clone, r, uses, phase);
2087     Node* iff_proj = iff->proj_out(0);
2088     r->init_req(2, iff_proj);
2089     phase->register_control(r, phase->ltree_root(), iff);
2090 
2091     Node* new_bol = new_iff->in(1)->clone();
2092     Node* new_cmp = new_bol->in(1)->clone();
2093     assert(new_cmp->Opcode() == Op_CmpP, "broken");
2094     assert(new_cmp->in(1) == val->in(1), "broken");
2095     new_bol->set_req(1, new_cmp);
2096     new_cmp->set_req(1, this);
2097     phase->register_new_node(new_bol, new_iff->in(0));
2098     phase->register_new_node(new_cmp, new_iff->in(0));
2099     phase->igvn().replace_input_of(new_iff, 1, new_bol);
2100     phase->igvn().replace_input_of(new_cast, 1, this);
2101 
2102     for (DUIterator_Fast imax, i = this->fast_outs(imax); i < imax; i++) {
2103       Node* u = this->fast_out(i);
2104       if (u == new_cast || u->Opcode() == Op_ShenandoahWBMemProj || u == new_cmp) {
2105         continue;
2106       }
2107       phase->igvn().rehash_node_delayed(u);
2108       int nb = u->replace_edge(this, new_cast);
2109       assert(nb > 0, "no update?");
2110       --i; imax -= nb;
2111     }
2112 
2113     for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
2114       Node* u = val->fast_out(i);
2115       if (u == this) {
2116         continue;
2117       }
2118       phase->igvn().rehash_node_delayed(u);
2119       int nb = u->replace_edge(val, new_cast);
2120       assert(nb > 0, "no update?");
2121       --i; imax -= nb;
2122     }
2123 
2124     Node* new_ctrl = unc_ctrl_clone;
2125 
2126     int alias = phase->C->get_alias_index(adr_type());
2127     MemoryGraphFixer* fixer = find_fixer(memory_graph_fixers, alias);
2128     if (fixer == NULL) {
2129       fixer = create_fixer(memory_graph_fixers, alias, phase, true);
2130     }
2131 
2132     Node* proj = find_out_with(Op_ShenandoahWBMemProj);
2133     fixer->remove(proj);
2134     Node* mem = fixer->find_mem(new_ctrl, NULL);
2135 
2136     if (in(Memory) != mem) {
2137       disconnect_barrier_mem(this, phase->igvn());
2138       phase->igvn().replace_input_of(this, Memory, mem);
2139     }
2140 
2141     phase->set_ctrl_and_loop(this, new_ctrl);
2142     phase->igvn().replace_input_of(this, Control, new_ctrl);
2143     phase->set_ctrl_and_loop(proj, new_ctrl);
2144 
2145     fixer->fix_mem(new_ctrl, new_ctrl, mem, mem, proj, uses);
2146   }
2147 }
2148 
2149 void ShenandoahWriteBarrierNode::pin_and_expand_helper(PhaseIdealLoop* phase) {
2150   Node* val = in(ValueIn);
2151   CallStaticJavaNode* unc = pin_and_expand_null_check(phase->igvn());
2152   Node* rep = this;
2153   Node* ctrl = phase->get_ctrl(this);
2154   if (unc != NULL && val->in(0) == ctrl) {
2155     Node* unc_ctrl = val->in(0);
2156     IfNode* other_iff = unc_ctrl->unique_ctrl_out()->as_If();
2157     ProjNode* other_unc_ctrl = other_iff->proj_out(1);
2158     Node* cast = NULL;
2159     for (DUIterator_Fast imax, i = other_unc_ctrl->fast_outs(imax); i < imax && cast == NULL; i++) {
2160       Node* u = other_unc_ctrl->fast_out(i);
2161       if (u->Opcode() == Op_CastPP && u->in(1) == this) {
2162         cast = u;
2163       }
2164     }
2165     assert(other_unc_ctrl->is_uncommon_trap_if_pattern(Deoptimization::Reason_none) == unc, "broken");
2166     rep = cast;
2167   }
2168 
2169   // Replace all uses of barrier's input that are dominated by ctrl
2170   // with the value returned by the barrier: no need to keep both
2171   // live.
2172   for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
2173     Node* u = val->fast_out(i);
2174     if (u != this) {
2175       if (u->is_Phi()) {
2176         int nb = 0;
2177         for (uint j = 1; j < u->req(); j++) {
2178           if (u->in(j) == val) {
2179             Node* c = u->in(0)->in(j);
2180             if (phase->is_dominator(ctrl, c)) {
2181               phase->igvn().replace_input_of(u, j, rep);
2182               nb++;
2183             }
2184           }
2185         }
2186         if (nb > 0) {
2187           imax -= nb;
2188           --i;
2189         }
2190       } else {
2191         Node* c = phase->ctrl_or_self(u);
2192         if (is_dominator(ctrl, c, this, u, phase)) {
2193           phase->igvn().rehash_node_delayed(u);
2194           int nb = u->replace_edge(val, rep);
2195           assert(nb > 0, "no update?");
2196           --i, imax -= nb;
2197         }
2198       }
2199     }
2200   }
2201 }
2202 
2203 Node* ShenandoahWriteBarrierNode::find_bottom_mem(Node* ctrl, PhaseIdealLoop* phase) {
2204   Node* mem = NULL;
2205   Node* c = ctrl;
2206   do {
2207     if (c->is_Region()) {
2208       Node* phi_bottom = NULL;
2209       for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax && mem == NULL; i++) {
2210         Node* u = c->fast_out(i);
2211         if (u->is_Phi() && u->bottom_type() == Type::MEMORY) {
2212           if (u->adr_type() == TypePtr::BOTTOM) {
2213             mem = u;
2214           }
2215         }
2216       }
2217     } else {
2218       if (c->is_Call() && c->as_Call()->adr_type() != NULL) {
2219         CallProjections projs;
2220         c->as_Call()->extract_projections(&projs, true, false);
2221         if (projs.fallthrough_memproj != NULL) {
2222           if (projs.fallthrough_memproj->adr_type() == TypePtr::BOTTOM) {
2223             if (projs.catchall_memproj == NULL) {
2224               mem = projs.fallthrough_memproj;
2225             } else {
2226               if (phase->is_dominator(projs.fallthrough_catchproj, ctrl)) {
2227                 mem = projs.fallthrough_memproj;
2228               } else {
2229                 assert(phase->is_dominator(projs.catchall_catchproj, ctrl), "one proj must dominate barrier");
2230                 mem = projs.catchall_memproj;
2231               }
2232             }
2233           }
2234         } else {
2235           Node* proj = c->as_Call()->proj_out(TypeFunc::Memory);
2236           if (proj != NULL &&
2237               proj->adr_type() == TypePtr::BOTTOM) {
2238             mem = proj;
2239           }
2240         }
2241       } else {
2242         for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax; i++) {
2243           Node* u = c->fast_out(i);
2244           if (u->is_Proj() &&
2245               u->bottom_type() == Type::MEMORY &&
2246               u->adr_type() == TypePtr::BOTTOM) {
2247               assert(c->is_SafePoint() || c->is_MemBar() || c->is_Start(), "");
2248               assert(mem == NULL, "only one proj");
2249               mem = u;
2250           }
2251         }
2252         assert(!c->is_Call() || c->as_Call()->adr_type() != NULL || mem == NULL, "no mem projection expected");
2253       }
2254     }
2255     c = phase->idom(c);
2256   } while (mem == NULL);
2257   return mem;
2258 }
2259 
2260 void ShenandoahWriteBarrierNode::follow_barrier_uses(Node* n, Node* ctrl, Unique_Node_List& uses, PhaseIdealLoop* phase) {
2261   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
2262     Node* u = n->fast_out(i);
2263     if (!u->is_CFG() && phase->get_ctrl(u) == ctrl && (!u->is_Phi() || !u->in(0)->is_Loop() || u->in(LoopNode::LoopBackControl) != n)) {
2264       uses.push(u);
2265     }
2266   }
2267 }
2268 
2269 void ShenandoahWriteBarrierNode::test_heap_stable(Node*& ctrl, Node* raw_mem, Node*& heap_stable_ctrl,
2270                                                   PhaseIdealLoop* phase) {
2271   IdealLoopTree* loop = phase->get_loop(ctrl);
2272   Node* thread = new ThreadLocalNode();
2273   phase->register_new_node(thread, ctrl);
2274   Node* offset = phase->igvn().MakeConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
2275   phase->set_ctrl(offset, phase->C->root());
2276   Node* gc_state_addr = new AddPNode(phase->C->top(), thread, offset);
2277   phase->register_new_node(gc_state_addr, ctrl);
2278   uint gc_state_idx = Compile::AliasIdxRaw;
2279   const TypePtr* gc_state_adr_type = NULL; // debug-mode-only argument
2280   debug_only(gc_state_adr_type = phase->C->get_adr_type(gc_state_idx));
2281 
2282   Node* gc_state = new LoadBNode(ctrl, raw_mem, gc_state_addr, gc_state_adr_type, TypeInt::BYTE, MemNode::unordered);
2283   phase->register_new_node(gc_state, ctrl);
2284   Node* heap_stable_and = new AndINode(gc_state, phase->igvn().intcon(ShenandoahHeap::HAS_FORWARDED));
2285   phase->register_new_node(heap_stable_and, ctrl);
2286   Node* heap_stable_cmp = new CmpINode(heap_stable_and, phase->igvn().zerocon(T_INT));
2287   phase->register_new_node(heap_stable_cmp, ctrl);
2288   Node* heap_stable_test = new BoolNode(heap_stable_cmp, BoolTest::ne);
2289   phase->register_new_node(heap_stable_test, ctrl);
2290   IfNode* heap_stable_iff = new IfNode(ctrl, heap_stable_test, PROB_UNLIKELY(0.999), COUNT_UNKNOWN);
2291   phase->register_control(heap_stable_iff, loop, ctrl);
2292 
2293   heap_stable_ctrl = new IfFalseNode(heap_stable_iff);
2294   phase->register_control(heap_stable_ctrl, loop, heap_stable_iff);
2295   ctrl = new IfTrueNode(heap_stable_iff);
2296   phase->register_control(ctrl, loop, heap_stable_iff);
2297 
2298   assert(is_heap_stable_test(heap_stable_iff), "Should match the shape");
2299 }
2300 
2301 void ShenandoahWriteBarrierNode::test_null(Node*& ctrl, Node* val, Node*& null_ctrl, PhaseIdealLoop* phase) {
2302   const Type* val_t = phase->igvn().type(val);
2303   if (val_t->meet(TypePtr::NULL_PTR) == val_t) {
2304     IdealLoopTree* loop = phase->get_loop(ctrl);
2305     Node* null_cmp = new CmpPNode(val, phase->igvn().zerocon(T_OBJECT));
2306     phase->register_new_node(null_cmp, ctrl);
2307     Node* null_test = new BoolNode(null_cmp, BoolTest::ne);
2308     phase->register_new_node(null_test, ctrl);
2309     IfNode* null_iff = new IfNode(ctrl, null_test, PROB_LIKELY(0.999), COUNT_UNKNOWN);
2310     phase->register_control(null_iff, loop, ctrl);
2311     ctrl = new IfTrueNode(null_iff);
2312     phase->register_control(ctrl, loop, null_iff);
2313     null_ctrl = new IfFalseNode(null_iff);
2314     phase->register_control(null_ctrl, loop, null_iff);
2315   }
2316 }
2317 
2318 Node* ShenandoahWriteBarrierNode::clone_null_check(Node*& c, Node* val, Node* unc_ctrl, PhaseIdealLoop* phase) {
2319   IdealLoopTree *loop = phase->get_loop(c);
2320   Node* iff = unc_ctrl->in(0);
2321   assert(iff->is_If(), "broken");
2322   Node* new_iff = iff->clone();
2323   new_iff->set_req(0, c);
2324   phase->register_control(new_iff, loop, c);
2325   Node* iffalse = new IfFalseNode(new_iff->as_If());
2326   phase->register_control(iffalse, loop, new_iff);
2327   Node* iftrue = new IfTrueNode(new_iff->as_If());
2328   phase->register_control(iftrue, loop, new_iff);
2329   c = iftrue;
2330   const Type *t = phase->igvn().type(val);
2331   assert(val->Opcode() == Op_CastPP, "expect cast to non null here");
2332   Node* uncasted_val = val->in(1);
2333   val = new CastPPNode(uncasted_val, t);
2334   val->init_req(0, c);
2335   phase->register_new_node(val, c);
2336   return val;
2337 }
2338 
2339 void ShenandoahWriteBarrierNode::fix_null_check(Node* unc, Node* unc_ctrl, Node* new_unc_ctrl,
2340                                                 Unique_Node_List& uses, PhaseIdealLoop* phase) {
2341   IfNode* iff = unc_ctrl->in(0)->as_If();
2342   Node* proj = iff->proj_out(0);
2343   assert(proj != unc_ctrl, "bad projection");
2344   Node* use = proj->unique_ctrl_out();
2345 
2346   assert(use == unc || use->is_Region(), "what else?");
2347 
2348   uses.clear();
2349   if (use == unc) {
2350     phase->set_idom(use, new_unc_ctrl, phase->dom_depth(use));
2351     for (uint i = 1; i < unc->req(); i++) {
2352       Node* n = unc->in(i);
2353       if (phase->has_ctrl(n) && phase->get_ctrl(n) == proj) {
2354         uses.push(n);
2355       }
2356     }
2357   } else {
2358     assert(use->is_Region(), "what else?");
2359     uint idx = 1;
2360     for (; use->in(idx) != proj; idx++);
2361     for (DUIterator_Fast imax, i = use->fast_outs(imax); i < imax; i++) {
2362       Node* u = use->fast_out(i);
2363       if (u->is_Phi() && phase->get_ctrl(u->in(idx)) == proj) {
2364         uses.push(u->in(idx));
2365       }
2366     }
2367   }
2368   for(uint next = 0; next < uses.size(); next++ ) {
2369     Node *n = uses.at(next);
2370     assert(phase->get_ctrl(n) == proj, "bad control");
2371     phase->set_ctrl_and_loop(n, new_unc_ctrl);
2372     if (n->in(0) == proj) {
2373       phase->igvn().replace_input_of(n, 0, new_unc_ctrl);
2374     }
2375     for (uint i = 0; i < n->req(); i++) {
2376       Node* m = n->in(i);
2377       if (m != NULL && phase->has_ctrl(m) && phase->get_ctrl(m) == proj) {
2378         uses.push(m);
2379       }
2380     }
2381   }
2382 
2383   phase->igvn().rehash_node_delayed(use);
2384   int nb = use->replace_edge(proj, new_unc_ctrl);
2385   assert(nb == 1, "only use expected");
2386 }
2387 
2388 void ShenandoahWriteBarrierNode::in_cset_fast_test(Node*& ctrl, Node*& not_cset_ctrl, Node* val, Node* raw_mem, PhaseIdealLoop* phase) {
2389   if (ShenandoahWriteBarrierCsetTestInIR) {
2390     IdealLoopTree *loop = phase->get_loop(ctrl);
2391     Node* raw_rbtrue = new CastP2XNode(ctrl, val);
2392     phase->register_new_node(raw_rbtrue, ctrl);
2393     Node* cset_offset = new URShiftXNode(raw_rbtrue, phase->igvn().intcon(ShenandoahHeapRegion::region_size_bytes_shift_jint()));
2394     phase->register_new_node(cset_offset, ctrl);
2395     Node* in_cset_fast_test_base_addr = phase->igvn().makecon(TypeRawPtr::make(ShenandoahHeap::in_cset_fast_test_addr()));
2396     phase->set_ctrl(in_cset_fast_test_base_addr, phase->C->root());
2397     Node* in_cset_fast_test_adr = new AddPNode(phase->C->top(), in_cset_fast_test_base_addr, cset_offset);
2398     phase->register_new_node(in_cset_fast_test_adr, ctrl);
2399     uint in_cset_fast_test_idx = Compile::AliasIdxRaw;
2400     const TypePtr* in_cset_fast_test_adr_type = NULL; // debug-mode-only argument
2401     debug_only(in_cset_fast_test_adr_type = phase->C->get_adr_type(in_cset_fast_test_idx));
2402     Node* in_cset_fast_test_load = new LoadBNode(ctrl, raw_mem, in_cset_fast_test_adr, in_cset_fast_test_adr_type, TypeInt::BYTE, MemNode::unordered);
2403     phase->register_new_node(in_cset_fast_test_load, ctrl);
2404     Node* in_cset_fast_test_cmp = new CmpINode(in_cset_fast_test_load, phase->igvn().zerocon(T_INT));
2405     phase->register_new_node(in_cset_fast_test_cmp, ctrl);
2406     Node* in_cset_fast_test_test = new BoolNode(in_cset_fast_test_cmp, BoolTest::eq);
2407     phase->register_new_node(in_cset_fast_test_test, ctrl);
2408     IfNode* in_cset_fast_test_iff = new IfNode(ctrl, in_cset_fast_test_test, PROB_UNLIKELY(0.999), COUNT_UNKNOWN);
2409     phase->register_control(in_cset_fast_test_iff, loop, ctrl);
2410 
2411     not_cset_ctrl = new IfTrueNode(in_cset_fast_test_iff);
2412     phase->register_control(not_cset_ctrl, loop, in_cset_fast_test_iff);
2413 
2414     ctrl = new IfFalseNode(in_cset_fast_test_iff);
2415     phase->register_control(ctrl, loop, in_cset_fast_test_iff);
2416   }
2417 }
2418 
2419 void ShenandoahWriteBarrierNode::call_wb_stub(Node*& ctrl, Node*& val, Node*& result_mem,
2420                                               Node* raw_mem, Node* wb_mem,
2421                                               int alias,
2422                                               PhaseIdealLoop* phase) {
2423   IdealLoopTree*loop = phase->get_loop(ctrl);
2424   const TypePtr* obj_type = phase->igvn().type(val)->is_oopptr()->cast_to_nonconst();
2425 
2426   // The slow path stub consumes and produces raw memory in addition
2427   // to the existing memory edges
2428   Node* base = find_bottom_mem(ctrl, phase);
2429 
2430   MergeMemNode* mm = MergeMemNode::make(base);
2431   mm->set_memory_at(alias, wb_mem);
2432   mm->set_memory_at(Compile::AliasIdxRaw, raw_mem);
2433   phase->register_new_node(mm, ctrl);
2434 
2435   Node* call = new CallLeafNoFPNode(ShenandoahBarrierSetC2::shenandoah_write_barrier_Type(), ShenandoahBarrierSetAssembler::shenandoah_wb_C(), "shenandoah_write_barrier", TypeRawPtr::BOTTOM);
2436   call->init_req(TypeFunc::Control, ctrl);
2437   call->init_req(TypeFunc::I_O, phase->C->top());
2438   call->init_req(TypeFunc::Memory, mm);
2439   call->init_req(TypeFunc::FramePtr, phase->C->top());
2440   call->init_req(TypeFunc::ReturnAdr, phase->C->top());
2441   call->init_req(TypeFunc::Parms, val);
2442   phase->register_control(call, loop, ctrl);
2443   ctrl = new ProjNode(call, TypeFunc::Control);
2444   phase->register_control(ctrl, loop, call);
2445   result_mem = new ProjNode(call, TypeFunc::Memory);
2446   phase->register_new_node(result_mem, call);
2447   val = new ProjNode(call, TypeFunc::Parms);
2448   phase->register_new_node(val, call);
2449   val = new CheckCastPPNode(ctrl, val, obj_type);
2450   phase->register_new_node(val, ctrl);
2451 }
2452 
2453 void ShenandoahWriteBarrierNode::fix_ctrl(Node* barrier, Node* region, const MemoryGraphFixer& fixer, Unique_Node_List& uses, Unique_Node_List& uses_to_ignore, uint last, PhaseIdealLoop* phase) {
2454   Node* ctrl = phase->get_ctrl(barrier);
2455   Node* init_raw_mem = fixer.find_mem(ctrl, barrier);
2456 
2457   // Update the control of all nodes that should be after the
2458   // barrier control flow
2459   uses.clear();
2460   // Every node that is control dependent on the barrier's input
2461   // control will be after the expanded barrier. The raw memory (if
2462   // its memory is control dependent on the barrier's input control)
2463   // must stay above the barrier.
2464   uses_to_ignore.clear();
2465   if (phase->has_ctrl(init_raw_mem) && phase->get_ctrl(init_raw_mem) == ctrl && !init_raw_mem->is_Phi()) {
2466     uses_to_ignore.push(init_raw_mem);
2467   }
2468   for (uint next = 0; next < uses_to_ignore.size(); next++) {
2469     Node *n = uses_to_ignore.at(next);
2470     for (uint i = 0; i < n->req(); i++) {
2471       Node* in = n->in(i);
2472       if (in != NULL && phase->has_ctrl(in) && phase->get_ctrl(in) == ctrl) {
2473         uses_to_ignore.push(in);
2474       }
2475     }
2476   }
2477   for (DUIterator_Fast imax, i = ctrl->fast_outs(imax); i < imax; i++) {
2478     Node* u = ctrl->fast_out(i);
2479     if (u->_idx < last &&
2480         u != barrier &&
2481         !uses_to_ignore.member(u) &&
2482         (u->in(0) != ctrl || (!u->is_Region() && !u->is_Phi())) &&
2483         (ctrl->Opcode() != Op_CatchProj || u->Opcode() != Op_CreateEx)) {
2484       Node* old_c = phase->ctrl_or_self(u);
2485       Node* c = old_c;
2486       if (c != ctrl ||
2487           is_dominator_same_ctrl(old_c, barrier, u, phase) ||
2488           ShenandoahBarrierSetC2::is_shenandoah_state_load(u)) {
2489         phase->igvn().rehash_node_delayed(u);
2490         int nb = u->replace_edge(ctrl, region);
2491         if (u->is_CFG()) {
2492           if (phase->idom(u) == ctrl) {
2493             phase->set_idom(u, region, phase->dom_depth(region));
2494           }
2495         } else if (phase->get_ctrl(u) == ctrl) {
2496           assert(u != init_raw_mem, "should leave input raw mem above the barrier");
2497           uses.push(u);
2498         }
2499         assert(nb == 1, "more than 1 ctrl input?");
2500         --i, imax -= nb;
2501       }
2502     }
2503   }
2504 }
2505 
2506 void ShenandoahWriteBarrierNode::pin_and_expand(PhaseIdealLoop* phase) {
2507   Node_List enqueue_barriers;
2508   if (ShenandoahStoreValEnqueueBarrier) {
2509     Unique_Node_List wq;
2510     wq.push(phase->C->root());
2511     for (uint i = 0; i < wq.size(); i++) {
2512       Node* n = wq.at(i);
2513       if (n->Opcode() == Op_ShenandoahEnqueueBarrier) {
2514         enqueue_barriers.push(n);
2515       }
2516       for (uint i = 0; i < n->req(); i++) {
2517         Node* in = n->in(i);
2518         if (in != NULL) {
2519           wq.push(in);
2520         }
2521       }
2522     }
2523   }
2524 
2525   const bool trace = false;
2526 
2527   // Collect raw memory state at CFG points in the entire graph and
2528   // record it in memory_nodes. Optimize the raw memory graph in the
2529   // process. Optimizing the memory graph also makes the memory graph
2530   // simpler.
2531   GrowableArray<MemoryGraphFixer*> memory_graph_fixers;
2532 
2533   // Let's try to common write barriers again
2534   optimize_before_expansion(phase, memory_graph_fixers, true);
2535 
2536   Unique_Node_List uses;
2537   for (int i = 0; i < ShenandoahBarrierSetC2::bsc2()->state()->shenandoah_barriers_count(); i++) {
2538     ShenandoahWriteBarrierNode* wb = ShenandoahBarrierSetC2::bsc2()->state()->shenandoah_barrier(i);
2539     Node* ctrl = phase->get_ctrl(wb);
2540 
2541     Node* val = wb->in(ValueIn);
2542     if (ctrl->is_Proj() && ctrl->in(0)->is_CallJava()) {
2543       assert(is_dominator(phase->get_ctrl(val), ctrl->in(0)->in(0), val, ctrl->in(0), phase), "can't move");
2544       phase->set_ctrl(wb, ctrl->in(0)->in(0));
2545     } else if (ctrl->is_CallRuntime()) {
2546       assert(is_dominator(phase->get_ctrl(val), ctrl->in(0), val, ctrl, phase), "can't move");
2547       phase->set_ctrl(wb, ctrl->in(0));
2548     }
2549 
2550     assert(wb->Opcode() == Op_ShenandoahWriteBarrier, "only for write barriers");
2551     // Look for a null check that dominates this barrier and move the
2552     // barrier right after the null check to enable implicit null
2553     // checks
2554     wb->pin_and_expand_move_barrier(phase, memory_graph_fixers, uses);
2555 
2556     wb->pin_and_expand_helper(phase);
2557   }
2558 
2559   MemoryGraphFixer fixer(Compile::AliasIdxRaw, true, phase);
2560   Unique_Node_List uses_to_ignore;
2561   for (uint i = 0; i < enqueue_barriers.size(); i++) {
2562     Node* barrier = enqueue_barriers.at(i);
2563     Node* pre_val = barrier->in(1);
2564 
2565     if (phase->igvn().type(pre_val)->higher_equal(TypePtr::NULL_PTR)) {
2566       ShouldNotReachHere();
2567       continue;
2568     }
2569 
2570     Node* ctrl = phase->get_ctrl(barrier);
2571 
2572     if (ctrl->is_Proj() && ctrl->in(0)->is_CallJava()) {
2573       assert(is_dominator(phase->get_ctrl(pre_val), ctrl->in(0)->in(0), pre_val, ctrl->in(0), phase), "can't move");
2574       ctrl = ctrl->in(0)->in(0);
2575       phase->set_ctrl(barrier, ctrl);
2576     } else if (ctrl->is_CallRuntime()) {
2577       assert(is_dominator(phase->get_ctrl(pre_val), ctrl->in(0), pre_val, ctrl, phase), "can't move");
2578       ctrl = ctrl->in(0);
2579       phase->set_ctrl(barrier, ctrl);
2580     }
2581 
2582     Node* init_ctrl = ctrl;
2583     IdealLoopTree* loop = phase->get_loop(ctrl);
2584     Node* raw_mem = fixer.find_mem(ctrl, barrier);
2585     Node* init_raw_mem = raw_mem;
2586     Node* raw_mem_for_ctrl = fixer.find_mem(ctrl, NULL);
2587     Node* heap_stable_ctrl = NULL;
2588     Node* null_ctrl = NULL;
2589     uint last = phase->C->unique();
2590 
2591     enum { _heap_stable = 1, _heap_unstable, PATH_LIMIT };
2592     Node* region = new RegionNode(PATH_LIMIT);
2593     Node* phi = PhiNode::make(region, raw_mem, Type::MEMORY, TypeRawPtr::BOTTOM);
2594 
2595     enum { _fast_path = 1, _slow_path, _null_path, PATH_LIMIT2 };
2596     Node* region2 = new RegionNode(PATH_LIMIT2);
2597     Node* phi2 = PhiNode::make(region2, raw_mem, Type::MEMORY, TypeRawPtr::BOTTOM);
2598 
2599     // Stable path.
2600     test_heap_stable(ctrl, raw_mem, heap_stable_ctrl, phase);
2601     region->init_req(_heap_stable, heap_stable_ctrl);
2602     phi->init_req(_heap_stable, raw_mem);
2603 
2604     // Null path
2605     Node* reg2_ctrl = NULL;
2606     test_null(ctrl, pre_val, null_ctrl, phase);
2607     if (null_ctrl != NULL) {
2608       reg2_ctrl = null_ctrl->in(0);
2609       region2->init_req(_null_path, null_ctrl);
2610       phi2->init_req(_null_path, raw_mem);
2611     } else {
2612       region2->del_req(_null_path);
2613       phi2->del_req(_null_path);
2614     }
2615 
2616     const int index_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset());
2617     const int buffer_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset());
2618     Node* thread = new ThreadLocalNode();
2619     phase->register_new_node(thread, ctrl);
2620     Node* buffer_adr = new AddPNode(phase->C->top(), thread, phase->igvn().MakeConX(buffer_offset));
2621     phase->register_new_node(buffer_adr, ctrl);
2622     Node* index_adr = new AddPNode(phase->C->top(), thread, phase->igvn().MakeConX(index_offset));
2623     phase->register_new_node(index_adr, ctrl);
2624 
2625     BasicType index_bt = TypeX_X->basic_type();
2626     assert(sizeof(size_t) == type2aelembytes(index_bt), "Loading G1 SATBMarkQueue::_index with wrong size.");
2627     const TypePtr* adr_type = TypeRawPtr::BOTTOM;
2628     Node* index = new LoadXNode(ctrl, raw_mem, index_adr, adr_type, TypeX_X, MemNode::unordered);
2629     phase->register_new_node(index, ctrl);
2630     Node* index_cmp = new CmpXNode(index, phase->igvn().MakeConX(0));
2631     phase->register_new_node(index_cmp, ctrl);
2632     Node* index_test = new BoolNode(index_cmp, BoolTest::ne);
2633     phase->register_new_node(index_test, ctrl);
2634     IfNode* queue_full_iff = new IfNode(ctrl, index_test, PROB_LIKELY(0.999), COUNT_UNKNOWN);
2635     if (reg2_ctrl == NULL) reg2_ctrl = queue_full_iff;
2636     phase->register_control(queue_full_iff, loop, ctrl);
2637     Node* not_full = new IfTrueNode(queue_full_iff);
2638     phase->register_control(not_full, loop, queue_full_iff);
2639     Node* full = new IfFalseNode(queue_full_iff);
2640     phase->register_control(full, loop, queue_full_iff);
2641 
2642     ctrl = not_full;
2643 
2644     Node* next_index = new SubXNode(index, phase->igvn().MakeConX(sizeof(intptr_t)));
2645     phase->register_new_node(next_index, ctrl);
2646 
2647     Node* buffer  = new LoadPNode(ctrl, raw_mem, buffer_adr, adr_type, TypeRawPtr::NOTNULL, MemNode::unordered);
2648     phase->register_new_node(buffer, ctrl);
2649     Node *log_addr = new AddPNode(phase->C->top(), buffer, next_index);
2650     phase->register_new_node(log_addr, ctrl);
2651     Node* log_store = new StorePNode(ctrl, raw_mem, log_addr, adr_type, pre_val, MemNode::unordered);
2652     phase->register_new_node(log_store, ctrl);
2653     // update the index
2654     Node* index_update = new StoreXNode(ctrl, log_store, index_adr, adr_type, next_index, MemNode::unordered);
2655     phase->register_new_node(index_update, ctrl);
2656 
2657     // Fast-path case
2658     region2->init_req(_fast_path, ctrl);
2659     phi2->init_req(_fast_path, index_update);
2660 
2661     ctrl = full;
2662 
2663     Node* base = find_bottom_mem(ctrl, phase);
2664 
2665     MergeMemNode* mm = MergeMemNode::make(base);
2666     mm->set_memory_at(Compile::AliasIdxRaw, raw_mem);
2667     phase->register_new_node(mm, ctrl);
2668 
2669     Node* call = new CallLeafNode(ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type(), CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), "shenandoah_wb_pre", TypeRawPtr::BOTTOM);
2670     call->init_req(TypeFunc::Control, ctrl);
2671     call->init_req(TypeFunc::I_O, phase->C->top());
2672     call->init_req(TypeFunc::Memory, mm);
2673     call->init_req(TypeFunc::FramePtr, phase->C->top());
2674     call->init_req(TypeFunc::ReturnAdr, phase->C->top());
2675     call->init_req(TypeFunc::Parms, pre_val);
2676     call->init_req(TypeFunc::Parms+1, thread);
2677     phase->register_control(call, loop, ctrl);
2678 
2679     Node* ctrl_proj = new ProjNode(call, TypeFunc::Control);
2680     phase->register_control(ctrl_proj, loop, call);
2681     Node* mem_proj = new ProjNode(call, TypeFunc::Memory);
2682     phase->register_new_node(mem_proj, call);
2683 
2684     // Slow-path case
2685     region2->init_req(_slow_path, ctrl_proj);
2686     phi2->init_req(_slow_path, mem_proj);
2687 
2688     phase->register_control(region2, loop, reg2_ctrl);
2689     phase->register_new_node(phi2, region2);
2690 
2691     region->init_req(_heap_unstable, region2);
2692     phi->init_req(_heap_unstable, phi2);
2693 
2694     phase->register_control(region, loop, heap_stable_ctrl->in(0));
2695     phase->register_new_node(phi, region);
2696 
2697     fix_ctrl(barrier, region, fixer, uses, uses_to_ignore, last, phase);
2698     for(uint next = 0; next < uses.size(); next++ ) {
2699       Node *n = uses.at(next);
2700       assert(phase->get_ctrl(n) == init_ctrl, "bad control");
2701       assert(n != init_raw_mem, "should leave input raw mem above the barrier");
2702       phase->set_ctrl(n, region);
2703       follow_barrier_uses(n, init_ctrl, uses, phase);
2704     }
2705     fixer.fix_mem(init_ctrl, region, init_raw_mem, raw_mem_for_ctrl, phi, uses);
2706 
2707     phase->igvn().replace_node(barrier, pre_val);
2708   }
2709 
2710   for (int i = ShenandoahBarrierSetC2::bsc2()->state()->shenandoah_barriers_count(); i > 0; i--) {
2711     int cnt = ShenandoahBarrierSetC2::bsc2()->state()->shenandoah_barriers_count();
2712     ShenandoahWriteBarrierNode* wb = ShenandoahBarrierSetC2::bsc2()->state()->shenandoah_barrier(i-1);
2713 
2714     uint last = phase->C->unique();
2715     Node* ctrl = phase->get_ctrl(wb);
2716     Node* orig_ctrl = ctrl;
2717 
2718     Node* raw_mem = fixer.find_mem(ctrl, wb);
2719     Node* init_raw_mem = raw_mem;
2720     Node* raw_mem_for_ctrl = fixer.find_mem(ctrl, NULL);
2721     int alias = phase->C->get_alias_index(wb->adr_type());
2722     Node* wb_mem =  wb->in(Memory);
2723     Node* init_wb_mem = wb_mem;
2724 
2725     Node* val = wb->in(ValueIn);
2726     Node* wbproj = wb->find_out_with(Op_ShenandoahWBMemProj);
2727     IdealLoopTree *loop = phase->get_loop(ctrl);
2728 
2729     assert(val->Opcode() != Op_ShenandoahWriteBarrier, "No chain of write barriers");
2730 
2731     CallStaticJavaNode* unc = wb->pin_and_expand_null_check(phase->igvn());
2732     Node* unc_ctrl = NULL;
2733     if (unc != NULL) {
2734       if (val->in(0) != ctrl) {
2735         unc = NULL;
2736       } else {
2737         unc_ctrl = val->in(0);
2738       }
2739     }
2740 
2741     Node* uncasted_val = val;
2742     if (unc != NULL) {
2743       uncasted_val = val->in(1);
2744     }
2745 
2746     Node* heap_stable_ctrl = NULL;
2747     Node* null_ctrl = NULL;
2748 
2749     assert(val->bottom_type()->make_oopptr(), "need oop");
2750     assert(val->bottom_type()->make_oopptr()->const_oop() == NULL, "expect non-constant");
2751 
2752     enum { _heap_stable = 1, _heap_unstable, PATH_LIMIT };
2753     Node* region = new RegionNode(PATH_LIMIT);
2754     Node* val_phi = new PhiNode(region, uncasted_val->bottom_type()->is_oopptr());
2755     Node* mem_phi = PhiNode::make(region, wb_mem, Type::MEMORY, phase->C->alias_type(wb->adr_type())->adr_type());
2756     Node* raw_mem_phi = PhiNode::make(region, raw_mem, Type::MEMORY, TypeRawPtr::BOTTOM);
2757 
2758     enum { _not_cset = 1, _not_equal, _evac_path, _null_path, PATH_LIMIT2 };
2759     Node* region2 = new RegionNode(PATH_LIMIT2);
2760     Node* val_phi2 = new PhiNode(region2, uncasted_val->bottom_type()->is_oopptr());
2761     Node* mem_phi2 = PhiNode::make(region2, wb_mem, Type::MEMORY, phase->C->alias_type(wb->adr_type())->adr_type());
2762     Node* raw_mem_phi2 = PhiNode::make(region2, raw_mem, Type::MEMORY, TypeRawPtr::BOTTOM);
2763 
2764       // Stable path.
2765     test_heap_stable(ctrl, raw_mem, heap_stable_ctrl, phase);
2766     IfNode* heap_stable_iff = heap_stable_ctrl->in(0)->as_If();
2767 
2768     // Heap stable case
2769     region->init_req(_heap_stable, heap_stable_ctrl);
2770     val_phi->init_req(_heap_stable, uncasted_val);
2771     mem_phi->init_req(_heap_stable, wb_mem);
2772     raw_mem_phi->init_req(_heap_stable, raw_mem);
2773 
2774     Node* reg2_ctrl = NULL;
2775     // Null case
2776     test_null(ctrl, val, null_ctrl, phase);
2777     if (null_ctrl != NULL) {
2778       reg2_ctrl = null_ctrl->in(0);
2779       region2->init_req(_null_path, null_ctrl);
2780       val_phi2->init_req(_null_path, uncasted_val);
2781       mem_phi2->init_req(_null_path, wb_mem);
2782       raw_mem_phi2->init_req(_null_path, raw_mem);
2783     } else {
2784       region2->del_req(_null_path);
2785       val_phi2->del_req(_null_path);
2786       mem_phi2->del_req(_null_path);
2787       raw_mem_phi2->del_req(_null_path);
2788     }
2789 
2790     // Test for in-cset.
2791     // Wires !in_cset(obj) to slot 2 of region and phis
2792     Node* not_cset_ctrl = NULL;
2793     in_cset_fast_test(ctrl, not_cset_ctrl, uncasted_val, raw_mem, phase);
2794     if (not_cset_ctrl != NULL) {
2795       if (reg2_ctrl == NULL) reg2_ctrl = not_cset_ctrl->in(0);
2796       region2->init_req(_not_cset, not_cset_ctrl);
2797       val_phi2->init_req(_not_cset, uncasted_val);
2798       mem_phi2->init_req(_not_cset, wb_mem);
2799       raw_mem_phi2->init_req(_not_cset, raw_mem);
2800     }
2801 
2802     // Resolve object when orig-value is in cset.
2803     // Make the unconditional resolve for fwdptr, not the read barrier.
2804     Node* new_val = uncasted_val;
2805     if (unc_ctrl != NULL) {
2806       // Clone the null check in this branch to allow implicit null check
2807       new_val = clone_null_check(ctrl, val, unc_ctrl, phase);
2808       fix_null_check(unc, unc_ctrl, ctrl->in(0)->as_If()->proj_out(0), uses, phase);
2809 
2810       IfNode* iff = unc_ctrl->in(0)->as_If();
2811       phase->igvn().replace_input_of(iff, 1, phase->igvn().intcon(1));
2812     }
2813     Node* addr = new AddPNode(new_val, uncasted_val, phase->igvn().MakeConX(BrooksPointer::byte_offset()));
2814     phase->register_new_node(addr, ctrl);
2815     assert(val->bottom_type()->isa_oopptr(), "what else?");
2816     const TypePtr* obj_type =  val->bottom_type()->is_oopptr();
2817     const TypePtr* adr_type = ShenandoahBarrierNode::brooks_pointer_type(obj_type);
2818     Node* fwd = new LoadPNode(ctrl, wb_mem, addr, adr_type, obj_type, MemNode::unordered);
2819     phase->register_new_node(fwd, ctrl);
2820 
2821     // Only branch to WB stub if object is not forwarded; otherwise reply with fwd ptr
2822     Node* cmp = new CmpPNode(fwd, new_val);
2823     phase->register_new_node(cmp, ctrl);
2824     Node* bol = new BoolNode(cmp, BoolTest::eq);
2825     phase->register_new_node(bol, ctrl);
2826 
2827     IfNode* iff = new IfNode(ctrl, bol, PROB_UNLIKELY(0.999), COUNT_UNKNOWN);
2828     if (reg2_ctrl == NULL) reg2_ctrl = iff;
2829     phase->register_control(iff, loop, ctrl);
2830     Node* if_not_eq = new IfFalseNode(iff);
2831     phase->register_control(if_not_eq, loop, iff);
2832     Node* if_eq = new IfTrueNode(iff);
2833     phase->register_control(if_eq, loop, iff);
2834 
2835     // Wire up not-equal-path in slots 3.
2836     region2->init_req(_not_equal, if_not_eq);
2837     val_phi2->init_req(_not_equal, fwd);
2838     mem_phi2->init_req(_not_equal, wb_mem);
2839     raw_mem_phi2->init_req(_not_equal, raw_mem);
2840 
2841     // Call wb-stub and wire up that path in slots 4
2842     Node* result_mem = NULL;
2843     ctrl = if_eq;
2844     call_wb_stub(ctrl, new_val, result_mem,
2845                  raw_mem, wb_mem,
2846                  alias, phase);
2847     region2->init_req(_evac_path, ctrl);
2848     val_phi2->init_req(_evac_path, new_val);
2849     mem_phi2->init_req(_evac_path, result_mem);
2850     raw_mem_phi2->init_req(_evac_path, result_mem);
2851 
2852     phase->register_control(region2, loop, reg2_ctrl);
2853     phase->register_new_node(val_phi2, region2);
2854     phase->register_new_node(mem_phi2, region2);
2855     phase->register_new_node(raw_mem_phi2, region2);
2856 
2857     region->init_req(_heap_unstable, region2);
2858     val_phi->init_req(_heap_unstable, val_phi2);
2859     mem_phi->init_req(_heap_unstable, mem_phi2);
2860     raw_mem_phi->init_req(_heap_unstable, raw_mem_phi2);
2861 
2862     phase->register_control(region, loop, heap_stable_iff);
2863     Node* out_val = val_phi;
2864     phase->register_new_node(val_phi, region);
2865     phase->register_new_node(mem_phi, region);
2866     phase->register_new_node(raw_mem_phi, region);
2867 
2868     fix_ctrl(wb, region, fixer, uses, uses_to_ignore, last, phase);
2869 
2870     ctrl = orig_ctrl;
2871 
2872     phase->igvn().replace_input_of(wbproj, ShenandoahWBMemProjNode::WriteBarrier, phase->C->top());
2873     phase->igvn().replace_node(wbproj, mem_phi);
2874     if (unc != NULL) {
2875       for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
2876         Node* u = val->fast_out(i);
2877         Node* c = phase->ctrl_or_self(u);
2878         if (u != wb && (c != ctrl || is_dominator_same_ctrl(c, wb, u, phase))) {
2879           phase->igvn().rehash_node_delayed(u);
2880           int nb = u->replace_edge(val, out_val);
2881           --i, imax -= nb;
2882         }
2883       }
2884       if (val->outcnt() == 0) {
2885         phase->igvn()._worklist.push(val);
2886       }
2887     }
2888     phase->igvn().replace_node(wb, out_val);
2889 
2890     follow_barrier_uses(mem_phi, ctrl, uses, phase);
2891     follow_barrier_uses(out_val, ctrl, uses, phase);
2892 
2893     for(uint next = 0; next < uses.size(); next++ ) {
2894       Node *n = uses.at(next);
2895       assert(phase->get_ctrl(n) == ctrl, "bad control");
2896       assert(n != init_raw_mem, "should leave input raw mem above the barrier");
2897       phase->set_ctrl(n, region);
2898       follow_barrier_uses(n, ctrl, uses, phase);
2899     }
2900 
2901     // The slow path call produces memory: hook the raw memory phi
2902     // from the expanded write barrier with the rest of the graph
2903     // which may require adding memory phis at every post dominated
2904     // region and at enclosing loop heads. Use the memory state
2905     // collected in memory_nodes to fix the memory graph. Update that
2906     // memory state as we go.
2907     fixer.fix_mem(ctrl, region, init_raw_mem, raw_mem_for_ctrl, raw_mem_phi, uses);
2908     assert(ShenandoahBarrierSetC2::bsc2()->state()->shenandoah_barriers_count() == cnt - 1, "not replaced");
2909   }
2910 
2911   assert(ShenandoahBarrierSetC2::bsc2()->state()->shenandoah_barriers_count() == 0, "all write barrier nodes should have been replaced");
2912 }
2913 
2914 void ShenandoahWriteBarrierNode::move_heap_stable_test_out_of_loop(IfNode* iff, PhaseIdealLoop* phase) {
2915   IdealLoopTree *loop = phase->get_loop(iff);
2916   Node* loop_head = loop->_head;
2917   Node* entry_c = loop_head->in(LoopNode::EntryControl);
2918 
2919   Node* load = iff->in(1)->in(1)->in(1)->in(1);
2920   assert(is_gc_state_load(load), "broken");
2921   if (!phase->is_dominator(load->in(0), entry_c)) {
2922     Node* mem_ctrl = NULL;
2923     Node* mem = dom_mem(load->in(MemNode::Memory), loop_head, Compile::AliasIdxRaw, mem_ctrl, phase);
2924     phase->igvn().replace_input_of(load, MemNode::Memory, mem);
2925     phase->igvn().replace_input_of(load, 0, entry_c);
2926     phase->set_ctrl_and_loop(load, entry_c);
2927   }
2928 }
2929 
2930 void ShenandoahWriteBarrierNode::merge_back_to_back_tests(Node* n, PhaseIdealLoop* phase) {
2931   assert(is_heap_stable_test(n), "no other tests");
2932   if (phase->identical_backtoback_ifs(n)) {
2933     Node* n_ctrl = n->in(0);
2934     if (phase->can_split_if(n_ctrl)) {
2935       IfNode* dom_if = phase->idom(n_ctrl)->as_If();
2936       if (is_heap_stable_test(n)) {
2937         Node* gc_state_load = n->in(1)->in(1)->in(1)->in(1);
2938         assert(is_gc_state_load(gc_state_load), "broken");
2939         Node* dom_gc_state_load = dom_if->in(1)->in(1)->in(1)->in(1);
2940         assert(is_gc_state_load(dom_gc_state_load), "broken");
2941         if (gc_state_load != dom_gc_state_load) {
2942           phase->igvn().replace_node(gc_state_load, dom_gc_state_load);
2943         }
2944       }
2945       PhiNode* bolphi = PhiNode::make_blank(n_ctrl, n->in(1));
2946       Node* proj_true = dom_if->proj_out(1);
2947       Node* proj_false = dom_if->proj_out(0);
2948       Node* con_true = phase->igvn().makecon(TypeInt::ONE);
2949       Node* con_false = phase->igvn().makecon(TypeInt::ZERO);
2950 
2951       for (uint i = 1; i < n_ctrl->req(); i++) {
2952         if (phase->is_dominator(proj_true, n_ctrl->in(i))) {
2953           bolphi->init_req(i, con_true);
2954         } else {
2955           assert(phase->is_dominator(proj_false, n_ctrl->in(i)), "bad if");
2956           bolphi->init_req(i, con_false);
2957         }
2958       }
2959       phase->register_new_node(bolphi, n_ctrl);
2960       phase->igvn().replace_input_of(n, 1, bolphi);
2961       phase->do_split_if(n);
2962     }
2963   }
2964 }
2965 
2966 void ShenandoahWriteBarrierNode::optimize_after_expansion(VectorSet &visited, Node_Stack &stack, Node_List &old_new, PhaseIdealLoop* phase) {
2967   Node_List heap_stable_tests;
2968   Node_List gc_state_loads;
2969 
2970   stack.push(phase->C->start(), 0);
2971   do {
2972     Node* n = stack.node();
2973     uint i = stack.index();
2974 
2975     if (i < n->outcnt()) {
2976       Node* u = n->raw_out(i);
2977       stack.set_index(i+1);
2978       if (!visited.test_set(u->_idx)) {
2979         stack.push(u, 0);
2980       }
2981     } else {
2982       stack.pop();
2983       if (ShenandoahCommonGCStateLoads && ShenandoahWriteBarrierNode::is_gc_state_load(n)) {
2984         gc_state_loads.push(n);
2985       }
2986       if (n->is_If() && ShenandoahWriteBarrierNode::is_heap_stable_test(n)) {
2987         heap_stable_tests.push(n);
2988       }
2989     }
2990   } while (stack.size() > 0);
2991 
2992   bool progress;
2993   do {
2994     progress = false;
2995     for (uint i = 0; i < gc_state_loads.size(); i++) {
2996       Node* n = gc_state_loads.at(i);
2997       if (n->outcnt() != 0) {
2998         progress |= ShenandoahWriteBarrierNode::try_common_gc_state_load(n, phase);
2999       }
3000     }
3001   } while (progress);
3002 
3003   for (uint i = 0; i < heap_stable_tests.size(); i++) {
3004     Node* n = heap_stable_tests.at(i);
3005     assert(is_heap_stable_test(n), "only evacuation test");
3006     merge_back_to_back_tests(n, phase);
3007   }
3008 
3009   if (!phase->C->major_progress()) {
3010     VectorSet seen(Thread::current()->resource_area());
3011     for (uint i = 0; i < heap_stable_tests.size(); i++) {
3012       Node* n = heap_stable_tests.at(i);
3013       IdealLoopTree* loop = phase->get_loop(n);
3014       if (loop != phase->ltree_root() &&
3015           loop->_child == NULL &&
3016           !loop->_irreducible) {
3017         LoopNode* head = loop->_head->as_Loop();
3018         if ((!head->is_CountedLoop() || head->as_CountedLoop()->is_main_loop() || head->as_CountedLoop()->is_normal_loop()) &&
3019             !seen.test_set(head->_idx) &&
3020             loop->policy_unswitching(phase, true)) {
3021           IfNode* iff = phase->find_unswitching_candidate(loop, true);
3022           if (iff != NULL && is_heap_stable_test(iff)) {
3023             if (head->is_strip_mined()) {
3024               head->verify_strip_mined(0);
3025               OuterStripMinedLoopNode* outer = head->as_CountedLoop()->outer_loop();
3026               OuterStripMinedLoopEndNode* le = head->outer_loop_end();
3027               Node* new_outer = new LoopNode(outer->in(LoopNode::EntryControl), outer->in(LoopNode::LoopBackControl));
3028               phase->register_control(new_outer, phase->get_loop(outer), outer->in(LoopNode::EntryControl));
3029               Node* new_le = new IfNode(le->in(0), le->in(1), le->_prob, le->_fcnt);
3030               phase->register_control(new_le, phase->get_loop(le), le->in(0));
3031               phase->lazy_replace(outer, new_outer);
3032               phase->lazy_replace(le, new_le);
3033               head->clear_strip_mined();
3034             }
3035             phase->do_unswitching(loop, old_new, true);
3036           }
3037         }
3038       }
3039     }
3040   }
3041 }
3042 
3043 #ifdef ASSERT
3044 void ShenandoahBarrierNode::verify_raw_mem(RootNode* root) {
3045   const bool trace = false;
3046   ResourceMark rm;
3047   Unique_Node_List nodes;
3048   Unique_Node_List controls;
3049   Unique_Node_List memories;
3050 
3051   nodes.push(root);
3052   for (uint next = 0; next < nodes.size(); next++) {
3053     Node *n  = nodes.at(next);
3054     if (n->Opcode() == Op_CallLeafNoFP &&
3055         ShenandoahBarrierSetAssembler::is_shenandoah_wb_C_call(n->as_Call()->entry_point())) {
3056       controls.push(n);
3057       if (trace) { tty->print("XXXXXX verifying"); n->dump(); }
3058       for (uint next2 = 0; next2 < controls.size(); next2++) {
3059         Node *m = controls.at(next2);
3060         for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax; i++) {
3061           Node* u = m->fast_out(i);
3062           if (u->is_CFG() && !u->is_Root() &&
3063               !(u->Opcode() == Op_CProj && u->in(0)->Opcode() == Op_NeverBranch && u->as_Proj()->_con == 1) &&
3064               !(u->is_Region() && u->unique_ctrl_out()->Opcode() == Op_Halt)) {
3065             if (trace) { tty->print("XXXXXX pushing control"); u->dump(); }
3066             controls.push(u);
3067           }
3068         }
3069       }
3070       memories.push(n->as_Call()->proj_out(TypeFunc::Memory));
3071       for (uint next2 = 0; next2 < memories.size(); next2++) {
3072         Node *m = memories.at(next2);
3073         assert(m->bottom_type() == Type::MEMORY, "");
3074         for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax; i++) {
3075           Node* u = m->fast_out(i);
3076           if (u->bottom_type() == Type::MEMORY && (u->is_Mem() || u->is_ClearArray())) {
3077             if (trace) { tty->print("XXXXXX pushing memory"); u->dump(); }
3078             memories.push(u);
3079           } else if (u->is_LoadStore()) {
3080             if (trace) { tty->print("XXXXXX pushing memory"); u->find_out_with(Op_SCMemProj)->dump(); }
3081             memories.push(u->find_out_with(Op_SCMemProj));
3082           } else if (u->is_MergeMem() && u->as_MergeMem()->memory_at(Compile::AliasIdxRaw) == m) {
3083             if (trace) { tty->print("XXXXXX pushing memory"); u->dump(); }
3084             memories.push(u);
3085           } else if (u->is_Phi()) {
3086             assert(u->bottom_type() == Type::MEMORY, "");
3087             if (u->adr_type() == TypeRawPtr::BOTTOM || u->adr_type() == TypePtr::BOTTOM) {
3088               assert(controls.member(u->in(0)), "");
3089               if (trace) { tty->print("XXXXXX pushing memory"); u->dump(); }
3090               memories.push(u);
3091             }
3092           } else if (u->is_SafePoint() || u->is_MemBar()) {
3093             for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) {
3094               Node* uu = u->fast_out(j);
3095               if (uu->bottom_type() == Type::MEMORY) {
3096                 if (trace) { tty->print("XXXXXX pushing memory"); uu->dump(); }
3097                 memories.push(uu);
3098               }
3099             }
3100           }
3101         }
3102       }
3103       for (uint next2 = 0; next2 < controls.size(); next2++) {
3104         Node *m = controls.at(next2);
3105         if (m->is_Region()) {
3106           bool all_in = true;
3107           for (uint i = 1; i < m->req(); i++) {
3108             if (!controls.member(m->in(i))) {
3109               all_in = false;
3110               break;
3111             }
3112           }
3113           if (trace) { tty->print("XXX verifying %s", all_in ? "all in" : ""); m->dump(); }
3114           bool found_phi = false;
3115           for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax && !found_phi; j++) {
3116             Node* u = m->fast_out(j);
3117             if (u->is_Phi() && memories.member(u)) {
3118               found_phi = true;
3119               for (uint i = 1; i < u->req() && found_phi; i++) {
3120                 Node* k = u->in(i);
3121                 if (memories.member(k) != controls.member(m->in(i))) {
3122                   found_phi = false;
3123                 }
3124               }
3125             }
3126           }
3127           assert(found_phi || all_in, "");
3128         }
3129       }
3130       controls.clear();
3131       memories.clear();
3132     }
3133     for( uint i = 0; i < n->len(); ++i ) {
3134       Node *m = n->in(i);
3135       if (m != NULL) {
3136         nodes.push(m);
3137       }
3138     }
3139   }
3140 }
3141 #endif
3142 
3143 const Type* ShenandoahEnqueueBarrierNode::bottom_type() const {
3144   if (in(1) == NULL || in(1)->is_top()) {
3145     return Type::TOP;
3146   }
3147   const Type* t = in(1)->bottom_type();
3148   if (t == TypePtr::NULL_PTR) {
3149     return t;
3150   }
3151   return t->is_oopptr()->cast_to_nonconst();
3152 }
3153 
3154 const Type* ShenandoahEnqueueBarrierNode::Value(PhaseGVN* phase) const {
3155   if (in(1) == NULL) {
3156     return Type::TOP;
3157   }
3158   const Type* t = phase->type(in(1));
3159   if (t == Type::TOP) {
3160     return Type::TOP;
3161   }
3162   if (t == TypePtr::NULL_PTR) {
3163     return t;
3164   }
3165   return t->is_oopptr()->cast_to_nonconst();
3166 }
3167 
3168 int ShenandoahEnqueueBarrierNode::needed(Node* n) {
3169   if (n == NULL ||
3170       n->is_Allocate() ||
3171       n->bottom_type() == TypePtr::NULL_PTR ||
3172       (n->bottom_type()->make_oopptr() != NULL && n->bottom_type()->make_oopptr()->const_oop() != NULL)) {
3173     return NotNeeded;
3174   }
3175   if (n->is_Phi() ||
3176       n->is_CMove()) {
3177     return MaybeNeeded;
3178   }
3179   return Needed;
3180 }
3181 
3182 Node* ShenandoahEnqueueBarrierNode::next(Node* n) {
3183   for (;;) {
3184     if (n == NULL) {
3185       return n;
3186     } else if (n->bottom_type() == TypePtr::NULL_PTR) {
3187       return n;
3188     } else if (n->bottom_type()->make_oopptr() != NULL && n->bottom_type()->make_oopptr()->const_oop() != NULL) {
3189       return n;
3190     } else if (n->is_ConstraintCast() ||
3191                n->Opcode() == Op_DecodeN ||
3192                n->Opcode() == Op_EncodeP) {
3193       n = n->in(1);
3194     } else if (n->is_Proj()) {
3195       n = n->in(0);
3196     } else {
3197       return n;
3198     }
3199   }
3200   ShouldNotReachHere();
3201   return NULL;
3202 }
3203 
3204 Node* ShenandoahEnqueueBarrierNode::Identity(PhaseGVN* phase) {
3205   PhaseIterGVN* igvn = phase->is_IterGVN();
3206 
3207   Node* n = next(in(1));
3208 
3209   int cont = needed(n);
3210 
3211   if (cont == NotNeeded) {
3212     return in(1);
3213   } else if (cont == MaybeNeeded) {
3214     if (igvn == NULL) {
3215       phase->record_for_igvn(this);
3216       return this;
3217     } else {
3218       ResourceMark rm;
3219       Unique_Node_List wq;
3220       uint wq_i = 0;
3221 
3222       for (;;) {
3223         if (n->is_Phi()) {
3224           for (uint i = 1; i < n->req(); i++) {
3225             Node* m = n->in(i);
3226             if (m != NULL) {
3227               wq.push(m);
3228             }
3229           }
3230         } else {
3231           assert(n->is_CMove(), "nothing else here");
3232           Node* m = n->in(CMoveNode::IfFalse);
3233           wq.push(m);
3234           m = n->in(CMoveNode::IfTrue);
3235           wq.push(m);
3236         }
3237         Node* orig_n = NULL;
3238         do {
3239           if (wq_i >= wq.size()) {
3240             return in(1);
3241           }
3242           n = wq.at(wq_i);
3243           wq_i++;
3244           orig_n = n;
3245           n = next(n);
3246           cont = needed(n);
3247           if (cont == Needed) {
3248             return this;
3249           }
3250         } while (cont != MaybeNeeded || (orig_n != n && wq.member(n)));
3251       }
3252     }
3253   }
3254 
3255   return this;
3256 }
3257 
3258 #ifdef ASSERT
3259 static bool has_never_branch(Node* root) {
3260   for (uint i = 1; i < root->req(); i++) {
3261     Node* in = root->in(i);
3262     if (in != NULL && in->Opcode() == Op_Halt && in->in(0)->is_Proj() && in->in(0)->in(0)->Opcode() == Op_NeverBranch) {
3263       return true;
3264     }
3265   }
3266   return false;
3267 }
3268 #endif
3269 
3270 void MemoryGraphFixer::collect_memory_nodes() {
3271   Node_Stack stack(0);
3272   VectorSet visited(Thread::current()->resource_area());
3273   Node_List regions;
3274 
3275   // Walk the raw memory graph and create a mapping from CFG node to
3276   // memory node. Exclude phis for now.
3277   stack.push(_phase->C->root(), 1);
3278   do {
3279     Node* n = stack.node();
3280     int opc = n->Opcode();
3281     uint i = stack.index();
3282     if (i < n->req()) {
3283       Node* mem = NULL;
3284       if (opc == Op_Root) {
3285         Node* in = n->in(i);
3286         int in_opc = in->Opcode();
3287         if (in_opc == Op_Return || in_opc == Op_Rethrow) {
3288           mem = in->in(TypeFunc::Memory);
3289         } else if (in_opc == Op_Halt) {
3290           if (!in->in(0)->is_Region()) {
3291             Node* proj = in->in(0);
3292             assert(proj->is_Proj(), "");
3293             Node* in = proj->in(0);
3294             assert(in->is_CallStaticJava() || in->Opcode() == Op_NeverBranch || in->Opcode() == Op_Catch || proj->is_IfProj(), "");
3295             if (in->is_CallStaticJava()) {
3296               mem = in->in(TypeFunc::Memory);
3297             } else if (in->Opcode() == Op_Catch) {
3298               Node* call = in->in(0)->in(0);
3299               assert(call->is_Call(), "");
3300               mem = call->in(TypeFunc::Memory);
3301             }
3302           }
3303         } else {
3304 #ifdef ASSERT
3305           n->dump();
3306           in->dump();
3307 #endif
3308           ShouldNotReachHere();
3309         }
3310       } else {
3311         assert(n->is_Phi() && n->bottom_type() == Type::MEMORY, "");
3312         assert(n->adr_type() == TypePtr::BOTTOM || _phase->C->get_alias_index(n->adr_type()) == _alias, "");
3313         mem = n->in(i);
3314       }
3315       i++;
3316       stack.set_index(i);
3317       if (mem == NULL) {
3318         continue;
3319       }
3320       for (;;) {
3321         if (visited.test_set(mem->_idx) || mem->is_Start()) {
3322           break;
3323         }
3324         if (mem->is_Phi()) {
3325           stack.push(mem, 2);
3326           mem = mem->in(1);
3327         } else if (mem->is_Proj()) {
3328           stack.push(mem, mem->req());
3329           mem = mem->in(0);
3330         } else if (mem->is_SafePoint() || mem->is_MemBar()) {
3331           mem = mem->in(TypeFunc::Memory);
3332         } else if (mem->is_MergeMem()) {
3333           MergeMemNode* mm = mem->as_MergeMem();
3334           mem = mm->memory_at(_alias);
3335         } else if (mem->is_Store() || mem->is_LoadStore() || mem->is_ClearArray()) {
3336           assert(_alias == Compile::AliasIdxRaw, "");
3337           stack.push(mem, mem->req());
3338           mem = mem->in(MemNode::Memory);
3339         } else if (mem->Opcode() == Op_ShenandoahWriteBarrier) {
3340           assert(_alias != Compile::AliasIdxRaw, "");
3341           mem = mem->in(ShenandoahBarrierNode::Memory);
3342         } else if (mem->Opcode() == Op_ShenandoahWBMemProj) {
3343           stack.push(mem, mem->req());
3344           mem = mem->in(ShenandoahWBMemProjNode::WriteBarrier);
3345         } else {
3346 #ifdef ASSERT
3347           mem->dump();
3348 #endif
3349           ShouldNotReachHere();
3350         }
3351       }
3352     } else {
3353       if (n->is_Phi()) {
3354         // Nothing
3355       } else if (!n->is_Root()) {
3356         Node* c = get_ctrl(n);
3357         _memory_nodes.map(c->_idx, n);
3358       }
3359       stack.pop();
3360     }
3361   } while(stack.is_nonempty());
3362 
3363   // Iterate over CFG nodes in rpo and propagate memory state to
3364   // compute memory state at regions, creating new phis if needed.
3365   Node_List rpo_list;
3366   visited.Clear();
3367   _phase->rpo(_phase->C->root(), stack, visited, rpo_list);
3368   Node* root = rpo_list.pop();
3369   assert(root == _phase->C->root(), "");
3370 
3371   const bool trace = false;
3372 #ifdef ASSERT
3373   if (trace) {
3374     for (int i = rpo_list.size() - 1; i >= 0; i--) {
3375       Node* c = rpo_list.at(i);
3376       if (_memory_nodes[c->_idx] != NULL) {
3377         tty->print("X %d", c->_idx);  _memory_nodes[c->_idx]->dump();
3378       }
3379     }
3380   }
3381 #endif
3382   uint last = _phase->C->unique();
3383 
3384 #ifdef ASSERT
3385   uint8_t max_depth = 0;
3386   for (LoopTreeIterator iter(_phase->ltree_root()); !iter.done(); iter.next()) {
3387     IdealLoopTree* lpt = iter.current();
3388     max_depth = MAX2(max_depth, lpt->_nest);
3389   }
3390 #endif
3391 
3392   bool progress = true;
3393   int iteration = 0;
3394   Node_List dead_phis;
3395   while (progress) {
3396     progress = false;
3397     iteration++;
3398     assert(iteration <= 2+max_depth || _phase->C->has_irreducible_loop(), "");
3399     if (trace) { tty->print_cr("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); }
3400     IdealLoopTree* last_updated_ilt = NULL;
3401     for (int i = rpo_list.size() - 1; i >= 0; i--) {
3402       Node* c = rpo_list.at(i);
3403 
3404       Node* prev_mem = _memory_nodes[c->_idx];
3405       if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) {
3406         Node* prev_region = regions[c->_idx];
3407         Node* unique = NULL;
3408         for (uint j = 1; j < c->req() && unique != NodeSentinel; j++) {
3409           Node* m = _memory_nodes[c->in(j)->_idx];
3410           assert(m != NULL || (c->is_Loop() && j == LoopNode::LoopBackControl && iteration == 1) || _phase->C->has_irreducible_loop() || has_never_branch(_phase->C->root()), "expect memory state");
3411           if (m != NULL) {
3412             if (m == prev_region && ((c->is_Loop() && j == LoopNode::LoopBackControl) || (prev_region->is_Phi() && prev_region->in(0) == c))) {
3413               assert(c->is_Loop() && j == LoopNode::LoopBackControl || _phase->C->has_irreducible_loop(), "");
3414               // continue
3415             } else if (unique == NULL) {
3416               unique = m;
3417             } else if (m == unique) {
3418               // continue
3419             } else {
3420               unique = NodeSentinel;
3421             }
3422           }
3423         }
3424         assert(unique != NULL, "empty phi???");
3425         if (unique != NodeSentinel) {
3426           if (prev_region != NULL && prev_region->is_Phi() && prev_region->in(0) == c) {
3427             dead_phis.push(prev_region);
3428           }
3429           regions.map(c->_idx, unique);
3430         } else {
3431           Node* phi = NULL;
3432           if (prev_region != NULL && prev_region->is_Phi() && prev_region->in(0) == c && prev_region->_idx >= last) {
3433             phi = prev_region;
3434             for (uint k = 1; k < c->req(); k++) {
3435               Node* m = _memory_nodes[c->in(k)->_idx];
3436               assert(m != NULL, "expect memory state");
3437               phi->set_req(k, m);
3438             }
3439           } else {
3440             for (DUIterator_Fast jmax, j = c->fast_outs(jmax); j < jmax && phi == NULL; j++) {
3441               Node* u = c->fast_out(j);
3442               if (u->is_Phi() && u->bottom_type() == Type::MEMORY &&
3443                   (u->adr_type() == TypePtr::BOTTOM || _phase->C->get_alias_index(u->adr_type()) == _alias)) {
3444                 phi = u;
3445                 for (uint k = 1; k < c->req() && phi != NULL; k++) {
3446                   Node* m = _memory_nodes[c->in(k)->_idx];
3447                   assert(m != NULL, "expect memory state");
3448                   if (u->in(k) != m) {
3449                     phi = NULL;
3450                   }
3451                 }
3452               }
3453             }
3454             if (phi == NULL) {
3455               phi = new PhiNode(c, Type::MEMORY, _phase->C->get_adr_type(_alias));
3456               for (uint k = 1; k < c->req(); k++) {
3457                 Node* m = _memory_nodes[c->in(k)->_idx];
3458                 assert(m != NULL, "expect memory state");
3459                 phi->init_req(k, m);
3460               }
3461             }
3462           }
3463           assert(phi != NULL, "");
3464           regions.map(c->_idx, phi);
3465         }
3466         Node* current_region = regions[c->_idx];
3467         if (current_region != prev_region) {
3468           progress = true;
3469           if (prev_region == prev_mem) {
3470             _memory_nodes.map(c->_idx, current_region);
3471           }
3472         }
3473       } else if (prev_mem == NULL || prev_mem->is_Phi() || ctrl_or_self(prev_mem) != c) {
3474         Node* m = _memory_nodes[_phase->idom(c)->_idx];
3475         assert(m != NULL, "expect memory state");
3476         if (m != prev_mem) {
3477           _memory_nodes.map(c->_idx, m);
3478           progress = true;
3479         }
3480       }
3481 #ifdef ASSERT
3482       if (trace) { tty->print("X %d", c->_idx);  _memory_nodes[c->_idx]->dump(); }
3483 #endif
3484     }
3485   }
3486 
3487   // Replace existing phi with computed memory state for that region
3488   // if different (could be a new phi or a dominating memory node if
3489   // that phi was found to be useless).
3490   while (dead_phis.size() > 0) {
3491     Node* n = dead_phis.pop();
3492     n->replace_by(_phase->C->top());
3493     n->destruct();
3494   }
3495   for (int i = rpo_list.size() - 1; i >= 0; i--) {
3496     Node* c = rpo_list.at(i);
3497     if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) {
3498       Node* n = regions[c->_idx];
3499       if (n->is_Phi() && n->_idx >= last && n->in(0) == c) {
3500         _phase->register_new_node(n, c);
3501       }
3502     }
3503   }
3504   for (int i = rpo_list.size() - 1; i >= 0; i--) {
3505     Node* c = rpo_list.at(i);
3506     if (c->is_Region() && (_include_lsm || !c->is_OuterStripMinedLoop())) {
3507       Node* n = regions[c->_idx];
3508       for (DUIterator_Fast imax, i = c->fast_outs(imax); i < imax; i++) {
3509         Node* u = c->fast_out(i);
3510         if (u->is_Phi() && u->bottom_type() == Type::MEMORY &&
3511             u != n) {
3512           if (u->adr_type() == TypePtr::BOTTOM) {
3513             fix_memory_uses(u, n, n, c);
3514           } else if (_phase->C->get_alias_index(u->adr_type()) == _alias) {
3515             _phase->lazy_replace(u, n);
3516             --i; --imax;
3517           }
3518         }
3519       }
3520     }
3521   }
3522 }
3523 
3524 Node* MemoryGraphFixer::get_ctrl(Node* n) const {
3525   Node* c = _phase->get_ctrl(n);
3526   if (n->is_Proj() && n->in(0) != NULL && n->in(0)->is_Call()) {
3527     assert(c == n->in(0), "");
3528     CallNode* call = c->as_Call();
3529     CallProjections projs;
3530     call->extract_projections(&projs, true, false);
3531     if (projs.catchall_memproj != NULL) {
3532       if (projs.fallthrough_memproj == n) {
3533         c = projs.fallthrough_catchproj;
3534       } else {
3535         assert(projs.catchall_memproj == n, "");
3536         c = projs.catchall_catchproj;
3537       }
3538     }
3539   }
3540   return c;
3541 }
3542 
3543 Node* MemoryGraphFixer::ctrl_or_self(Node* n) const {
3544   if (_phase->has_ctrl(n))
3545     return get_ctrl(n);
3546   else {
3547     assert (n->is_CFG(), "must be a CFG node");
3548     return n;
3549   }
3550 }
3551 
3552 bool MemoryGraphFixer::mem_is_valid(Node* m, Node* c) const {
3553   return m != NULL && get_ctrl(m) == c;
3554 }
3555 
3556 Node* MemoryGraphFixer::find_mem(Node* ctrl, Node* n) const {
3557   assert(n == NULL || _phase->ctrl_or_self(n) == ctrl, "");
3558   Node* mem = _memory_nodes[ctrl->_idx];
3559   Node* c = ctrl;
3560   while (!mem_is_valid(mem, c) &&
3561          (!c->is_CatchProj() || mem == NULL || c->in(0)->in(0)->in(0) != get_ctrl(mem))) {
3562     c = _phase->idom(c);
3563     mem = _memory_nodes[c->_idx];
3564   }
3565   if (n != NULL && mem_is_valid(mem, c)) {
3566     while (!ShenandoahWriteBarrierNode::is_dominator_same_ctrl(c, mem, n, _phase) && _phase->ctrl_or_self(mem) == ctrl) {
3567       mem = next_mem(mem, _alias);
3568     }
3569     if (mem->is_MergeMem()) {
3570       mem = mem->as_MergeMem()->memory_at(_alias);
3571     }
3572     if (!mem_is_valid(mem, c)) {
3573       do {
3574         c = _phase->idom(c);
3575         mem = _memory_nodes[c->_idx];
3576       } while (!mem_is_valid(mem, c) &&
3577                (!c->is_CatchProj() || mem == NULL || c->in(0)->in(0)->in(0) != get_ctrl(mem)));
3578     }
3579   }
3580   assert(mem->bottom_type() == Type::MEMORY, "");
3581   return mem;
3582 }
3583 
3584 bool MemoryGraphFixer::has_mem_phi(Node* region) const {
3585   for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
3586     Node* use = region->fast_out(i);
3587     if (use->is_Phi() && use->bottom_type() == Type::MEMORY &&
3588         (_phase->C->get_alias_index(use->adr_type()) == _alias)) {
3589       return true;
3590     }
3591   }
3592   return false;
3593 }
3594 
3595 void MemoryGraphFixer::fix_mem(Node* ctrl, Node* new_ctrl, Node* mem, Node* mem_for_ctrl, Node* new_mem, Unique_Node_List& uses) {
3596   assert(_phase->ctrl_or_self(new_mem) == new_ctrl, "");
3597   const bool trace = false;
3598   DEBUG_ONLY(if (trace) { tty->print("ZZZ control is"); ctrl->dump(); });
3599   DEBUG_ONLY(if (trace) { tty->print("ZZZ mem is"); mem->dump(); });
3600   GrowableArray<Node*> phis;
3601   if (mem_for_ctrl != mem) {
3602     Node* old = mem_for_ctrl;
3603     Node* prev = NULL;
3604     while (old != mem) {
3605       prev = old;
3606       if (old->is_Store() || old->is_ClearArray() || old->is_LoadStore()) {
3607         assert(_alias == Compile::AliasIdxRaw, "");
3608         old = old->in(MemNode::Memory);
3609       } else if (old->Opcode() == Op_SCMemProj) {
3610         assert(_alias == Compile::AliasIdxRaw, "");
3611         old = old->in(0);
3612       } else if (old->Opcode() == Op_ShenandoahWBMemProj) {
3613         assert(_alias != Compile::AliasIdxRaw, "");
3614         old = old->in(ShenandoahWBMemProjNode::WriteBarrier);
3615       } else if (old->Opcode() == Op_ShenandoahWriteBarrier) {
3616         assert(_alias != Compile::AliasIdxRaw, "");
3617         old = old->in(ShenandoahBarrierNode::Memory);
3618       } else {
3619         ShouldNotReachHere();
3620       }
3621     }
3622     assert(prev != NULL, "");
3623     if (new_ctrl != ctrl) {
3624       _memory_nodes.map(ctrl->_idx, mem);
3625       _memory_nodes.map(new_ctrl->_idx, mem_for_ctrl);
3626     }
3627     uint input = prev->Opcode() == Op_ShenandoahWriteBarrier ? (uint)ShenandoahBarrierNode::Memory : (uint)MemNode::Memory;
3628     _phase->igvn().replace_input_of(prev, input, new_mem);
3629   } else {
3630     uses.clear();
3631     _memory_nodes.map(new_ctrl->_idx, new_mem);
3632     uses.push(new_ctrl);
3633     for(uint next = 0; next < uses.size(); next++ ) {
3634       Node *n = uses.at(next);
3635       assert(n->is_CFG(), "");
3636       DEBUG_ONLY(if (trace) { tty->print("ZZZ ctrl"); n->dump(); });
3637       for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3638         Node* u = n->fast_out(i);
3639         if (!u->is_Root() && u->is_CFG() && u != n) {
3640           Node* m = _memory_nodes[u->_idx];
3641           if (u->is_Region() && (!u->is_OuterStripMinedLoop() || _include_lsm) &&
3642               !has_mem_phi(u) &&
3643               u->unique_ctrl_out()->Opcode() != Op_Halt) {
3644             DEBUG_ONLY(if (trace) { tty->print("ZZZ region"); u->dump(); });
3645             DEBUG_ONLY(if (trace && m != NULL) { tty->print("ZZZ mem"); m->dump(); });
3646 
3647             if (!mem_is_valid(m, u) || !m->is_Phi()) {
3648               bool push = true;
3649               bool create_phi = true;
3650               if (_phase->is_dominator(new_ctrl, u)) {
3651                 create_phi = false;
3652               } else if (!_phase->C->has_irreducible_loop()) {
3653                 IdealLoopTree* loop = _phase->get_loop(ctrl);
3654                 bool do_check = true;
3655                 IdealLoopTree* l = loop;
3656                 create_phi = false;
3657                 while (l != _phase->ltree_root()) {
3658                   if (_phase->is_dominator(l->_head, u) && _phase->is_dominator(_phase->idom(u), l->_head)) {
3659                     create_phi = true;
3660                     do_check = false;
3661                     break;
3662                   }
3663                   l = l->_parent;
3664                 }
3665 
3666                 if (do_check) {
3667                   assert(!create_phi, "");
3668                   IdealLoopTree* u_loop = _phase->get_loop(u);
3669                   if (u_loop != _phase->ltree_root() && u_loop->is_member(loop)) {
3670                     Node* c = ctrl;
3671                     while (!_phase->is_dominator(c, u_loop->tail())) {
3672                       c = _phase->idom(c);
3673                     }
3674                     if (!_phase->is_dominator(c, u)) {
3675                       do_check = false;
3676                     }
3677                   }
3678                 }
3679 
3680                 if (do_check && _phase->is_dominator(_phase->idom(u), new_ctrl)) {
3681                   create_phi = true;
3682                 }
3683               }
3684               if (create_phi) {
3685                 Node* phi = new PhiNode(u, Type::MEMORY, _phase->C->get_adr_type(_alias));
3686                 _phase->register_new_node(phi, u);
3687                 phis.push(phi);
3688                 DEBUG_ONLY(if (trace) { tty->print("ZZZ new phi"); phi->dump(); });
3689                 if (!mem_is_valid(m, u)) {
3690                   DEBUG_ONLY(if (trace) { tty->print("ZZZ setting mem"); phi->dump(); });
3691                   _memory_nodes.map(u->_idx, phi);
3692                 } else {
3693                   DEBUG_ONLY(if (trace) { tty->print("ZZZ NOT setting mem"); m->dump(); });
3694                   for (;;) {
3695                     assert(m->is_Mem() || m->is_LoadStore() || m->is_Proj() || m->Opcode() == Op_ShenandoahWriteBarrier || m->Opcode() == Op_ShenandoahWBMemProj, "");
3696                     Node* next = NULL;
3697                     if (m->is_Proj()) {
3698                       next = m->in(0);
3699                     } else if (m->Opcode() == Op_ShenandoahWBMemProj) {
3700                       next = m->in(ShenandoahWBMemProjNode::WriteBarrier);
3701                     } else if (m->is_Mem() || m->is_LoadStore()) {
3702                       assert(_alias == Compile::AliasIdxRaw, "");
3703                       next = m->in(MemNode::Memory);
3704                     } else {
3705                       assert(_alias != Compile::AliasIdxRaw, "");
3706                       assert (m->Opcode() == Op_ShenandoahWriteBarrier, "");
3707                       next = m->in(ShenandoahBarrierNode::Memory);
3708                     }
3709                     if (_phase->get_ctrl(next) != u) {
3710                       break;
3711                     }
3712                     if (next->is_MergeMem()) {
3713                       assert(_phase->get_ctrl(next->as_MergeMem()->memory_at(_alias)) != u, "");
3714                       break;
3715                     }
3716                     if (next->is_Phi()) {
3717                       assert(next->adr_type() == TypePtr::BOTTOM && next->in(0) == u, "");
3718                       break;
3719                     }
3720                     m = next;
3721                   }
3722 
3723                   DEBUG_ONLY(if (trace) { tty->print("ZZZ setting to phi"); m->dump(); });
3724                   assert(m->is_Mem() || m->is_LoadStore() || m->Opcode() == Op_ShenandoahWriteBarrier, "");
3725                   uint input = (m->is_Mem() || m->is_LoadStore()) ? (uint)MemNode::Memory : (uint)ShenandoahBarrierNode::Memory;
3726                   _phase->igvn().replace_input_of(m, input, phi);
3727                   push = false;
3728                 }
3729               } else {
3730                 DEBUG_ONLY(if (trace) { tty->print("ZZZ skipping region"); u->dump(); });
3731               }
3732               if (push) {
3733                 uses.push(u);
3734               }
3735             }
3736           } else if (!mem_is_valid(m, u) &&
3737                      !(u->Opcode() == Op_CProj && u->in(0)->Opcode() == Op_NeverBranch && u->as_Proj()->_con == 1)) {
3738             uses.push(u);
3739           }
3740         }
3741       }
3742     }
3743     for (int i = 0; i < phis.length(); i++) {
3744       Node* n = phis.at(i);
3745       Node* r = n->in(0);
3746       DEBUG_ONLY(if (trace) { tty->print("ZZZ fixing new phi"); n->dump(); });
3747       for (uint j = 1; j < n->req(); j++) {
3748         Node* m = find_mem(r->in(j), NULL);
3749         _phase->igvn().replace_input_of(n, j, m);
3750         DEBUG_ONLY(if (trace) { tty->print("ZZZ fixing new phi: %d", j); m->dump(); });
3751       }
3752     }
3753   }
3754   uint last = _phase->C->unique();
3755   MergeMemNode* mm = NULL;
3756   int alias = _alias;
3757   DEBUG_ONLY(if (trace) { tty->print("ZZZ raw mem is"); mem->dump(); });
3758   for (DUIterator i = mem->outs(); mem->has_out(i); i++) {
3759     Node* u = mem->out(i);
3760     if (u->_idx < last) {
3761       if (u->is_Mem()) {
3762         if (_phase->C->get_alias_index(u->adr_type()) == alias) {
3763           Node* m = find_mem(_phase->get_ctrl(u), u);
3764           if (m != mem) {
3765             DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); });
3766             _phase->igvn().replace_input_of(u, MemNode::Memory, m);
3767             --i;
3768           }
3769         }
3770       } else if (u->is_MergeMem()) {
3771         MergeMemNode* u_mm = u->as_MergeMem();
3772         if (u_mm->memory_at(alias) == mem) {
3773           MergeMemNode* newmm = NULL;
3774           for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) {
3775             Node* uu = u->fast_out(j);
3776             assert(!uu->is_MergeMem(), "chain of MergeMems?");
3777             if (uu->is_Phi()) {
3778               assert(uu->adr_type() == TypePtr::BOTTOM, "");
3779               Node* region = uu->in(0);
3780               int nb = 0;
3781               for (uint k = 1; k < uu->req(); k++) {
3782                 if (uu->in(k) == u) {
3783                   Node* m = find_mem(region->in(k), NULL);
3784                   if (m != mem) {
3785                     DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of phi %d", k); uu->dump(); });
3786                     newmm = clone_merge_mem(u, mem, m, _phase->ctrl_or_self(m), i);
3787                     if (newmm != u) {
3788                       _phase->igvn().replace_input_of(uu, k, newmm);
3789                       nb++;
3790                       --jmax;
3791                     }
3792                   }
3793                 }
3794               }
3795               if (nb > 0) {
3796                 --j;
3797               }
3798             } else {
3799               Node* m = find_mem(_phase->ctrl_or_self(uu), uu);
3800               if (m != mem) {
3801                 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); uu->dump(); });
3802                 newmm = clone_merge_mem(u, mem, m, _phase->ctrl_or_self(m), i);
3803                 if (newmm != u) {
3804                   _phase->igvn().replace_input_of(uu, uu->find_edge(u), newmm);
3805                   --j, --jmax;
3806                 }
3807               }
3808             }
3809           }
3810         }
3811       } else if (u->is_Phi()) {
3812         assert(u->bottom_type() == Type::MEMORY, "what else?");
3813         if (_phase->C->get_alias_index(u->adr_type()) == alias || u->adr_type() == TypePtr::BOTTOM) {
3814           Node* region = u->in(0);
3815           bool replaced = false;
3816           for (uint j = 1; j < u->req(); j++) {
3817             if (u->in(j) == mem) {
3818               Node* m = find_mem(region->in(j), NULL);
3819               Node* nnew = m;
3820               if (m != mem) {
3821                 if (u->adr_type() == TypePtr::BOTTOM) {
3822                   mm = allocate_merge_mem(mem, m, _phase->ctrl_or_self(m));
3823                   nnew = mm;
3824                 }
3825                 DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of phi %d", j); u->dump(); });
3826                 _phase->igvn().replace_input_of(u, j, nnew);
3827                 replaced = true;
3828               }
3829             }
3830           }
3831           if (replaced) {
3832             --i;
3833           }
3834         }
3835       } else if ((u->adr_type() == TypePtr::BOTTOM && u->Opcode() != Op_StrInflatedCopy) ||
3836                  u->adr_type() == NULL) {
3837         assert(u->adr_type() != NULL ||
3838                u->Opcode() == Op_Rethrow ||
3839                u->Opcode() == Op_Return ||
3840                u->Opcode() == Op_SafePoint ||
3841                (u->is_CallStaticJava() && u->as_CallStaticJava()->uncommon_trap_request() != 0) ||
3842                (u->is_CallStaticJava() && u->as_CallStaticJava()->_entry_point == OptoRuntime::rethrow_stub()) ||
3843                u->Opcode() == Op_CallLeaf, "");
3844         Node* m = find_mem(_phase->ctrl_or_self(u), u);
3845         if (m != mem) {
3846           mm = allocate_merge_mem(mem, m, _phase->get_ctrl(m));
3847           _phase->igvn().replace_input_of(u, u->find_edge(mem), mm);
3848           --i;
3849         }
3850       } else if (_phase->C->get_alias_index(u->adr_type()) == alias) {
3851         Node* m = find_mem(_phase->ctrl_or_self(u), u);
3852         if (m != mem) {
3853           DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); });
3854           _phase->igvn().replace_input_of(u, u->find_edge(mem), m);
3855           --i;
3856         }
3857       } else if (u->adr_type() != TypePtr::BOTTOM &&
3858                  _memory_nodes[_phase->ctrl_or_self(u)->_idx] == u) {
3859         Node* m = find_mem(_phase->ctrl_or_self(u), u);
3860         assert(m != mem, "");
3861         // u is on the wrong slice...
3862         assert(u->is_ClearArray(), "");
3863         DEBUG_ONLY(if (trace) { tty->print("ZZZ setting memory of use"); u->dump(); });
3864         _phase->igvn().replace_input_of(u, u->find_edge(mem), m);
3865         --i;
3866       }
3867     }
3868   }
3869 #ifdef ASSERT
3870   assert(new_mem->outcnt() > 0, "");
3871   for (int i = 0; i < phis.length(); i++) {
3872     Node* n = phis.at(i);
3873     assert(n->outcnt() > 0, "new phi must have uses now");
3874   }
3875 #endif
3876 }
3877 
3878 MergeMemNode* MemoryGraphFixer::allocate_merge_mem(Node* mem, Node* rep_proj, Node* rep_ctrl) const {
3879   MergeMemNode* mm = MergeMemNode::make(mem);
3880   mm->set_memory_at(_alias, rep_proj);
3881   _phase->register_new_node(mm, rep_ctrl);
3882   return mm;
3883 }
3884 
3885 MergeMemNode* MemoryGraphFixer::clone_merge_mem(Node* u, Node* mem, Node* rep_proj, Node* rep_ctrl, DUIterator& i) const {
3886   MergeMemNode* newmm = NULL;
3887   MergeMemNode* u_mm = u->as_MergeMem();
3888   Node* c = _phase->get_ctrl(u);
3889   if (_phase->is_dominator(c, rep_ctrl)) {
3890     c = rep_ctrl;
3891   } else {
3892     assert(_phase->is_dominator(rep_ctrl, c), "one must dominate the other");
3893   }
3894   if (u->outcnt() == 1) {
3895     if (u->req() > (uint)_alias && u->in(_alias) == mem) {
3896       _phase->igvn().replace_input_of(u, _alias, rep_proj);
3897       --i;
3898     } else {
3899       _phase->igvn().rehash_node_delayed(u);
3900       u_mm->set_memory_at(_alias, rep_proj);
3901     }
3902     newmm = u_mm;
3903     _phase->set_ctrl_and_loop(u, c);
3904   } else {
3905     // can't simply clone u and then change one of its input because
3906     // it adds and then removes an edge which messes with the
3907     // DUIterator
3908     newmm = MergeMemNode::make(u_mm->base_memory());
3909     for (uint j = 0; j < u->req(); j++) {
3910       if (j < newmm->req()) {
3911         if (j == (uint)_alias) {
3912           newmm->set_req(j, rep_proj);
3913         } else if (newmm->in(j) != u->in(j)) {
3914           newmm->set_req(j, u->in(j));
3915         }
3916       } else if (j == (uint)_alias) {
3917         newmm->add_req(rep_proj);
3918       } else {
3919         newmm->add_req(u->in(j));
3920       }
3921     }
3922     if ((uint)_alias >= u->req()) {
3923       newmm->set_memory_at(_alias, rep_proj);
3924     }
3925     _phase->register_new_node(newmm, c);
3926   }
3927   return newmm;
3928 }
3929 
3930 bool MemoryGraphFixer::should_process_phi(Node* phi) const {
3931   if (phi->adr_type() == TypePtr::BOTTOM) {
3932     Node* region = phi->in(0);
3933     for (DUIterator_Fast jmax, j = region->fast_outs(jmax); j < jmax; j++) {
3934       Node* uu = region->fast_out(j);
3935       if (uu->is_Phi() && uu != phi && uu->bottom_type() == Type::MEMORY && _phase->C->get_alias_index(uu->adr_type()) == _alias) {
3936         return false;
3937       }
3938     }
3939     return true;
3940   }
3941   return _phase->C->get_alias_index(phi->adr_type()) == _alias;
3942 }
3943 
3944 void MemoryGraphFixer::fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl) const {
3945   uint last = _phase-> C->unique();
3946   MergeMemNode* mm = NULL;
3947   assert(mem->bottom_type() == Type::MEMORY, "");
3948   for (DUIterator i = mem->outs(); mem->has_out(i); i++) {
3949     Node* u = mem->out(i);
3950     if (u != replacement && u->_idx < last) {
3951       if (u->is_ShenandoahBarrier() && _alias != Compile::AliasIdxRaw) {
3952         if (_phase->C->get_alias_index(u->adr_type()) == _alias && ShenandoahWriteBarrierNode::is_dominator(rep_ctrl, _phase->ctrl_or_self(u), replacement, u, _phase)) {
3953           _phase->igvn().replace_input_of(u, u->find_edge(mem), rep_proj);
3954           assert(u->find_edge(mem) == -1, "only one edge");
3955           --i;
3956         }
3957       } else if (u->is_Mem()) {
3958         if (_phase->C->get_alias_index(u->adr_type()) == _alias && ShenandoahWriteBarrierNode::is_dominator(rep_ctrl, _phase->ctrl_or_self(u), replacement, u, _phase)) {
3959           assert(_alias == Compile::AliasIdxRaw , "only raw memory can lead to a memory operation");
3960           _phase->igvn().replace_input_of(u, u->find_edge(mem), rep_proj);
3961           assert(u->find_edge(mem) == -1, "only one edge");
3962           --i;
3963         }
3964       } else if (u->is_MergeMem()) {
3965         MergeMemNode* u_mm = u->as_MergeMem();
3966         if (u_mm->memory_at(_alias) == mem) {
3967           MergeMemNode* newmm = NULL;
3968           for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) {
3969             Node* uu = u->fast_out(j);
3970             assert(!uu->is_MergeMem(), "chain of MergeMems?");
3971             if (uu->is_Phi()) {
3972               if (should_process_phi(uu)) {
3973                 Node* region = uu->in(0);
3974                 int nb = 0;
3975                 for (uint k = 1; k < uu->req(); k++) {
3976                   if (uu->in(k) == u && _phase->is_dominator(rep_ctrl, region->in(k))) {
3977                     if (newmm == NULL) {
3978                       newmm = clone_merge_mem(u, mem, rep_proj, rep_ctrl, i);
3979                     }
3980                     if (newmm != u) {
3981                       _phase->igvn().replace_input_of(uu, k, newmm);
3982                       nb++;
3983                       --jmax;
3984                     }
3985                   }
3986                 }
3987                 if (nb > 0) {
3988                   --j;
3989                 }
3990               }
3991             } else {
3992               if (rep_ctrl != uu && ShenandoahWriteBarrierNode::is_dominator(rep_ctrl, _phase->ctrl_or_self(uu), replacement, uu, _phase)) {
3993                 if (newmm == NULL) {
3994                   newmm = clone_merge_mem(u, mem, rep_proj, rep_ctrl, i);
3995                 }
3996                 if (newmm != u) {
3997                   _phase->igvn().replace_input_of(uu, uu->find_edge(u), newmm);
3998                   --j, --jmax;
3999                 }
4000               }
4001             }
4002           }
4003         }
4004       } else if (u->is_Phi()) {
4005         assert(u->bottom_type() == Type::MEMORY, "what else?");
4006         Node* region = u->in(0);
4007         if (should_process_phi(u)) {
4008           bool replaced = false;
4009           for (uint j = 1; j < u->req(); j++) {
4010             if (u->in(j) == mem && _phase->is_dominator(rep_ctrl, region->in(j))) {
4011               Node* nnew = rep_proj;
4012               if (u->adr_type() == TypePtr::BOTTOM) {
4013                 if (mm == NULL) {
4014                   mm = allocate_merge_mem(mem, rep_proj, rep_ctrl);
4015                 }
4016                 nnew = mm;
4017               }
4018               _phase->igvn().replace_input_of(u, j, nnew);
4019               replaced = true;
4020             }
4021           }
4022           if (replaced) {
4023             --i;
4024           }
4025 
4026         }
4027       } else if ((u->adr_type() == TypePtr::BOTTOM && u->Opcode() != Op_StrInflatedCopy) ||
4028                  u->adr_type() == NULL) {
4029         assert(u->adr_type() != NULL ||
4030                u->Opcode() == Op_Rethrow ||
4031                u->Opcode() == Op_Return ||
4032                u->Opcode() == Op_SafePoint ||
4033                (u->is_CallStaticJava() && u->as_CallStaticJava()->uncommon_trap_request() != 0) ||
4034                (u->is_CallStaticJava() && u->as_CallStaticJava()->_entry_point == OptoRuntime::rethrow_stub()) ||
4035                u->Opcode() == Op_CallLeaf, "");
4036         if (ShenandoahWriteBarrierNode::is_dominator(rep_ctrl, _phase->ctrl_or_self(u), replacement, u, _phase)) {
4037           if (mm == NULL) {
4038             mm = allocate_merge_mem(mem, rep_proj, rep_ctrl);
4039           }
4040           _phase->igvn().replace_input_of(u, u->find_edge(mem), mm);
4041           --i;
4042         }
4043       } else if (_phase->C->get_alias_index(u->adr_type()) == _alias) {
4044         if (ShenandoahWriteBarrierNode::is_dominator(rep_ctrl, _phase->ctrl_or_self(u), replacement, u, _phase)) {
4045           _phase->igvn().replace_input_of(u, u->find_edge(mem), rep_proj);
4046           --i;
4047         }
4048       }
4049     }
4050   }
4051 }
4052 
4053 void MemoryGraphFixer::remove(Node* n) {
4054   assert(n->Opcode() == Op_ShenandoahWBMemProj, "");
4055   Node* c = _phase->get_ctrl(n);
4056   Node* mem = find_mem(c, NULL);
4057   if (mem == n) {
4058     _memory_nodes.map(c->_idx, mem->in(ShenandoahWBMemProjNode::WriteBarrier)->in(ShenandoahBarrierNode::Memory));
4059   }
4060 }
4061 
4062 static bool is_on_null_check_path(Block* b, Block* null_check_block) {
4063   if (null_check_block == NULL) {
4064     return false;
4065   }
4066   do {
4067     assert(null_check_block->_num_succs == 1, "only one succ on the path to unc");
4068     if (b == null_check_block) {
4069       return true;
4070     }
4071     null_check_block = null_check_block->_succs[0];
4072   } while(!null_check_block->head()->is_Root());
4073 
4074   return false;
4075 }
4076 
4077 int PhaseCFG::replace_uses_with_shenandoah_barrier_helper(Node* n, Node* use, Node* val, Block* block, Block* null_check_block) {
4078   int nb = 0;
4079   Block* buse = get_block_for_node(use);
4080   if (is_on_null_check_path(buse, null_check_block)) {
4081     return 0;
4082   }
4083   if (use->is_Phi()) {
4084     for (uint j = 1; j < use->req(); j++) {
4085       if (use->in(j) == val) {
4086         Block* b = get_block_for_node(use->in(0)->in(j));
4087         if ((block != b && block->dom_lca(b) == block) ||
4088             block == b) {
4089           use->set_req(j, n);
4090           nb++;
4091         }
4092       }
4093     }
4094   } else {
4095     if ((block != buse && block->dom_lca(buse) == block) ||
4096         (block == buse && !use->is_scheduled())) {
4097       // Let precedence edges alone (can confuse anti-dependence verification code)
4098       for (uint i = 0; i < use->req(); i++) {
4099         if (use->in(i) == val) {
4100           use->set_req(i, n);
4101           nb++;
4102         }
4103       }
4104       assert(nb > 0 || use->find_prec_edge(val) != -1, "no replacement?");
4105     }
4106   }
4107 
4108   return nb;
4109 }
4110 
4111 void PhaseCFG::replace_uses_with_shenandoah_barrier(Node* n, Block* block, Node_List& worklist, GrowableArray<int>& ready_cnt, uint max_idx, uint& phi_cnt) {
4112   if (!ShenandoahDecreaseRegisterPressure) {
4113     return;
4114   }
4115   // Replace all uses of barrier's input that are dominated by the
4116   // barrier with the value returned by the barrier: no need to keep
4117   // both live.
4118   if (n->is_Mach() && n->as_Mach()->ideal_Opcode() == Op_ShenandoahReadBarrier) {
4119     MachNullCheckNode* null_check = NULL;
4120     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax && null_check == NULL; i++) {
4121       Node* use = n->fast_out(i);
4122       if (use->is_MachNullCheck()) {
4123         null_check = use->as_MachNullCheck();
4124       }
4125     }
4126     Block* null_check_block = NULL;
4127     if (null_check != NULL) {
4128       Node* proj = null_check->find_out_with(Op_IfTrue);
4129       Node* head = proj->unique_out();
4130       null_check_block = get_block_for_node(head);
4131     }
4132 
4133     Node* val = n->in(ShenandoahBarrierNode::ValueIn);
4134     if (!val->bottom_type()->isa_narrowoop()) {
4135       for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
4136         Node* use = val->fast_out(i);
4137         if (use != n) {
4138           int nb = replace_uses_with_shenandoah_barrier_helper(n, use, val, block, null_check_block);
4139           if (nb > 0) {
4140             --i; imax -= nb;
4141           }
4142         }
4143       }
4144     } else {
4145       for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
4146         Node* u = val->fast_out(i);
4147         if (u->is_Mach() && u->as_Mach()->ideal_Opcode() == Op_DecodeN) {
4148           int projs = 0;
4149           for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) {
4150             Node* uu = u->fast_out(j);
4151             assert(!uu->is_MachTemp(), "");
4152             if (uu->is_MachProj() && uu->outcnt() == 0) {
4153               projs++;
4154             } else {
4155               int nb = replace_uses_with_shenandoah_barrier_helper(n, uu, u, block, null_check_block);
4156               if (nb > 0) {
4157                 if (!u->is_scheduled()) {
4158                   push_ready_nodes(n, uu, block, ready_cnt, worklist, max_idx, nb);
4159                 }
4160                 --j; jmax -= nb;
4161               }
4162             }
4163           }
4164           // The DecodeN may have gone dead
4165           if (u->outcnt() - projs == 0) {
4166             u->disconnect_inputs(NULL, C);
4167             Block* bu = get_block_for_node(u);
4168             unmap_node_from_block(u);
4169             if (bu == block) {
4170               if (u->is_scheduled()) {
4171                 block->find_remove(u);
4172                 phi_cnt--;
4173               } else {
4174                 worklist.yank(u);
4175                 block->remove_node(block->end_idx()-1);
4176               }
4177             } else {
4178               bu->find_remove(u);
4179             }
4180             for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) {
4181               Node* uu = u->fast_out(j);
4182               assert(uu->is_MachProj() && uu->outcnt() == 0, "");
4183               assert(bu == get_block_for_node(uu), "");
4184               uu->disconnect_inputs(NULL, C);
4185               --j; --jmax;
4186               unmap_node_from_block(uu);
4187               if (bu == block) {
4188                 if (u->is_scheduled()) {
4189                   block->find_remove(uu);
4190                   phi_cnt--;
4191                 } else {
4192                   worklist.yank(uu);
4193                   block->remove_node(block->end_idx()-1);
4194                 }
4195               } else {
4196                 bu->find_remove(uu);
4197               }
4198               assert(uu->is_scheduled() == u->is_scheduled(), "");
4199             }
4200             --i; --imax;
4201           }
4202         }
4203       }
4204     }
4205   }
4206 }