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 "gc/shared/c1/modRefBarrierSetC1.hpp"
27 #include "utilities/macros.hpp"
28 
29 #ifdef ASSERT
30 #define __ lir_generator->lir(__FILE__, __LINE__)->
31 #else
32 #define __ lir_generator->lir()->
33 #endif
34 
35 void ModRefBarrierSetC1::store_at_resolved(LIRGenerator *lir_generator, DecoratorSet decorators, BasicType type,
36                                            LIR_Opr addr, LIRItem& base, LIR_Opr offset, LIR_Opr value,
37                                            CodeEmitInfo* patch_info, CodeEmitInfo* store_emit_info) {
38   bool is_oop = type == T_OBJECT || type == T_ARRAY;
39   bool on_array = (decorators & IN_HEAP_ARRAY) != 0;
40   bool on_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
41 
42   if (is_oop) {
43     pre_barrier(lir_generator, decorators, addr, LIR_OprFact::illegalOpr /* pre_val */, patch_info);
44   }
45 
46   BarrierSetC1::store_at_resolved(lir_generator, decorators, type, addr, base, offset, value, patch_info, store_emit_info);
47 
48   if (is_oop) {
49     bool precise = on_array || on_anonymous;
50     LIR_Opr post_addr = precise ? addr : base.result();
51     post_barrier(lir_generator, decorators, post_addr, value);
52   }
53 }
54 
55 LIR_Opr ModRefBarrierSetC1::atomic_cmpxchg_resolved(LIRGenerator *lir_generator, DecoratorSet decorators, BasicType type,
56                                             LIR_Opr addr, LIRItem& base, LIRItem& offset, LIRItem& cmp_value, LIRItem& new_value) {
57   bool is_oop = type == T_OBJECT || type == T_ARRAY;
58 
59   if (is_oop) {
60     pre_barrier(lir_generator, decorators, addr, LIR_OprFact::illegalOpr /* pre_val */, NULL);
61   }
62 
63   LIR_Opr result = BarrierSetC1::atomic_cmpxchg_resolved(lir_generator, decorators, type, addr, base, offset, cmp_value, new_value);
64 
65   if (is_oop) {
66     post_barrier(lir_generator, decorators, addr, new_value.result());
67   }
68 
69   return result;
70 }
71 
72 LIR_Opr ModRefBarrierSetC1::atomic_xchg_resolved(LIRGenerator* lir_generator, DecoratorSet decorators, BasicType type,
73                                              LIR_Opr addr, LIRItem& base, LIRItem& offset, LIRItem& value) {
74   bool is_oop = type == T_OBJECT || type == T_ARRAY;
75 
76   if (is_oop) {
77     pre_barrier(lir_generator, decorators, addr, LIR_OprFact::illegalOpr /* pre_val */, NULL);
78   }
79 
80   LIR_Opr result = BarrierSetC1::atomic_xchg_resolved(lir_generator, decorators, type, addr, base, offset, value);
81 
82   if (is_oop) {
83     post_barrier(lir_generator, decorators, addr, value.result());
84   }
85 
86   return result;
87 }
88 
89 // This overrides the default to resolve the address into a register, assuming it will be used by a write barrier
90 void ModRefBarrierSetC1::store_at(LIRGenerator *lir_generator, DecoratorSet decorators, BasicType type,
91                                   LIRItem& base, LIR_Opr offset, LIR_Opr value,
92                                   CodeEmitInfo* patch_info, CodeEmitInfo* store_emit_info) {
93   bool is_oop = type == T_OBJECT || type == T_ARRAY;
94   bool needs_patching = (decorators & C1_NEEDS_PATCHING) != 0;
95   LIR_Opr addr = resolve_address(lir_generator, decorators, type, base, offset, !needs_patching && is_oop);
96   store_at_resolved(lir_generator, decorators, type, addr, base, offset, value, patch_info, store_emit_info);
97 }