1 /*
   2  * Copyright (c) 2018, 2019, Red Hat, Inc. All rights reserved.
   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/shared/barrierSet.hpp"
  26 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  27 #include "gc/shenandoah/shenandoahForwarding.hpp"
  28 #include "gc/shenandoah/shenandoahHeap.hpp"
  29 #include "gc/shenandoah/shenandoahRuntime.hpp"
  30 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  31 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
  32 #include "gc/shenandoah/c2/shenandoahSupport.hpp"
  33 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  34 #include "opto/arraycopynode.hpp"
  35 #include "opto/escape.hpp"
  36 #include "opto/graphKit.hpp"
  37 #include "opto/idealKit.hpp"
  38 #include "opto/macro.hpp"
  39 #include "opto/movenode.hpp"
  40 #include "opto/narrowptrnode.hpp"
  41 #include "opto/rootnode.hpp"
  42 #include "opto/runtime.hpp"
  43 
  44 ShenandoahBarrierSetC2* ShenandoahBarrierSetC2::bsc2() {
  45   return reinterpret_cast<ShenandoahBarrierSetC2*>(BarrierSet::barrier_set()->barrier_set_c2());
  46 }
  47 
  48 ShenandoahBarrierSetC2State::ShenandoahBarrierSetC2State(Arena* comp_arena)
  49   : _enqueue_barriers(new (comp_arena) GrowableArray<ShenandoahEnqueueBarrierNode*>(comp_arena, 8,  0, NULL)),
  50     _load_reference_barriers(new (comp_arena) GrowableArray<ShenandoahLoadReferenceBarrierNode*>(comp_arena, 8,  0, NULL)) {
  51 }
  52 
  53 int ShenandoahBarrierSetC2State::enqueue_barriers_count() const {
  54   return _enqueue_barriers->length();
  55 }
  56 
  57 ShenandoahEnqueueBarrierNode* ShenandoahBarrierSetC2State::enqueue_barrier(int idx) const {
  58   return _enqueue_barriers->at(idx);
  59 }
  60 
  61 void ShenandoahBarrierSetC2State::add_enqueue_barrier(ShenandoahEnqueueBarrierNode * n) {
  62   assert(!_enqueue_barriers->contains(n), "duplicate entry in barrier list");
  63   _enqueue_barriers->append(n);
  64 }
  65 
  66 void ShenandoahBarrierSetC2State::remove_enqueue_barrier(ShenandoahEnqueueBarrierNode * n) {
  67   if (_enqueue_barriers->contains(n)) {
  68     _enqueue_barriers->remove(n);
  69   }
  70 }
  71 
  72 int ShenandoahBarrierSetC2State::load_reference_barriers_count() const {
  73   return _load_reference_barriers->length();
  74 }
  75 
  76 ShenandoahLoadReferenceBarrierNode* ShenandoahBarrierSetC2State::load_reference_barrier(int idx) const {
  77   return _load_reference_barriers->at(idx);
  78 }
  79 
  80 void ShenandoahBarrierSetC2State::add_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n) {
  81   assert(!_load_reference_barriers->contains(n), "duplicate entry in barrier list");
  82   _load_reference_barriers->append(n);
  83 }
  84 
  85 void ShenandoahBarrierSetC2State::remove_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n) {
  86   if (_load_reference_barriers->contains(n)) {
  87     _load_reference_barriers->remove(n);
  88   }
  89 }
  90 
  91 Node* ShenandoahBarrierSetC2::shenandoah_storeval_barrier(GraphKit* kit, Node* obj) const {
  92   if (ShenandoahStoreValEnqueueBarrier) {
  93     obj = shenandoah_enqueue_barrier(kit, obj);
  94   }
  95   return obj;
  96 }
  97 
  98 #define __ kit->
  99 
 100 bool ShenandoahBarrierSetC2::satb_can_remove_pre_barrier(GraphKit* kit, PhaseTransform* phase, Node* adr,
 101                                                          BasicType bt, uint adr_idx) const {
 102   intptr_t offset = 0;
 103   Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
 104   AllocateNode* alloc = AllocateNode::Ideal_allocation(base, phase);
 105 
 106   if (offset == Type::OffsetBot) {
 107     return false; // cannot unalias unless there are precise offsets
 108   }
 109 
 110   if (alloc == NULL) {
 111     return false; // No allocation found
 112   }
 113 
 114   intptr_t size_in_bytes = type2aelembytes(bt);
 115 
 116   Node* mem = __ memory(adr_idx); // start searching here...
 117 
 118   for (int cnt = 0; cnt < 50; cnt++) {
 119 
 120     if (mem->is_Store()) {
 121 
 122       Node* st_adr = mem->in(MemNode::Address);
 123       intptr_t st_offset = 0;
 124       Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset);
 125 
 126       if (st_base == NULL) {
 127         break; // inscrutable pointer
 128       }
 129 
 130       // Break we have found a store with same base and offset as ours so break
 131       if (st_base == base && st_offset == offset) {
 132         break;
 133       }
 134 
 135       if (st_offset != offset && st_offset != Type::OffsetBot) {
 136         const int MAX_STORE = BytesPerLong;
 137         if (st_offset >= offset + size_in_bytes ||
 138             st_offset <= offset - MAX_STORE ||
 139             st_offset <= offset - mem->as_Store()->memory_size()) {
 140           // Success:  The offsets are provably independent.
 141           // (You may ask, why not just test st_offset != offset and be done?
 142           // The answer is that stores of different sizes can co-exist
 143           // in the same sequence of RawMem effects.  We sometimes initialize
 144           // a whole 'tile' of array elements with a single jint or jlong.)
 145           mem = mem->in(MemNode::Memory);
 146           continue; // advance through independent store memory
 147         }
 148       }
 149 
 150       if (st_base != base
 151           && MemNode::detect_ptr_independence(base, alloc, st_base,
 152                                               AllocateNode::Ideal_allocation(st_base, phase),
 153                                               phase)) {
 154         // Success:  The bases are provably independent.
 155         mem = mem->in(MemNode::Memory);
 156         continue; // advance through independent store memory
 157       }
 158     } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
 159 
 160       InitializeNode* st_init = mem->in(0)->as_Initialize();
 161       AllocateNode* st_alloc = st_init->allocation();
 162 
 163       // Make sure that we are looking at the same allocation site.
 164       // The alloc variable is guaranteed to not be null here from earlier check.
 165       if (alloc == st_alloc) {
 166         // Check that the initialization is storing NULL so that no previous store
 167         // has been moved up and directly write a reference
 168         Node* captured_store = st_init->find_captured_store(offset,
 169                                                             type2aelembytes(T_OBJECT),
 170                                                             phase);
 171         if (captured_store == NULL || captured_store == st_init->zero_memory()) {
 172           return true;
 173         }
 174       }
 175     }
 176 
 177     // Unless there is an explicit 'continue', we must bail out here,
 178     // because 'mem' is an inscrutable memory state (e.g., a call).
 179     break;
 180   }
 181 
 182   return false;
 183 }
 184 
 185 #undef __
 186 #define __ ideal.
 187 
 188 void ShenandoahBarrierSetC2::satb_write_barrier_pre(GraphKit* kit,
 189                                                     bool do_load,
 190                                                     Node* obj,
 191                                                     Node* adr,
 192                                                     uint alias_idx,
 193                                                     Node* val,
 194                                                     const TypeOopPtr* val_type,
 195                                                     Node* pre_val,
 196                                                     BasicType bt) const {
 197   // Some sanity checks
 198   // Note: val is unused in this routine.
 199 
 200   if (do_load) {
 201     // We need to generate the load of the previous value
 202     assert(obj != NULL, "must have a base");
 203     assert(adr != NULL, "where are loading from?");
 204     assert(pre_val == NULL, "loaded already?");
 205     assert(val_type != NULL, "need a type");
 206 
 207     if (ReduceInitialCardMarks
 208         && satb_can_remove_pre_barrier(kit, &kit->gvn(), adr, bt, alias_idx)) {
 209       return;
 210     }
 211 
 212   } else {
 213     // In this case both val_type and alias_idx are unused.
 214     assert(pre_val != NULL, "must be loaded already");
 215     // Nothing to be done if pre_val is null.
 216     if (pre_val->bottom_type() == TypePtr::NULL_PTR) return;
 217     assert(pre_val->bottom_type()->basic_type() == T_OBJECT, "or we shouldn't be here");
 218   }
 219   assert(bt == T_OBJECT, "or we shouldn't be here");
 220 
 221   IdealKit ideal(kit, true);
 222 
 223   Node* tls = __ thread(); // ThreadLocalStorage
 224 
 225   Node* no_base = __ top();
 226   Node* zero  = __ ConI(0);
 227   Node* zeroX = __ ConX(0);
 228 
 229   float likely  = PROB_LIKELY(0.999);
 230   float unlikely  = PROB_UNLIKELY(0.999);
 231 
 232   // Offsets into the thread
 233   const int index_offset   = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset());
 234   const int buffer_offset  = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset());
 235 
 236   // Now the actual pointers into the thread
 237   Node* buffer_adr  = __ AddP(no_base, tls, __ ConX(buffer_offset));
 238   Node* index_adr   = __ AddP(no_base, tls, __ ConX(index_offset));
 239 
 240   // Now some of the values
 241   Node* marking;
 242   Node* gc_state = __ AddP(no_base, tls, __ ConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset())));
 243   Node* ld = __ load(__ ctrl(), gc_state, TypeInt::BYTE, T_BYTE, Compile::AliasIdxRaw);
 244   marking = __ AndI(ld, __ ConI(ShenandoahHeap::MARKING));
 245   assert(ShenandoahBarrierC2Support::is_gc_state_load(ld), "Should match the shape");
 246 
 247   // if (!marking)
 248   __ if_then(marking, BoolTest::ne, zero, unlikely); {
 249     BasicType index_bt = TypeX_X->basic_type();
 250     assert(sizeof(size_t) == type2aelembytes(index_bt), "Loading Shenandoah SATBMarkQueue::_index with wrong size.");
 251     Node* index   = __ load(__ ctrl(), index_adr, TypeX_X, index_bt, Compile::AliasIdxRaw);
 252 
 253     if (do_load) {
 254       // load original value
 255       // alias_idx correct??
 256       pre_val = __ load(__ ctrl(), adr, val_type, bt, alias_idx);
 257     }
 258 
 259     // if (pre_val != NULL)
 260     __ if_then(pre_val, BoolTest::ne, kit->null()); {
 261       Node* buffer  = __ load(__ ctrl(), buffer_adr, TypeRawPtr::NOTNULL, T_ADDRESS, Compile::AliasIdxRaw);
 262 
 263       // is the queue for this thread full?
 264       __ if_then(index, BoolTest::ne, zeroX, likely); {
 265 
 266         // decrement the index
 267         Node* next_index = kit->gvn().transform(new SubXNode(index, __ ConX(sizeof(intptr_t))));
 268 
 269         // Now get the buffer location we will log the previous value into and store it
 270         Node *log_addr = __ AddP(no_base, buffer, next_index);
 271         __ store(__ ctrl(), log_addr, pre_val, T_OBJECT, Compile::AliasIdxRaw, MemNode::unordered);
 272         // update the index
 273         __ store(__ ctrl(), index_adr, next_index, index_bt, Compile::AliasIdxRaw, MemNode::unordered);
 274 
 275       } __ else_(); {
 276 
 277         // logging buffer is full, call the runtime
 278         const TypeFunc *tf = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type();
 279         __ make_leaf_call(tf, CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), "shenandoah_wb_pre", pre_val, tls);
 280       } __ end_if();  // (!index)
 281     } __ end_if();  // (pre_val != NULL)
 282   } __ end_if();  // (!marking)
 283 
 284   // Final sync IdealKit and GraphKit.
 285   kit->final_sync(ideal);
 286 
 287   if (ShenandoahSATBBarrier && adr != NULL) {
 288     Node* c = kit->control();
 289     Node* call = c->in(1)->in(1)->in(1)->in(0);
 290     assert(is_shenandoah_wb_pre_call(call), "shenandoah_wb_pre call expected");
 291     call->add_req(adr);
 292   }
 293 }
 294 
 295 bool ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(Node* call) {
 296   return call->is_CallLeaf() &&
 297          call->as_CallLeaf()->entry_point() == CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry);
 298 }
 299 
 300 bool ShenandoahBarrierSetC2::is_shenandoah_lrb_call(Node* call) {
 301   if (!call->is_CallLeaf()) {
 302     return false;
 303   }
 304 
 305   address entry_point = call->as_CallLeaf()->entry_point();
 306   return (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier)) ||
 307          (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_narrow));
 308 }
 309 
 310 bool ShenandoahBarrierSetC2::is_shenandoah_marking_if(PhaseTransform *phase, Node* n) {
 311   if (n->Opcode() != Op_If) {
 312     return false;
 313   }
 314 
 315   Node* bol = n->in(1);
 316   assert(bol->is_Bool(), "");
 317   Node* cmpx = bol->in(1);
 318   if (bol->as_Bool()->_test._test == BoolTest::ne &&
 319       cmpx->is_Cmp() && cmpx->in(2) == phase->intcon(0) &&
 320       is_shenandoah_state_load(cmpx->in(1)->in(1)) &&
 321       cmpx->in(1)->in(2)->is_Con() &&
 322       cmpx->in(1)->in(2) == phase->intcon(ShenandoahHeap::MARKING)) {
 323     return true;
 324   }
 325 
 326   return false;
 327 }
 328 
 329 bool ShenandoahBarrierSetC2::is_shenandoah_state_load(Node* n) {
 330   if (!n->is_Load()) return false;
 331   const int state_offset = in_bytes(ShenandoahThreadLocalData::gc_state_offset());
 332   return n->in(2)->is_AddP() && n->in(2)->in(2)->Opcode() == Op_ThreadLocal
 333          && n->in(2)->in(3)->is_Con()
 334          && n->in(2)->in(3)->bottom_type()->is_intptr_t()->get_con() == state_offset;
 335 }
 336 
 337 void ShenandoahBarrierSetC2::shenandoah_write_barrier_pre(GraphKit* kit,
 338                                                           bool do_load,
 339                                                           Node* obj,
 340                                                           Node* adr,
 341                                                           uint alias_idx,
 342                                                           Node* val,
 343                                                           const TypeOopPtr* val_type,
 344                                                           Node* pre_val,
 345                                                           BasicType bt) const {
 346   if (ShenandoahSATBBarrier) {
 347     IdealKit ideal(kit);
 348     kit->sync_kit(ideal);
 349 
 350     satb_write_barrier_pre(kit, do_load, obj, adr, alias_idx, val, val_type, pre_val, bt);
 351 
 352     ideal.sync_kit(kit);
 353     kit->final_sync(ideal);
 354   }
 355 }
 356 
 357 Node* ShenandoahBarrierSetC2::shenandoah_enqueue_barrier(GraphKit* kit, Node* pre_val) const {
 358   return kit->gvn().transform(new ShenandoahEnqueueBarrierNode(pre_val));
 359 }
 360 
 361 // Helper that guards and inserts a pre-barrier.
 362 void ShenandoahBarrierSetC2::insert_pre_barrier(GraphKit* kit, Node* base_oop, Node* offset,
 363                                                 Node* pre_val, bool need_mem_bar) const {
 364   // We could be accessing the referent field of a reference object. If so, when G1
 365   // is enabled, we need to log the value in the referent field in an SATB buffer.
 366   // This routine performs some compile time filters and generates suitable
 367   // runtime filters that guard the pre-barrier code.
 368   // Also add memory barrier for non volatile load from the referent field
 369   // to prevent commoning of loads across safepoint.
 370 
 371   // Some compile time checks.
 372 
 373   // If offset is a constant, is it java_lang_ref_Reference::_reference_offset?
 374   const TypeX* otype = offset->find_intptr_t_type();
 375   if (otype != NULL && otype->is_con() &&
 376       otype->get_con() != java_lang_ref_Reference::referent_offset) {
 377     // Constant offset but not the reference_offset so just return
 378     return;
 379   }
 380 
 381   // We only need to generate the runtime guards for instances.
 382   const TypeOopPtr* btype = base_oop->bottom_type()->isa_oopptr();
 383   if (btype != NULL) {
 384     if (btype->isa_aryptr()) {
 385       // Array type so nothing to do
 386       return;
 387     }
 388 
 389     const TypeInstPtr* itype = btype->isa_instptr();
 390     if (itype != NULL) {
 391       // Can the klass of base_oop be statically determined to be
 392       // _not_ a sub-class of Reference and _not_ Object?
 393       ciKlass* klass = itype->klass();
 394       if ( klass->is_loaded() &&
 395           !klass->is_subtype_of(kit->env()->Reference_klass()) &&
 396           !kit->env()->Object_klass()->is_subtype_of(klass)) {
 397         return;
 398       }
 399     }
 400   }
 401 
 402   // The compile time filters did not reject base_oop/offset so
 403   // we need to generate the following runtime filters
 404   //
 405   // if (offset == java_lang_ref_Reference::_reference_offset) {
 406   //   if (instance_of(base, java.lang.ref.Reference)) {
 407   //     pre_barrier(_, pre_val, ...);
 408   //   }
 409   // }
 410 
 411   float likely   = PROB_LIKELY(  0.999);
 412   float unlikely = PROB_UNLIKELY(0.999);
 413 
 414   IdealKit ideal(kit);
 415 
 416   Node* referent_off = __ ConX(java_lang_ref_Reference::referent_offset);
 417 
 418   __ if_then(offset, BoolTest::eq, referent_off, unlikely); {
 419       // Update graphKit memory and control from IdealKit.
 420       kit->sync_kit(ideal);
 421 
 422       Node* ref_klass_con = kit->makecon(TypeKlassPtr::make(kit->env()->Reference_klass()));
 423       Node* is_instof = kit->gen_instanceof(base_oop, ref_klass_con);
 424 
 425       // Update IdealKit memory and control from graphKit.
 426       __ sync_kit(kit);
 427 
 428       Node* one = __ ConI(1);
 429       // is_instof == 0 if base_oop == NULL
 430       __ if_then(is_instof, BoolTest::eq, one, unlikely); {
 431 
 432         // Update graphKit from IdeakKit.
 433         kit->sync_kit(ideal);
 434 
 435         // Use the pre-barrier to record the value in the referent field
 436         satb_write_barrier_pre(kit, false /* do_load */,
 437                                NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */,
 438                                pre_val /* pre_val */,
 439                                T_OBJECT);
 440         if (need_mem_bar) {
 441           // Add memory barrier to prevent commoning reads from this field
 442           // across safepoint since GC can change its value.
 443           kit->insert_mem_bar(Op_MemBarCPUOrder);
 444         }
 445         // Update IdealKit from graphKit.
 446         __ sync_kit(kit);
 447 
 448       } __ end_if(); // _ref_type != ref_none
 449   } __ end_if(); // offset == referent_offset
 450 
 451   // Final sync IdealKit and GraphKit.
 452   kit->final_sync(ideal);
 453 }
 454 
 455 #undef __
 456 
 457 const TypeFunc* ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type() {
 458   const Type **fields = TypeTuple::fields(2);
 459   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value
 460   fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // thread
 461   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 462 
 463   // create result type (range)
 464   fields = TypeTuple::fields(0);
 465   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 466 
 467   return TypeFunc::make(domain, range);
 468 }
 469 
 470 const TypeFunc* ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type() {
 471   const Type **fields = TypeTuple::fields(1);
 472   fields[TypeFunc::Parms+0] = TypeOopPtr::NOTNULL; // src oop
 473   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 474 
 475   // create result type (range)
 476   fields = TypeTuple::fields(0);
 477   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 478 
 479   return TypeFunc::make(domain, range);
 480 }
 481 
 482 const TypeFunc* ShenandoahBarrierSetC2::shenandoah_load_reference_barrier_Type() {
 483   const Type **fields = TypeTuple::fields(2);
 484   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value
 485   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;   // original load address
 486 
 487   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 488 
 489   // create result type (range)
 490   fields = TypeTuple::fields(1);
 491   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;
 492   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 493 
 494   return TypeFunc::make(domain, range);
 495 }
 496 
 497 Node* ShenandoahBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {
 498   DecoratorSet decorators = access.decorators();
 499 
 500   const TypePtr* adr_type = access.addr().type();
 501   Node* adr = access.addr().node();
 502 
 503   bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 504   bool on_heap = (decorators & IN_HEAP) != 0;
 505 
 506   if (!access.is_oop() || (!on_heap && !anonymous)) {
 507     return BarrierSetC2::store_at_resolved(access, val);
 508   }
 509 
 510   GraphKit* kit = access.kit();
 511 
 512   uint adr_idx = kit->C->get_alias_index(adr_type);
 513   assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );
 514   Node* value = val.node();
 515   value = shenandoah_storeval_barrier(kit, value);
 516   val.set_node(value);
 517   shenandoah_write_barrier_pre(kit, true /* do_load */, /*kit->control(),*/ access.base(), adr, adr_idx, val.node(),
 518                                static_cast<const TypeOopPtr*>(val.type()), NULL /* pre_val */, access.type());
 519   return BarrierSetC2::store_at_resolved(access, val);
 520 }
 521 
 522 Node* ShenandoahBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const {
 523   // 1: non-reference load, no additional barrier is needed
 524   if (!access.is_oop()) {
 525     return BarrierSetC2::load_at_resolved(access, val_type);;
 526   }
 527 
 528   Node* load = BarrierSetC2::load_at_resolved(access, val_type);
 529   DecoratorSet decorators = access.decorators();
 530   BasicType type = access.type();
 531 
 532   // 2: apply LRB if needed
 533   if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
 534     load = new ShenandoahLoadReferenceBarrierNode(NULL, load);
 535     load = access.kit()->gvn().transform(load);
 536   }
 537 
 538   // 3: apply keep-alive barrier if needed
 539   if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
 540     Node* top = Compile::current()->top();
 541     Node* adr = access.addr().node();
 542     Node* offset = adr->is_AddP() ? adr->in(AddPNode::Offset) : top;
 543     Node* obj = access.base();
 544 
 545     bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 546     bool on_weak_ref = (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) != 0;
 547     bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0;
 548 
 549     // If we are reading the value of the referent field of a Reference
 550     // object (either by using Unsafe directly or through reflection)
 551     // then, if SATB is enabled, we need to record the referent in an
 552     // SATB log buffer using the pre-barrier mechanism.
 553     // Also we need to add memory barrier to prevent commoning reads
 554     // from this field across safepoint since GC can change its value.
 555     if (!on_weak_ref || (unknown && (offset == top || obj == top)) || !keep_alive) {
 556       return load;
 557     }
 558     GraphKit* kit = access.kit();
 559     bool mismatched = (decorators & C2_MISMATCHED) != 0;
 560     bool is_unordered = (decorators & MO_UNORDERED) != 0;
 561     bool need_cpu_mem_bar = !is_unordered || mismatched;
 562 
 563     if (on_weak_ref) {
 564       // Use the pre-barrier to record the value in the referent field
 565       satb_write_barrier_pre(kit, false /* do_load */,
 566                              NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */,
 567                              load /* pre_val */, T_OBJECT);
 568       // Add memory barrier to prevent commoning reads from this field
 569       // across safepoint since GC can change its value.
 570       kit->insert_mem_bar(Op_MemBarCPUOrder);
 571     } else if (unknown) {
 572       // We do not require a mem bar inside pre_barrier if need_mem_bar
 573       // is set: the barriers would be emitted by us.
 574       insert_pre_barrier(kit, obj, offset, load, !need_cpu_mem_bar);
 575     }
 576   }
 577 
 578   return load;
 579 }
 580 
 581 static void pin_atomic_op(C2AtomicAccess& access) {
 582   if (!access.needs_pinning()) {
 583     return;
 584   }
 585   // SCMemProjNodes represent the memory state of a LoadStore. Their
 586   // main role is to prevent LoadStore nodes from being optimized away
 587   // when their results aren't used.
 588   GraphKit* kit = access.kit();
 589   Node* load_store = access.raw_access();
 590   assert(load_store != NULL, "must pin atomic op");
 591   Node* proj = kit->gvn().transform(new SCMemProjNode(load_store));
 592   kit->set_memory(proj, access.alias_idx());
 593 }
 594 
 595 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicAccess& access, Node* expected_val,
 596                                                    Node* new_val, const Type* value_type) const {
 597   GraphKit* kit = access.kit();
 598   if (access.is_oop()) {
 599     new_val = shenandoah_storeval_barrier(kit, new_val);
 600     shenandoah_write_barrier_pre(kit, false /* do_load */,
 601                                  NULL, NULL, max_juint, NULL, NULL,
 602                                  expected_val /* pre_val */, T_OBJECT);
 603 
 604     MemNode::MemOrd mo = access.mem_node_mo();
 605     Node* mem = access.memory();
 606     Node* adr = access.addr().node();
 607     const TypePtr* adr_type = access.addr().type();
 608     Node* load_store = NULL;
 609 
 610 #ifdef _LP64
 611     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
 612       Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));
 613       Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop()));
 614       if (ShenandoahCASBarrier) {
 615         load_store = kit->gvn().transform(new ShenandoahCompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo));
 616       } else {
 617         load_store = kit->gvn().transform(new CompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo));
 618       }
 619     } else
 620 #endif
 621     {
 622       if (ShenandoahCASBarrier) {
 623         load_store = kit->gvn().transform(new ShenandoahCompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo));
 624       } else {
 625         load_store = kit->gvn().transform(new CompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo));
 626       }
 627     }
 628 
 629     access.set_raw_access(load_store);
 630     pin_atomic_op(access);
 631 
 632 #ifdef _LP64
 633     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
 634       load_store = kit->gvn().transform(new DecodeNNode(load_store, load_store->get_ptr_type()));
 635     }
 636 #endif
 637     load_store = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(NULL, load_store));
 638     return load_store;
 639   }
 640   return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
 641 }
 642 
 643 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicAccess& access, Node* expected_val,
 644                                                               Node* new_val, const Type* value_type) const {
 645   GraphKit* kit = access.kit();
 646   if (access.is_oop()) {
 647     new_val = shenandoah_storeval_barrier(kit, new_val);
 648     shenandoah_write_barrier_pre(kit, false /* do_load */,
 649                                  NULL, NULL, max_juint, NULL, NULL,
 650                                  expected_val /* pre_val */, T_OBJECT);
 651     DecoratorSet decorators = access.decorators();
 652     MemNode::MemOrd mo = access.mem_node_mo();
 653     Node* mem = access.memory();
 654     bool is_weak_cas = (decorators & C2_WEAK_CMPXCHG) != 0;
 655     Node* load_store = NULL;
 656     Node* adr = access.addr().node();
 657 #ifdef _LP64
 658     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
 659       Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));
 660       Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop()));
 661       if (ShenandoahCASBarrier) {
 662         if (is_weak_cas) {
 663           load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));
 664         } else {
 665           load_store = kit->gvn().transform(new ShenandoahCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));
 666         }
 667       } else {
 668         if (is_weak_cas) {
 669           load_store = kit->gvn().transform(new WeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));
 670         } else {
 671           load_store = kit->gvn().transform(new CompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));
 672         }
 673       }
 674     } else
 675 #endif
 676     {
 677       if (ShenandoahCASBarrier) {
 678         if (is_weak_cas) {
 679           load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
 680         } else {
 681           load_store = kit->gvn().transform(new ShenandoahCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
 682         }
 683       } else {
 684         if (is_weak_cas) {
 685           load_store = kit->gvn().transform(new WeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
 686         } else {
 687           load_store = kit->gvn().transform(new CompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
 688         }
 689       }
 690     }
 691     access.set_raw_access(load_store);
 692     pin_atomic_op(access);
 693     return load_store;
 694   }
 695   return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
 696 }
 697 
 698 Node* ShenandoahBarrierSetC2::atomic_xchg_at_resolved(C2AtomicAccess& access, Node* val, const Type* value_type) const {
 699   GraphKit* kit = access.kit();
 700   if (access.is_oop()) {
 701     val = shenandoah_storeval_barrier(kit, val);
 702   }
 703   Node* result = BarrierSetC2::atomic_xchg_at_resolved(access, val, value_type);
 704   if (access.is_oop()) {
 705     result = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(NULL, result));
 706     shenandoah_write_barrier_pre(kit, false /* do_load */,
 707                                  NULL, NULL, max_juint, NULL, NULL,
 708                                  result /* pre_val */, T_OBJECT);
 709   }
 710   return result;
 711 }
 712 
 713 // Support for GC barriers emitted during parsing
 714 bool ShenandoahBarrierSetC2::is_gc_barrier_node(Node* node) const {
 715   if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) return true;
 716   if (node->Opcode() != Op_CallLeaf && node->Opcode() != Op_CallLeafNoFP) {
 717     return false;
 718   }
 719   CallLeafNode *call = node->as_CallLeaf();
 720   if (call->_name == NULL) {
 721     return false;
 722   }
 723 
 724   return strcmp(call->_name, "shenandoah_clone_barrier") == 0 ||
 725          strcmp(call->_name, "shenandoah_cas_obj") == 0 ||
 726          strcmp(call->_name, "shenandoah_wb_pre") == 0;
 727 }
 728 
 729 Node* ShenandoahBarrierSetC2::step_over_gc_barrier(Node* c) const {
 730   if (c == NULL) {
 731     return c;
 732   }
 733   if (c->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
 734     return c->in(ShenandoahLoadReferenceBarrierNode::ValueIn);
 735   }
 736   if (c->Opcode() == Op_ShenandoahEnqueueBarrier) {
 737     c = c->in(1);
 738   }
 739   return c;
 740 }
 741 
 742 bool ShenandoahBarrierSetC2::expand_barriers(Compile* C, PhaseIterGVN& igvn) const {
 743   return !ShenandoahBarrierC2Support::expand(C, igvn);
 744 }
 745 
 746 bool ShenandoahBarrierSetC2::optimize_loops(PhaseIdealLoop* phase, LoopOptsMode mode, VectorSet& visited, Node_Stack& nstack, Node_List& worklist) const {
 747   if (mode == LoopOptsShenandoahExpand) {
 748     assert(UseShenandoahGC, "only for shenandoah");
 749     ShenandoahBarrierC2Support::pin_and_expand(phase);
 750     return true;
 751   } else if (mode == LoopOptsShenandoahPostExpand) {
 752     assert(UseShenandoahGC, "only for shenandoah");
 753     visited.Clear();
 754     ShenandoahBarrierC2Support::optimize_after_expansion(visited, nstack, worklist, phase);
 755     return true;
 756   }
 757   return false;
 758 }
 759 
 760 bool ShenandoahBarrierSetC2::array_copy_requires_gc_barriers(BasicType type) const {
 761   return false;
 762 }
 763 
 764 bool ShenandoahBarrierSetC2::clone_needs_barrier(Node* src, PhaseGVN& gvn) {
 765   const TypeOopPtr* src_type = gvn.type(src)->is_oopptr();
 766   if (src_type->isa_instptr() != NULL) {
 767     ciInstanceKlass* ik = src_type->klass()->as_instance_klass();
 768     if ((src_type->klass_is_exact() || (!ik->is_interface() && !ik->has_subklass())) && !ik->has_injected_fields()) {
 769       if (ik->has_object_fields()) {
 770         return true;
 771       } else {
 772         if (!src_type->klass_is_exact()) {
 773           Compile::current()->dependencies()->assert_leaf_type(ik);
 774         }
 775       }
 776     } else {
 777       return true;
 778         }
 779   } else if (src_type->isa_aryptr()) {
 780     BasicType src_elem  = src_type->klass()->as_array_klass()->element_type()->basic_type();
 781     if (src_elem == T_OBJECT || src_elem == T_ARRAY) {
 782       return true;
 783     }
 784   } else {
 785     return true;
 786   }
 787   return false;
 788 }
 789 
 790 void ShenandoahBarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const {
 791   Node* ctrl = ac->in(TypeFunc::Control);
 792   Node* mem = ac->in(TypeFunc::Memory);
 793   Node* src = ac->in(ArrayCopyNode::Src);
 794   Node* src_offset = ac->in(ArrayCopyNode::SrcPos);
 795   Node* dest = ac->in(ArrayCopyNode::Dest);
 796   Node* dest_offset = ac->in(ArrayCopyNode::DestPos);
 797   Node* length = ac->in(ArrayCopyNode::Length);
 798   assert (src_offset == NULL && dest_offset == NULL, "for clone offsets should be null");
 799   assert (src->is_AddP(), "for clone the src should be the interior ptr");
 800   assert (dest->is_AddP(), "for clone the dst should be the interior ptr");
 801 
 802   if (ShenandoahCloneBarrier && clone_needs_barrier(src, phase->igvn())) {
 803     // Check if heap is has forwarded objects. If it does, we need to call into the special
 804     // routine that would fix up source references before we can continue.
 805 
 806     enum { _heap_stable = 1, _heap_unstable, PATH_LIMIT };
 807     Node* region = new RegionNode(PATH_LIMIT);
 808     Node* mem_phi = new PhiNode(region, Type::MEMORY, TypeRawPtr::BOTTOM);
 809 
 810     Node* thread = phase->transform_later(new ThreadLocalNode());
 811     Node* offset = phase->igvn().MakeConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 812     Node* gc_state_addr = phase->transform_later(new AddPNode(phase->C->top(), thread, offset));
 813 
 814     uint gc_state_idx = Compile::AliasIdxRaw;
 815     const TypePtr* gc_state_adr_type = NULL; // debug-mode-only argument
 816     debug_only(gc_state_adr_type = phase->C->get_adr_type(gc_state_idx));
 817 
 818     Node* gc_state    = phase->transform_later(new LoadBNode(ctrl, mem, gc_state_addr, gc_state_adr_type, TypeInt::BYTE, MemNode::unordered));
 819     int flags = ShenandoahHeap::HAS_FORWARDED;
 820     if (ShenandoahStoreValEnqueueBarrier) {
 821       flags |= ShenandoahHeap::MARKING;
 822     }
 823     Node* stable_and  = phase->transform_later(new AndINode(gc_state, phase->igvn().intcon(flags)));
 824     Node* stable_cmp  = phase->transform_later(new CmpINode(stable_and, phase->igvn().zerocon(T_INT)));
 825     Node* stable_test = phase->transform_later(new BoolNode(stable_cmp, BoolTest::ne));
 826 
 827     IfNode* stable_iff  = phase->transform_later(new IfNode(ctrl, stable_test, PROB_UNLIKELY(0.999), COUNT_UNKNOWN))->as_If();
 828     Node* stable_ctrl   = phase->transform_later(new IfFalseNode(stable_iff));
 829     Node* unstable_ctrl = phase->transform_later(new IfTrueNode(stable_iff));
 830 
 831     // Heap is stable, no need to do anything additional
 832     region->init_req(_heap_stable, stable_ctrl);
 833     mem_phi->init_req(_heap_stable, mem);
 834 
 835     // Heap is unstable, call into clone barrier stub
 836     Node* call = phase->make_leaf_call(unstable_ctrl, mem,
 837                     ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type(),
 838                     CAST_FROM_FN_PTR(address, ShenandoahRuntime::shenandoah_clone_barrier),
 839                     "shenandoah_clone",
 840                     TypeRawPtr::BOTTOM,
 841                     src->in(AddPNode::Base));
 842     call = phase->transform_later(call);
 843 
 844     ctrl = phase->transform_later(new ProjNode(call, TypeFunc::Control));
 845     mem = phase->transform_later(new ProjNode(call, TypeFunc::Memory));
 846     region->init_req(_heap_unstable, ctrl);
 847     mem_phi->init_req(_heap_unstable, mem);
 848 
 849     // Wire up the actual arraycopy stub now
 850     ctrl = phase->transform_later(region);
 851     mem = phase->transform_later(mem_phi);
 852 
 853     const char* name = "arraycopy";
 854     call = phase->make_leaf_call(ctrl, mem,
 855                                  OptoRuntime::fast_arraycopy_Type(),
 856                                  phase->basictype2arraycopy(T_LONG, NULL, NULL, true, name, true),
 857                                  name, TypeRawPtr::BOTTOM,
 858                                  src, dest, length
 859                                  LP64_ONLY(COMMA phase->top()));
 860     call = phase->transform_later(call);
 861 
 862     // Hook up the whole thing into the graph
 863     phase->igvn().replace_node(ac, call);
 864   } else {
 865     BarrierSetC2::clone_at_expansion(phase, ac);
 866   }
 867 }
 868 
 869 // Support for macro expanded GC barriers
 870 void ShenandoahBarrierSetC2::register_potential_barrier_node(Node* node) const {
 871   if (node->Opcode() == Op_ShenandoahEnqueueBarrier) {
 872     state()->add_enqueue_barrier((ShenandoahEnqueueBarrierNode*) node);
 873   }
 874   if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
 875     state()->add_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node);
 876   }
 877 }
 878 
 879 void ShenandoahBarrierSetC2::unregister_potential_barrier_node(Node* node) const {
 880   if (node->Opcode() == Op_ShenandoahEnqueueBarrier) {
 881     state()->remove_enqueue_barrier((ShenandoahEnqueueBarrierNode*) node);
 882   }
 883   if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
 884     state()->remove_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node);
 885   }
 886 }
 887 
 888 void ShenandoahBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* n) const {
 889   if (is_shenandoah_wb_pre_call(n)) {
 890     shenandoah_eliminate_wb_pre(n, &macro->igvn());
 891   }
 892 }
 893 
 894 void ShenandoahBarrierSetC2::shenandoah_eliminate_wb_pre(Node* call, PhaseIterGVN* igvn) const {
 895   assert(UseShenandoahGC && is_shenandoah_wb_pre_call(call), "");
 896   Node* c = call->as_Call()->proj_out(TypeFunc::Control);
 897   c = c->unique_ctrl_out();
 898   assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
 899   c = c->unique_ctrl_out();
 900   assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
 901   Node* iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0);
 902   assert(iff->is_If(), "expect test");
 903   if (!is_shenandoah_marking_if(igvn, iff)) {
 904     c = c->unique_ctrl_out();
 905     assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
 906     iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0);
 907     assert(is_shenandoah_marking_if(igvn, iff), "expect marking test");
 908   }
 909   Node* cmpx = iff->in(1)->in(1);
 910   igvn->replace_node(cmpx, igvn->makecon(TypeInt::CC_EQ));
 911   igvn->rehash_node_delayed(call);
 912   call->del_req(call->req()-1);
 913 }
 914 
 915 void ShenandoahBarrierSetC2::enqueue_useful_gc_barrier(Unique_Node_List &worklist, Node* node) const {
 916   if (node->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(node)) {
 917     for (DUIterator_Fast imax, i = node->fast_outs(imax); i < imax; i++) {
 918       Node* use = node->fast_out(i);
 919       worklist.push(use);
 920     }
 921   }
 922 }
 923 
 924 void ShenandoahBarrierSetC2::eliminate_useless_gc_barriers(Unique_Node_List &useful) const {
 925   for (uint i = 0; i < useful.size(); i++) {
 926     Node* n = useful.at(i);
 927     if (n->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(n)) {
 928       for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
 929         Compile::current()->record_for_igvn(n->fast_out(i));
 930       }
 931     }
 932   }
 933   for (int i = state()->enqueue_barriers_count() - 1; i >= 0; i--) {
 934     ShenandoahEnqueueBarrierNode* n = state()->enqueue_barrier(i);
 935     if (!useful.member(n)) {
 936       state()->remove_enqueue_barrier(n);
 937     }
 938   }
 939   for (int i = state()->load_reference_barriers_count() - 1; i >= 0; i--) {
 940     ShenandoahLoadReferenceBarrierNode* n = state()->load_reference_barrier(i);
 941     if (!useful.member(n)) {
 942       state()->remove_load_reference_barrier(n);
 943     }
 944   }
 945 }
 946 
 947 void ShenandoahBarrierSetC2::add_users_to_worklist(Unique_Node_List* worklist) const {}
 948 
 949 void* ShenandoahBarrierSetC2::create_barrier_state(Arena* comp_arena) const {
 950   return new(comp_arena) ShenandoahBarrierSetC2State(comp_arena);
 951 }
 952 
 953 ShenandoahBarrierSetC2State* ShenandoahBarrierSetC2::state() const {
 954   return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state());
 955 }
 956 
 957 // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be
 958 // expanded later, then now is the time to do so.
 959 bool ShenandoahBarrierSetC2::expand_macro_nodes(PhaseMacroExpand* macro) const { return false; }
 960 
 961 #ifdef ASSERT
 962 void ShenandoahBarrierSetC2::verify_gc_barriers(bool post_parse) const {
 963   if (ShenandoahVerifyOptoBarriers && !post_parse) {
 964     ShenandoahBarrierC2Support::verify(Compile::current()->root());
 965   }
 966 }
 967 #endif
 968 
 969 Node* ShenandoahBarrierSetC2::ideal_node(PhaseGVN* phase, Node* n, bool can_reshape) const {
 970   if (is_shenandoah_wb_pre_call(n)) {
 971     uint cnt = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type()->domain()->cnt();
 972     if (n->req() > cnt) {
 973       Node* addp = n->in(cnt);
 974       if (has_only_shenandoah_wb_pre_uses(addp)) {
 975         n->del_req(cnt);
 976         if (can_reshape) {
 977           phase->is_IterGVN()->_worklist.push(addp);
 978         }
 979         return n;
 980       }
 981     }
 982   }
 983   if (n->Opcode() == Op_CmpP) {
 984     Node* in1 = n->in(1);
 985     Node* in2 = n->in(2);
 986     if (in1->bottom_type() == TypePtr::NULL_PTR) {
 987       in2 = step_over_gc_barrier(in2);
 988     }
 989     if (in2->bottom_type() == TypePtr::NULL_PTR) {
 990       in1 = step_over_gc_barrier(in1);
 991     }
 992     PhaseIterGVN* igvn = phase->is_IterGVN();
 993     if (in1 != n->in(1)) {
 994       if (igvn != NULL) {
 995         n->set_req_X(1, in1, igvn);
 996       } else {
 997         n->set_req(1, in1);
 998       }
 999       assert(in2 == n->in(2), "only one change");
1000       return n;
1001     }
1002     if (in2 != n->in(2)) {
1003       if (igvn != NULL) {
1004         n->set_req_X(2, in2, igvn);
1005       } else {
1006         n->set_req(2, in2);
1007       }
1008       return n;
1009     }
1010   } else if (can_reshape &&
1011              n->Opcode() == Op_If &&
1012              ShenandoahBarrierC2Support::is_heap_stable_test(n) &&
1013              n->in(0) != NULL) {
1014     Node* dom = n->in(0);
1015     Node* prev_dom = n;
1016     int op = n->Opcode();
1017     int dist = 16;
1018     // Search up the dominator tree for another heap stable test
1019     while (dom->Opcode() != op    ||  // Not same opcode?
1020            !ShenandoahBarrierC2Support::is_heap_stable_test(dom) ||  // Not same input 1?
1021            prev_dom->in(0) != dom) {  // One path of test does not dominate?
1022       if (dist < 0) return NULL;
1023 
1024       dist--;
1025       prev_dom = dom;
1026       dom = IfNode::up_one_dom(dom);
1027       if (!dom) return NULL;
1028     }
1029 
1030     // Check that we did not follow a loop back to ourselves
1031     if (n == dom) {
1032       return NULL;
1033     }
1034 
1035     return n->as_If()->dominated_by(prev_dom, phase->is_IterGVN());
1036   }
1037   return NULL;
1038 }
1039 
1040 bool ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(Node* n) {
1041   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1042     Node* u = n->fast_out(i);
1043     if (!is_shenandoah_wb_pre_call(u)) {
1044       return false;
1045     }
1046   }
1047   return n->outcnt() > 0;
1048 }
1049 
1050 Node* ShenandoahBarrierSetC2::arraycopy_load_reference_barrier(PhaseGVN *phase, Node* v) {
1051   if (ShenandoahLoadRefBarrier) {
1052     return phase->transform(new ShenandoahLoadReferenceBarrierNode(NULL, v));
1053   }
1054   if (ShenandoahStoreValEnqueueBarrier) {
1055     return phase->transform(new ShenandoahEnqueueBarrierNode(v));
1056   }
1057   return v;
1058 }
1059