1 /*
   2  * Copyright (c) 2018, 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 "opto/arraycopynode.hpp"
  27 #include "opto/graphKit.hpp"
  28 #include "opto/idealKit.hpp"
  29 #include "opto/narrowptrnode.hpp"
  30 #include "gc/shared/c2/modRefBarrierSetC2.hpp"
  31 #include "utilities/macros.hpp"
  32 
  33 Node* ModRefBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {
  34   DecoratorSet decorators = access.decorators();
  35   GraphKit* kit = access.kit();
  36 
  37   const TypePtr* adr_type = access.addr().type();
  38   Node* adr = access.addr().node();
  39 
  40   bool on_array = (decorators & IN_HEAP_ARRAY) != 0;
  41   bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
  42   bool in_heap = (decorators & IN_HEAP) != 0;
  43   bool use_precise = on_array || anonymous;
  44 
  45   if (!access.is_oop() || (!in_heap && !anonymous)) {
  46     return BarrierSetC2::store_at_resolved(access, val);
  47   }
  48 
  49   uint adr_idx = kit->C->get_alias_index(adr_type);
  50   assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );
  51 
  52   pre_barrier(kit, true /* do_load */, kit->control(), access.base(), adr, adr_idx, val.node(),
  53               static_cast<const TypeOopPtr*>(val.type()), NULL /* pre_val */, access.type());
  54   Node* store = BarrierSetC2::store_at_resolved(access, val);
  55   post_barrier(kit, kit->control(), access.raw_access(), access.base(), adr, adr_idx, val.node(),
  56                access.type(), use_precise);
  57 
  58   return store;
  59 }
  60 
  61 Node* ModRefBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicAccess& access, Node* expected_val,
  62                                                          Node* new_val, const Type* value_type) const {
  63   GraphKit* kit = access.kit();
  64 
  65   if (!access.is_oop()) {
  66     return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
  67   }
  68 
  69   pre_barrier(kit, false /* do_load */,
  70               kit->control(), NULL, NULL, max_juint, NULL, NULL,
  71               expected_val /* pre_val */, T_OBJECT);
  72 
  73   Node* result = BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
  74 
  75   post_barrier(kit, kit->control(), access.raw_access(), access.base(),
  76                access.addr().node(), access.alias_idx(), new_val, T_OBJECT, true);
  77 
  78   return result;
  79 }
  80 
  81 Node* ModRefBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicAccess& access, Node* expected_val,
  82                                                           Node* new_val, const Type* value_type) const {
  83   GraphKit* kit = access.kit();
  84 
  85   if (!access.is_oop()) {
  86     return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
  87   }
  88 
  89   pre_barrier(kit, false /* do_load */,
  90               kit->control(), NULL, NULL, max_juint, NULL, NULL,
  91               expected_val /* pre_val */, T_OBJECT);
  92 
  93   Node* load_store = BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
  94 
  95   // Emit the post barrier only when the actual store happened. This makes sense
  96   // to check only for LS_cmp_* that can fail to set the value.
  97   // LS_cmp_exchange does not produce any branches by default, so there is no
  98   // boolean result to piggyback on. TODO: When we merge CompareAndSwap with
  99   // CompareAndExchange and move branches here, it would make sense to conditionalize
 100   // post_barriers for LS_cmp_exchange as well.
 101   //
 102   // CAS success path is marked more likely since we anticipate this is a performance
 103   // critical path, while CAS failure path can use the penalty for going through unlikely
 104   // path as backoff. Which is still better than doing a store barrier there.
 105   IdealKit ideal(kit);
 106   ideal.if_then(load_store, BoolTest::ne, ideal.ConI(0), PROB_STATIC_FREQUENT); {
 107     kit->sync_kit(ideal);
 108     post_barrier(kit, ideal.ctrl(), access.raw_access(), access.base(),
 109                  access.addr().node(), access.alias_idx(), new_val, T_OBJECT, true);
 110     ideal.sync_kit(kit);
 111   } ideal.end_if();
 112   kit->final_sync(ideal);
 113 
 114   return load_store;
 115 }
 116 
 117 Node* ModRefBarrierSetC2::atomic_xchg_at_resolved(C2AtomicAccess& access, Node* new_val, const Type* value_type) const {
 118   GraphKit* kit = access.kit();
 119 
 120   Node* result = BarrierSetC2::atomic_xchg_at_resolved(access, new_val, value_type);
 121   if (!access.is_oop()) {
 122     return result;
 123   }
 124 
 125   // Don't need to load pre_val. The old value is returned by load_store.
 126   // The pre_barrier can execute after the xchg as long as no safepoint
 127   // gets inserted between them.
 128   pre_barrier(kit, false /* do_load */,
 129               kit->control(), NULL, NULL, max_juint, NULL, NULL,
 130               result /* pre_val */, T_OBJECT);
 131   post_barrier(kit, kit->control(), access.raw_access(), access.base(), access.addr().node(),
 132                access.alias_idx(), new_val, T_OBJECT, true);
 133 
 134   return result;
 135 }