1 /*
  2  * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "gc/shared/tlab_globals.hpp"
 27 #include "gc/shared/c2/barrierSetC2.hpp"
 28 #include "opto/arraycopynode.hpp"
 29 #include "opto/convertnode.hpp"
 30 #include "opto/graphKit.hpp"
 31 #include "opto/idealKit.hpp"
 32 #include "opto/macro.hpp"
 33 #include "opto/narrowptrnode.hpp"
 34 #include "opto/runtime.hpp"
 35 #include "utilities/macros.hpp"
 36 
 37 // By default this is a no-op.
 38 void BarrierSetC2::resolve_address(C2Access& access) const { }
 39 
 40 void* C2ParseAccess::barrier_set_state() const {
 41   return _kit->barrier_set_state();
 42 }
 43 
 44 PhaseGVN& C2ParseAccess::gvn() const { return _kit->gvn(); }
 45 
 46 Node* C2ParseAccess::control() const {
 47   return _ctl == NULL ? _kit->control() : _ctl;
 48 }
 49 
 50 bool C2Access::needs_cpu_membar() const {
 51   bool mismatched   = (_decorators & C2_MISMATCHED) != 0;
 52   bool is_unordered = (_decorators & MO_UNORDERED) != 0;
 53 
 54   bool anonymous = (_decorators & C2_UNSAFE_ACCESS) != 0;
 55   bool in_heap   = (_decorators & IN_HEAP) != 0;
 56   bool in_native = (_decorators & IN_NATIVE) != 0;
 57   bool is_mixed  = !in_heap && !in_native;
 58 
 59   bool is_write  = (_decorators & C2_WRITE_ACCESS) != 0;
 60   bool is_read   = (_decorators & C2_READ_ACCESS) != 0;
 61   bool is_atomic = is_read && is_write;
 62 
 63   if (is_atomic) {
 64     // Atomics always need to be wrapped in CPU membars
 65     return true;
 66   }
 67 
 68   if (anonymous) {
 69     // We will need memory barriers unless we can determine a unique
 70     // alias category for this reference.  (Note:  If for some reason
 71     // the barriers get omitted and the unsafe reference begins to "pollute"
 72     // the alias analysis of the rest of the graph, either Compile::can_alias
 73     // or Compile::must_alias will throw a diagnostic assert.)
 74     if (is_mixed || !is_unordered || (mismatched && !_addr.type()->isa_aryptr())) {
 75       return true;
 76     }
 77   } else {
 78     assert(!is_mixed, "not unsafe");
 79   }
 80 
 81   return false;
 82 }
 83 
 84 Node* BarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {
 85   DecoratorSet decorators = access.decorators();
 86 
 87   bool mismatched = (decorators & C2_MISMATCHED) != 0;
 88   bool unaligned = (decorators & C2_UNALIGNED) != 0;
 89   bool unsafe = (decorators & C2_UNSAFE_ACCESS) != 0;
 90   bool requires_atomic_access = (decorators & MO_UNORDERED) == 0;
 91 
 92   bool in_native = (decorators & IN_NATIVE) != 0;
 93   assert(!in_native || (unsafe && !access.is_oop()), "not supported yet");
 94 
 95   MemNode::MemOrd mo = access.mem_node_mo();
 96 
 97   Node* store;
 98   if (access.is_parse_access()) {
 99     C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access);
100 
101     GraphKit* kit = parse_access.kit();
102     if (access.type() == T_DOUBLE) {
103       Node* new_val = kit->dstore_rounding(val.node());
104       val.set_node(new_val);
105     }
106 
107     store = kit->store_to_memory(kit->control(), access.addr().node(), val.node(), access.type(),
108                                      access.addr().type(), mo, requires_atomic_access, unaligned, mismatched, unsafe);
109   } else {
110     assert(!requires_atomic_access, "not yet supported");
111     assert(access.is_opt_access(), "either parse or opt access");
112     C2OptAccess& opt_access = static_cast<C2OptAccess&>(access);
113     Node* ctl = opt_access.ctl();
114     MergeMemNode* mm = opt_access.mem();
115     PhaseGVN& gvn = opt_access.gvn();
116     const TypePtr* adr_type = access.addr().type();
117     int alias = gvn.C->get_alias_index(adr_type);
118     Node* mem = mm->memory_at(alias);
119 
120     StoreNode* st = StoreNode::make(gvn, ctl, mem, access.addr().node(), adr_type, val.node(), access.type(), mo);
121     if (unaligned) {
122       st->set_unaligned_access();
123     }
124     if (mismatched) {
125       st->set_mismatched_access();
126     }
127     store = gvn.transform(st);
128     if (store == st) {
129       mm->set_memory_at(alias, st);
130     }
131   }
132   access.set_raw_access(store);
133 
134   return store;
135 }
136 
137 Node* BarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const {
138   DecoratorSet decorators = access.decorators();
139 
140   Node* adr = access.addr().node();
141   const TypePtr* adr_type = access.addr().type();
142 
143   bool mismatched = (decorators & C2_MISMATCHED) != 0;
144   bool requires_atomic_access = (decorators & MO_UNORDERED) == 0;
145   bool unaligned = (decorators & C2_UNALIGNED) != 0;
146   bool control_dependent = (decorators & C2_CONTROL_DEPENDENT_LOAD) != 0;
147   bool unknown_control = (decorators & C2_UNKNOWN_CONTROL_LOAD) != 0;
148   bool unsafe = (decorators & C2_UNSAFE_ACCESS) != 0;
149   bool immutable = (decorators & C2_IMMUTABLE_MEMORY) != 0;
150 
151   bool in_native = (decorators & IN_NATIVE) != 0;
152 
153   MemNode::MemOrd mo = access.mem_node_mo();
154   LoadNode::ControlDependency dep = unknown_control ? LoadNode::UnknownControl : LoadNode::DependsOnlyOnTest;
155 
156   Node* load;
157   if (access.is_parse_access()) {
158     C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access);
159     GraphKit* kit = parse_access.kit();
160     Node* control = control_dependent ? parse_access.control() : NULL;
161 
162     if (immutable) {
163       assert(!requires_atomic_access, "can't ensure atomicity");
164       Compile* C = Compile::current();
165       Node* mem = kit->immutable_memory();
166       load = LoadNode::make(kit->gvn(), control, mem, adr,
167                             adr_type, val_type, access.type(), mo, dep, unaligned,
168                             mismatched, unsafe, access.barrier_data());
169       load = kit->gvn().transform(load);
170     } else {
171       load = kit->make_load(control, adr, val_type, access.type(), adr_type, mo,
172                             dep, requires_atomic_access, unaligned, mismatched, unsafe,
173                             access.barrier_data());
174     }
175   } else {
176     assert(!requires_atomic_access, "not yet supported");
177     assert(access.is_opt_access(), "either parse or opt access");
178     C2OptAccess& opt_access = static_cast<C2OptAccess&>(access);
179     Node* control = control_dependent ? opt_access.ctl() : NULL;
180     MergeMemNode* mm = opt_access.mem();
181     PhaseGVN& gvn = opt_access.gvn();
182     Node* mem = mm->memory_at(gvn.C->get_alias_index(adr_type));
183     load = LoadNode::make(gvn, control, mem, adr, adr_type, val_type, access.type(), mo,
184                           dep, unaligned, mismatched, unsafe, access.barrier_data());
185     load = gvn.transform(load);
186   }
187   access.set_raw_access(load);
188 
189   return load;
190 }
191 
192 class C2AccessFence: public StackObj {
193   C2Access& _access;
194   Node* _leading_membar;
195 
196 public:
197   C2AccessFence(C2Access& access) :
198     _access(access), _leading_membar(NULL) {
199     GraphKit* kit = NULL;
200     if (access.is_parse_access()) {
201       C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access);
202       kit = parse_access.kit();
203     }
204     DecoratorSet decorators = access.decorators();
205 
206     bool is_write = (decorators & C2_WRITE_ACCESS) != 0;
207     bool is_read = (decorators & C2_READ_ACCESS) != 0;
208     bool is_atomic = is_read && is_write;
209 
210     bool is_volatile = (decorators & MO_SEQ_CST) != 0;
211     bool is_release = (decorators & MO_RELEASE) != 0;
212 
213     if (is_atomic) {
214       assert(kit != NULL, "unsupported at optimization time");
215       // Memory-model-wise, a LoadStore acts like a little synchronized
216       // block, so needs barriers on each side.  These don't translate
217       // into actual barriers on most machines, but we still need rest of
218       // compiler to respect ordering.
219       if (is_release) {
220         _leading_membar = kit->insert_mem_bar(Op_MemBarRelease);
221       } else if (is_volatile) {
222         if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
223           _leading_membar = kit->insert_mem_bar(Op_MemBarVolatile);
224         } else {
225           _leading_membar = kit->insert_mem_bar(Op_MemBarRelease);
226         }
227       }
228     } else if (is_write) {
229       // If reference is volatile, prevent following memory ops from
230       // floating down past the volatile write.  Also prevents commoning
231       // another volatile read.
232       if (is_volatile || is_release) {
233         assert(kit != NULL, "unsupported at optimization time");
234         _leading_membar = kit->insert_mem_bar(Op_MemBarRelease);
235       }
236     } else {
237       // Memory barrier to prevent normal and 'unsafe' accesses from
238       // bypassing each other.  Happens after null checks, so the
239       // exception paths do not take memory state from the memory barrier,
240       // so there's no problems making a strong assert about mixing users
241       // of safe & unsafe memory.
242       if (is_volatile && support_IRIW_for_not_multiple_copy_atomic_cpu) {
243         assert(kit != NULL, "unsupported at optimization time");
244         _leading_membar = kit->insert_mem_bar(Op_MemBarVolatile);
245       }
246     }
247 
248     if (access.needs_cpu_membar()) {
249       assert(kit != NULL, "unsupported at optimization time");
250       kit->insert_mem_bar(Op_MemBarCPUOrder);
251     }
252 
253     if (is_atomic) {
254       // 4984716: MemBars must be inserted before this
255       //          memory node in order to avoid a false
256       //          dependency which will confuse the scheduler.
257       access.set_memory();
258     }
259   }
260 
261   ~C2AccessFence() {
262     GraphKit* kit = NULL;
263     if (_access.is_parse_access()) {
264       C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(_access);
265       kit = parse_access.kit();
266     }
267     DecoratorSet decorators = _access.decorators();
268 
269     bool is_write = (decorators & C2_WRITE_ACCESS) != 0;
270     bool is_read = (decorators & C2_READ_ACCESS) != 0;
271     bool is_atomic = is_read && is_write;
272 
273     bool is_volatile = (decorators & MO_SEQ_CST) != 0;
274     bool is_acquire = (decorators & MO_ACQUIRE) != 0;
275 
276     // If reference is volatile, prevent following volatiles ops from
277     // floating up before the volatile access.
278     if (_access.needs_cpu_membar()) {
279       kit->insert_mem_bar(Op_MemBarCPUOrder);
280     }
281 
282     if (is_atomic) {
283       assert(kit != NULL, "unsupported at optimization time");
284       if (is_acquire || is_volatile) {
285         Node* n = _access.raw_access();
286         Node* mb = kit->insert_mem_bar(Op_MemBarAcquire, n);
287         if (_leading_membar != NULL) {
288           MemBarNode::set_load_store_pair(_leading_membar->as_MemBar(), mb->as_MemBar());
289         }
290       }
291     } else if (is_write) {
292       // If not multiple copy atomic, we do the MemBarVolatile before the load.
293       if (is_volatile && !support_IRIW_for_not_multiple_copy_atomic_cpu) {
294         assert(kit != NULL, "unsupported at optimization time");
295         Node* n = _access.raw_access();
296         Node* mb = kit->insert_mem_bar(Op_MemBarVolatile, n); // Use fat membar
297         if (_leading_membar != NULL) {
298           MemBarNode::set_store_pair(_leading_membar->as_MemBar(), mb->as_MemBar());
299         }
300       }
301     } else {
302       if (is_volatile || is_acquire) {
303         assert(kit != NULL, "unsupported at optimization time");
304         Node* n = _access.raw_access();
305         assert(_leading_membar == NULL || support_IRIW_for_not_multiple_copy_atomic_cpu, "no leading membar expected");
306         Node* mb = kit->insert_mem_bar(Op_MemBarAcquire, n);
307         mb->as_MemBar()->set_trailing_load();
308       }
309     }
310   }
311 };
312 
313 Node* BarrierSetC2::store_at(C2Access& access, C2AccessValue& val) const {
314   C2AccessFence fence(access);
315   resolve_address(access);
316   return store_at_resolved(access, val);
317 }
318 
319 Node* BarrierSetC2::load_at(C2Access& access, const Type* val_type) const {
320   C2AccessFence fence(access);
321   resolve_address(access);
322   return load_at_resolved(access, val_type);
323 }
324 
325 MemNode::MemOrd C2Access::mem_node_mo() const {
326   bool is_write = (_decorators & C2_WRITE_ACCESS) != 0;
327   bool is_read = (_decorators & C2_READ_ACCESS) != 0;
328   if ((_decorators & MO_SEQ_CST) != 0) {
329     if (is_write && is_read) {
330       // For atomic operations
331       return MemNode::seqcst;
332     } else if (is_write) {
333       return MemNode::release;
334     } else {
335       assert(is_read, "what else?");
336       return MemNode::acquire;
337     }
338   } else if ((_decorators & MO_RELEASE) != 0) {
339     return MemNode::release;
340   } else if ((_decorators & MO_ACQUIRE) != 0) {
341     return MemNode::acquire;
342   } else if (is_write) {
343     // Volatile fields need releasing stores.
344     // Non-volatile fields also need releasing stores if they hold an
345     // object reference, because the object reference might point to
346     // a freshly created object.
347     // Conservatively release stores of object references.
348     return StoreNode::release_if_reference(_type);
349   } else {
350     return MemNode::unordered;
351   }
352 }
353 
354 void C2Access::fixup_decorators() {
355   bool default_mo = (_decorators & MO_DECORATOR_MASK) == 0;
356   bool is_unordered = (_decorators & MO_UNORDERED) != 0 || default_mo;
357   bool anonymous = (_decorators & C2_UNSAFE_ACCESS) != 0;
358 
359   bool is_read = (_decorators & C2_READ_ACCESS) != 0;
360   bool is_write = (_decorators & C2_WRITE_ACCESS) != 0;
361 
362   if (AlwaysAtomicAccesses && is_unordered) {
363     _decorators &= ~MO_DECORATOR_MASK; // clear the MO bits
364     _decorators |= MO_RELAXED; // Force the MO_RELAXED decorator with AlwaysAtomicAccess
365   }
366 
367   _decorators = AccessInternal::decorator_fixup(_decorators);
368 
369   if (is_read && !is_write && anonymous) {
370     // To be valid, unsafe loads may depend on other conditions than
371     // the one that guards them: pin the Load node
372     _decorators |= C2_CONTROL_DEPENDENT_LOAD;
373     _decorators |= C2_UNKNOWN_CONTROL_LOAD;
374     const TypePtr* adr_type = _addr.type();
375     Node* adr = _addr.node();
376     if (!needs_cpu_membar() && adr_type->isa_instptr()) {
377       assert(adr_type->meet(TypePtr::NULL_PTR) != adr_type->remove_speculative(), "should be not null");
378       intptr_t offset = Type::OffsetBot;
379       AddPNode::Ideal_base_and_offset(adr, &gvn(), offset);
380       if (offset >= 0) {
381         int s = Klass::layout_helper_size_in_bytes(adr_type->isa_instptr()->klass()->layout_helper());
382         if (offset < s) {
383           // Guaranteed to be a valid access, no need to pin it
384           _decorators ^= C2_CONTROL_DEPENDENT_LOAD;
385           _decorators ^= C2_UNKNOWN_CONTROL_LOAD;
386         }
387       }
388     }
389   }
390 }
391 
392 //--------------------------- atomic operations---------------------------------
393 
394 void BarrierSetC2::pin_atomic_op(C2AtomicParseAccess& access) const {
395   if (!access.needs_pinning()) {
396     return;
397   }
398   // SCMemProjNodes represent the memory state of a LoadStore. Their
399   // main role is to prevent LoadStore nodes from being optimized away
400   // when their results aren't used.
401   assert(access.is_parse_access(), "entry not supported at optimization time");
402   C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access);
403   GraphKit* kit = parse_access.kit();
404   Node* load_store = access.raw_access();
405   assert(load_store != NULL, "must pin atomic op");
406   Node* proj = kit->gvn().transform(new SCMemProjNode(load_store));
407   kit->set_memory(proj, access.alias_idx());
408 }
409 
410 void C2AtomicParseAccess::set_memory() {
411   Node *mem = _kit->memory(_alias_idx);
412   _memory = mem;
413 }
414 
415 Node* BarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
416                                                    Node* new_val, const Type* value_type) const {
417   GraphKit* kit = access.kit();
418   MemNode::MemOrd mo = access.mem_node_mo();
419   Node* mem = access.memory();
420 
421   Node* adr = access.addr().node();
422   const TypePtr* adr_type = access.addr().type();
423 
424   Node* load_store = NULL;
425 
426   if (access.is_oop()) {
427 #ifdef _LP64
428     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
429       Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));
430       Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop()));
431       load_store = new CompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo);
432     } else
433 #endif
434     {
435       load_store = new CompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo);
436     }
437   } else {
438     switch (access.type()) {
439       case T_BYTE: {
440         load_store = new CompareAndExchangeBNode(kit->control(), mem, adr, new_val, expected_val, adr_type, mo);
441         break;
442       }
443       case T_SHORT: {
444         load_store = new CompareAndExchangeSNode(kit->control(), mem, adr, new_val, expected_val, adr_type, mo);
445         break;
446       }
447       case T_INT: {
448         load_store = new CompareAndExchangeINode(kit->control(), mem, adr, new_val, expected_val, adr_type, mo);
449         break;
450       }
451       case T_LONG: {
452         load_store = new CompareAndExchangeLNode(kit->control(), mem, adr, new_val, expected_val, adr_type, mo);
453         break;
454       }
455       default:
456         ShouldNotReachHere();
457     }
458   }
459 
460   load_store->as_LoadStore()->set_barrier_data(access.barrier_data());
461   load_store = kit->gvn().transform(load_store);
462 
463   access.set_raw_access(load_store);
464   pin_atomic_op(access);
465 
466 #ifdef _LP64
467   if (access.is_oop() && adr->bottom_type()->is_ptr_to_narrowoop()) {
468     return kit->gvn().transform(new DecodeNNode(load_store, load_store->get_ptr_type()));
469   }
470 #endif
471 
472   return load_store;
473 }
474 
475 Node* BarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
476                                                     Node* new_val, const Type* value_type) const {
477   GraphKit* kit = access.kit();
478   DecoratorSet decorators = access.decorators();
479   MemNode::MemOrd mo = access.mem_node_mo();
480   Node* mem = access.memory();
481   bool is_weak_cas = (decorators & C2_WEAK_CMPXCHG) != 0;
482   Node* load_store = NULL;
483   Node* adr = access.addr().node();
484 
485   if (access.is_oop()) {
486 #ifdef _LP64
487     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
488       Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));
489       Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop()));
490       if (is_weak_cas) {
491         load_store = new WeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo);
492       } else {
493         load_store = new CompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo);
494       }
495     } else
496 #endif
497     {
498       if (is_weak_cas) {
499         load_store = new WeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo);
500       } else {
501         load_store = new CompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo);
502       }
503     }
504   } else {
505     switch(access.type()) {
506       case T_BYTE: {
507         if (is_weak_cas) {
508           load_store = new WeakCompareAndSwapBNode(kit->control(), mem, adr, new_val, expected_val, mo);
509         } else {
510           load_store = new CompareAndSwapBNode(kit->control(), mem, adr, new_val, expected_val, mo);
511         }
512         break;
513       }
514       case T_SHORT: {
515         if (is_weak_cas) {
516           load_store = new WeakCompareAndSwapSNode(kit->control(), mem, adr, new_val, expected_val, mo);
517         } else {
518           load_store = new CompareAndSwapSNode(kit->control(), mem, adr, new_val, expected_val, mo);
519         }
520         break;
521       }
522       case T_INT: {
523         if (is_weak_cas) {
524           load_store = new WeakCompareAndSwapINode(kit->control(), mem, adr, new_val, expected_val, mo);
525         } else {
526           load_store = new CompareAndSwapINode(kit->control(), mem, adr, new_val, expected_val, mo);
527         }
528         break;
529       }
530       case T_LONG: {
531         if (is_weak_cas) {
532           load_store = new WeakCompareAndSwapLNode(kit->control(), mem, adr, new_val, expected_val, mo);
533         } else {
534           load_store = new CompareAndSwapLNode(kit->control(), mem, adr, new_val, expected_val, mo);
535         }
536         break;
537       }
538       default:
539         ShouldNotReachHere();
540     }
541   }
542 
543   load_store->as_LoadStore()->set_barrier_data(access.barrier_data());
544   load_store = kit->gvn().transform(load_store);
545 
546   access.set_raw_access(load_store);
547   pin_atomic_op(access);
548 
549   return load_store;
550 }
551 
552 Node* BarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const {
553   GraphKit* kit = access.kit();
554   Node* mem = access.memory();
555   Node* adr = access.addr().node();
556   const TypePtr* adr_type = access.addr().type();
557   Node* load_store = NULL;
558 
559   if (access.is_oop()) {
560 #ifdef _LP64
561     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
562       Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));
563       load_store = kit->gvn().transform(new GetAndSetNNode(kit->control(), mem, adr, newval_enc, adr_type, value_type->make_narrowoop()));
564     } else
565 #endif
566     {
567       load_store = new GetAndSetPNode(kit->control(), mem, adr, new_val, adr_type, value_type->is_oopptr());
568     }
569   } else  {
570     switch (access.type()) {
571       case T_BYTE:
572         load_store = new GetAndSetBNode(kit->control(), mem, adr, new_val, adr_type);
573         break;
574       case T_SHORT:
575         load_store = new GetAndSetSNode(kit->control(), mem, adr, new_val, adr_type);
576         break;
577       case T_INT:
578         load_store = new GetAndSetINode(kit->control(), mem, adr, new_val, adr_type);
579         break;
580       case T_LONG:
581         load_store = new GetAndSetLNode(kit->control(), mem, adr, new_val, adr_type);
582         break;
583       default:
584         ShouldNotReachHere();
585     }
586   }
587 
588   load_store->as_LoadStore()->set_barrier_data(access.barrier_data());
589   load_store = kit->gvn().transform(load_store);
590 
591   access.set_raw_access(load_store);
592   pin_atomic_op(access);
593 
594 #ifdef _LP64
595   if (access.is_oop() && adr->bottom_type()->is_ptr_to_narrowoop()) {
596     return kit->gvn().transform(new DecodeNNode(load_store, load_store->get_ptr_type()));
597   }
598 #endif
599 
600   return load_store;
601 }
602 
603 Node* BarrierSetC2::atomic_add_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const {
604   Node* load_store = NULL;
605   GraphKit* kit = access.kit();
606   Node* adr = access.addr().node();
607   const TypePtr* adr_type = access.addr().type();
608   Node* mem = access.memory();
609 
610   switch(access.type()) {
611     case T_BYTE:
612       load_store = new GetAndAddBNode(kit->control(), mem, adr, new_val, adr_type);
613       break;
614     case T_SHORT:
615       load_store = new GetAndAddSNode(kit->control(), mem, adr, new_val, adr_type);
616       break;
617     case T_INT:
618       load_store = new GetAndAddINode(kit->control(), mem, adr, new_val, adr_type);
619       break;
620     case T_LONG:
621       load_store = new GetAndAddLNode(kit->control(), mem, adr, new_val, adr_type);
622       break;
623     default:
624       ShouldNotReachHere();
625   }
626 
627   load_store->as_LoadStore()->set_barrier_data(access.barrier_data());
628   load_store = kit->gvn().transform(load_store);
629 
630   access.set_raw_access(load_store);
631   pin_atomic_op(access);
632 
633   return load_store;
634 }
635 
636 Node* BarrierSetC2::atomic_cmpxchg_val_at(C2AtomicParseAccess& access, Node* expected_val,
637                                           Node* new_val, const Type* value_type) const {
638   C2AccessFence fence(access);
639   resolve_address(access);
640   return atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
641 }
642 
643 Node* BarrierSetC2::atomic_cmpxchg_bool_at(C2AtomicParseAccess& access, Node* expected_val,
644                                            Node* new_val, const Type* value_type) const {
645   C2AccessFence fence(access);
646   resolve_address(access);
647   return atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
648 }
649 
650 Node* BarrierSetC2::atomic_xchg_at(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const {
651   C2AccessFence fence(access);
652   resolve_address(access);
653   return atomic_xchg_at_resolved(access, new_val, value_type);
654 }
655 
656 Node* BarrierSetC2::atomic_add_at(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const {
657   C2AccessFence fence(access);
658   resolve_address(access);
659   return atomic_add_at_resolved(access, new_val, value_type);
660 }
661 
662 int BarrierSetC2::arraycopy_payload_base_offset(bool is_array) {
663   // Exclude the header but include array length to copy by 8 bytes words.
664   // Can't use base_offset_in_bytes(bt) since basic type is unknown.
665   int base_off = is_array ? arrayOopDesc::length_offset_in_bytes() :
666                             instanceOopDesc::base_offset_in_bytes();
667   // base_off:
668   // 8  - 32-bit VM
669   // 12 - 64-bit VM, compressed klass
670   // 16 - 64-bit VM, normal klass
671   if (base_off % BytesPerLong != 0) {
672     assert(UseCompressedClassPointers, "");
673     if (is_array) {
674       // Exclude length to copy by 8 bytes words.
675       base_off += sizeof(int);
676     } else {
677       // Include klass to copy by 8 bytes words.
678       base_off = instanceOopDesc::klass_offset_in_bytes();
679     }
680     assert(base_off % BytesPerLong == 0, "expect 8 bytes alignment");
681   }
682   return base_off;
683 }
684 
685 void BarrierSetC2::clone(GraphKit* kit, Node* src_base, Node* dst_base, Node* size, bool is_array) const {
686   int base_off = arraycopy_payload_base_offset(is_array);
687   Node* payload_size = size;
688   Node* offset = kit->MakeConX(base_off);
689   payload_size = kit->gvn().transform(new SubXNode(payload_size, offset));
690   payload_size = kit->gvn().transform(new URShiftXNode(payload_size, kit->intcon(LogBytesPerLong)));
691   ArrayCopyNode* ac = ArrayCopyNode::make(kit, false, src_base, offset, dst_base, offset, payload_size, true, false);
692   if (is_array) {
693     ac->set_clone_array();
694   } else {
695     ac->set_clone_inst();
696   }
697   Node* n = kit->gvn().transform(ac);
698   if (n == ac) {
699     const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
700     ac->set_adr_type(TypeRawPtr::BOTTOM);
701     kit->set_predefined_output_for_runtime_call(ac, ac->in(TypeFunc::Memory), raw_adr_type);
702   } else {
703     kit->set_all_memory(n);
704   }
705 }
706 
707 Node* BarrierSetC2::obj_allocate(PhaseMacroExpand* macro, Node* mem, Node* toobig_false, Node* size_in_bytes,
708                                  Node*& i_o, Node*& needgc_ctrl,
709                                  Node*& fast_oop_ctrl, Node*& fast_oop_rawmem,
710                                  intx prefetch_lines) const {
711 
712   Node* eden_top_adr;
713   Node* eden_end_adr;
714 
715   macro->set_eden_pointers(eden_top_adr, eden_end_adr);
716 
717   // Load Eden::end.  Loop invariant and hoisted.
718   //
719   // Note: We set the control input on "eden_end" and "old_eden_top" when using
720   //       a TLAB to work around a bug where these values were being moved across
721   //       a safepoint.  These are not oops, so they cannot be include in the oop
722   //       map, but they can be changed by a GC.   The proper way to fix this would
723   //       be to set the raw memory state when generating a  SafepointNode.  However
724   //       this will require extensive changes to the loop optimization in order to
725   //       prevent a degradation of the optimization.
726   //       See comment in memnode.hpp, around line 227 in class LoadPNode.
727   Node *eden_end = macro->make_load(toobig_false, mem, eden_end_adr, 0, TypeRawPtr::BOTTOM, T_ADDRESS);
728 
729   // We need a Region for the loop-back contended case.
730   enum { fall_in_path = 1, contended_loopback_path = 2 };
731   Node *contended_region;
732   Node *contended_phi_rawmem;
733   if (UseTLAB) {
734     contended_region = toobig_false;
735     contended_phi_rawmem = mem;
736   } else {
737     contended_region = new RegionNode(3);
738     contended_phi_rawmem = new PhiNode(contended_region, Type::MEMORY, TypeRawPtr::BOTTOM);
739     // Now handle the passing-too-big test.  We fall into the contended
740     // loop-back merge point.
741     contended_region    ->init_req(fall_in_path, toobig_false);
742     contended_phi_rawmem->init_req(fall_in_path, mem);
743     macro->transform_later(contended_region);
744     macro->transform_later(contended_phi_rawmem);
745   }
746 
747   // Load(-locked) the heap top.
748   // See note above concerning the control input when using a TLAB
749   Node *old_eden_top = UseTLAB
750     ? new LoadPNode      (toobig_false, contended_phi_rawmem, eden_top_adr, TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM, MemNode::unordered)
751     : new LoadPLockedNode(contended_region, contended_phi_rawmem, eden_top_adr, MemNode::acquire);
752 
753   macro->transform_later(old_eden_top);
754   // Add to heap top to get a new heap top
755   Node *new_eden_top = new AddPNode(macro->top(), old_eden_top, size_in_bytes);
756   macro->transform_later(new_eden_top);
757   // Check for needing a GC; compare against heap end
758   Node *needgc_cmp = new CmpPNode(new_eden_top, eden_end);
759   macro->transform_later(needgc_cmp);
760   Node *needgc_bol = new BoolNode(needgc_cmp, BoolTest::ge);
761   macro->transform_later(needgc_bol);
762   IfNode *needgc_iff = new IfNode(contended_region, needgc_bol, PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN);
763   macro->transform_later(needgc_iff);
764 
765   // Plug the failing-heap-space-need-gc test into the slow-path region
766   Node *needgc_true = new IfTrueNode(needgc_iff);
767   macro->transform_later(needgc_true);
768   needgc_ctrl = needgc_true;
769 
770   // No need for a GC.  Setup for the Store-Conditional
771   Node *needgc_false = new IfFalseNode(needgc_iff);
772   macro->transform_later(needgc_false);
773 
774   i_o = macro->prefetch_allocation(i_o, needgc_false, contended_phi_rawmem,
775                                    old_eden_top, new_eden_top, prefetch_lines);
776 
777   Node* fast_oop = old_eden_top;
778 
779   // Store (-conditional) the modified eden top back down.
780   // StorePConditional produces flags for a test PLUS a modified raw
781   // memory state.
782   if (UseTLAB) {
783     Node* store_eden_top =
784       new StorePNode(needgc_false, contended_phi_rawmem, eden_top_adr,
785                      TypeRawPtr::BOTTOM, new_eden_top, MemNode::unordered);
786     macro->transform_later(store_eden_top);
787     fast_oop_ctrl = needgc_false; // No contention, so this is the fast path
788     fast_oop_rawmem = store_eden_top;
789   } else {
790     Node* store_eden_top =
791       new StorePConditionalNode(needgc_false, contended_phi_rawmem, eden_top_adr,
792                                 new_eden_top, fast_oop/*old_eden_top*/);
793     macro->transform_later(store_eden_top);
794     Node *contention_check = new BoolNode(store_eden_top, BoolTest::ne);
795     macro->transform_later(contention_check);
796     store_eden_top = new SCMemProjNode(store_eden_top);
797     macro->transform_later(store_eden_top);
798 
799     // If not using TLABs, check to see if there was contention.
800     IfNode *contention_iff = new IfNode (needgc_false, contention_check, PROB_MIN, COUNT_UNKNOWN);
801     macro->transform_later(contention_iff);
802     Node *contention_true = new IfTrueNode(contention_iff);
803     macro->transform_later(contention_true);
804     // If contention, loopback and try again.
805     contended_region->init_req(contended_loopback_path, contention_true);
806     contended_phi_rawmem->init_req(contended_loopback_path, store_eden_top);
807 
808     // Fast-path succeeded with no contention!
809     Node *contention_false = new IfFalseNode(contention_iff);
810     macro->transform_later(contention_false);
811     fast_oop_ctrl = contention_false;
812 
813     // Bump total allocated bytes for this thread
814     Node* thread = new ThreadLocalNode();
815     macro->transform_later(thread);
816     Node* alloc_bytes_adr = macro->basic_plus_adr(macro->top()/*not oop*/, thread,
817                                                   in_bytes(JavaThread::allocated_bytes_offset()));
818     Node* alloc_bytes = macro->make_load(fast_oop_ctrl, store_eden_top, alloc_bytes_adr,
819                                          0, TypeLong::LONG, T_LONG);
820 #ifdef _LP64
821     Node* alloc_size = size_in_bytes;
822 #else
823     Node* alloc_size = new ConvI2LNode(size_in_bytes);
824     macro->transform_later(alloc_size);
825 #endif
826     Node* new_alloc_bytes = new AddLNode(alloc_bytes, alloc_size);
827     macro->transform_later(new_alloc_bytes);
828     fast_oop_rawmem = macro->make_store(fast_oop_ctrl, store_eden_top, alloc_bytes_adr,
829                                         0, new_alloc_bytes, T_LONG);
830   }
831   return fast_oop;
832 }
833 
834 #define XTOP LP64_ONLY(COMMA phase->top())
835 
836 void BarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const {
837   Node* ctrl = ac->in(TypeFunc::Control);
838   Node* mem = ac->in(TypeFunc::Memory);
839   Node* src = ac->in(ArrayCopyNode::Src);
840   Node* src_offset = ac->in(ArrayCopyNode::SrcPos);
841   Node* dest = ac->in(ArrayCopyNode::Dest);
842   Node* dest_offset = ac->in(ArrayCopyNode::DestPos);
843   Node* length = ac->in(ArrayCopyNode::Length);
844 
845   Node* payload_src = phase->basic_plus_adr(src, src_offset);
846   Node* payload_dst = phase->basic_plus_adr(dest, dest_offset);
847 
848   const char* copyfunc_name = "arraycopy";
849   address     copyfunc_addr = phase->basictype2arraycopy(T_LONG, NULL, NULL, true, copyfunc_name, true);
850 
851   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
852   const TypeFunc* call_type = OptoRuntime::fast_arraycopy_Type();
853 
854   Node* call = phase->make_leaf_call(ctrl, mem, call_type, copyfunc_addr, copyfunc_name, raw_adr_type, payload_src, payload_dst, length XTOP);
855   phase->transform_later(call);
856 
857   phase->replace_node(ac, call);
858 }
859 
860 #undef XTOP