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