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