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