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