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