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