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