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