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 "c1/c1_LIRGenerator.hpp"
 27 #include "c1/c1_CodeStubs.hpp"
 28 #include "gc/g1/c1/g1BarrierSetC1.hpp"
 29 #include "gc/g1/g1BarrierSet.hpp"
 30 #include "gc/g1/g1BarrierSetAssembler.hpp"
 31 #include "gc/g1/g1ThreadLocalData.hpp"
 32 #include "gc/g1/heapRegion.hpp"
 33 #include "utilities/macros.hpp"
 34 
 35 #ifdef ASSERT
 36 #define __ lir_generator->lir(__FILE__, __LINE__)->
 37 #else
 38 #define __ lir_generator->lir()->
 39 #endif
 40 
 41 void G1PreBarrierStub::emit_code(LIR_Assembler* ce) {
 42   G1BarrierSetAssembler* bs = (G1BarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
 43   bs->gen_g1_pre_barrier_stub(ce, this);
 44 }
 45 
 46 void G1PostBarrierStub::emit_code(LIR_Assembler* ce) {
 47   G1BarrierSetAssembler* bs = (G1BarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
 48   bs->gen_g1_post_barrier_stub(ce, this);
 49 }
 50 
 51 void G1BarrierSetC1::pre_barrier(LIRGenerator *lir_generator, DecoratorSet decorators,
 52                                  LIR_Opr addr_opr, LIR_Opr pre_val, CodeEmitInfo* info) {
 53   bool in_heap = (decorators & IN_HEAP) != 0;
 54   bool in_conc_root = (decorators & IN_CONCURRENT_ROOT) != 0;
 55   if (!in_heap && !in_conc_root) {
 56     return;
 57   }
 58 
 59   // First we test whether marking is in progress.
 60   BasicType flag_type;
 61   bool patch = (decorators & C1_NEEDS_PATCHING) != 0;
 62   bool do_load = pre_val == LIR_OprFact::illegalOpr;
 63   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 64     flag_type = T_INT;
 65   } else {
 66     guarantee(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1,
 67               "Assumption");
 68     // Use unsigned type T_BOOLEAN here rather than signed T_BYTE since some platforms, eg. ARM,
 69     // need to use unsigned instructions to use the large offset to load the satb_mark_queue.
 70     flag_type = T_BOOLEAN;
 71   }
 72   LIR_Opr thrd = lir_generator->getThreadPointer();
 73   LIR_Address* mark_active_flag_addr =
 74     new LIR_Address(thrd,
 75                     in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()),
 76                     flag_type);
 77   // Read the marking-in-progress flag.
 78   LIR_Opr flag_val = lir_generator->new_register(T_INT);
 79   __ load(mark_active_flag_addr, flag_val);
 80   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
 81 
 82   LIR_PatchCode pre_val_patch_code = lir_patch_none;
 83 
 84   CodeStub* slow;
 85 
 86   if (do_load) {
 87     assert(pre_val == LIR_OprFact::illegalOpr, "sanity");
 88     assert(addr_opr != LIR_OprFact::illegalOpr, "sanity");
 89 
 90     if (patch)
 91       pre_val_patch_code = lir_patch_normal;
 92 
 93     pre_val = lir_generator->new_register(T_OBJECT);
 94 
 95     if (!addr_opr->is_address()) {
 96       assert(addr_opr->is_register(), "must be");
 97       addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));
 98     }
 99     slow = new G1PreBarrierStub(addr_opr, pre_val, pre_val_patch_code, info);
100   } else {
101     assert(addr_opr == LIR_OprFact::illegalOpr, "sanity");
102     assert(pre_val->is_register(), "must be");
103     assert(pre_val->type() == T_OBJECT, "must be an object");
104     assert(info == NULL, "sanity");
105 
106     slow = new G1PreBarrierStub(pre_val);
107   }
108 
109   __ branch(lir_cond_notEqual, T_INT, slow);
110   __ branch_destination(slow->continuation());
111 }
112 
113 void G1BarrierSetC1::post_barrier(LIRGenerator *lir_generator, DecoratorSet decorators,
114                                   LIR_OprDesc* addr, LIR_OprDesc* new_val) {
115   bool in_heap = (decorators & IN_HEAP) != 0;
116   if (!in_heap) {
117     return;
118   }
119 
120   // If the "new_val" is a constant NULL, no barrier is necessary.
121   if (new_val->is_constant() &&
122       new_val->as_constant_ptr()->as_jobject() == NULL) return;
123 
124   if (!new_val->is_register()) {
125     LIR_Opr new_val_reg = lir_generator->new_register(T_OBJECT);
126     if (new_val->is_constant()) {
127       __ move(new_val, new_val_reg);
128     } else {
129       __ leal(new_val, new_val_reg);
130     }
131     new_val = new_val_reg;
132   }
133   assert(new_val->is_register(), "must be a register at this point");
134 
135   if (addr->is_address()) {
136     LIR_Address* address = addr->as_address_ptr();
137     LIR_Opr ptr = lir_generator->new_pointer_register();
138     if (!address->index()->is_valid() && address->disp() == 0) {
139       __ move(address->base(), ptr);
140     } else {
141       assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
142       __ leal(addr, ptr);
143     }
144     addr = ptr;
145   }
146   assert(addr->is_register(), "must be a register at this point");
147 
148   LIR_Opr xor_res = lir_generator->new_pointer_register();
149   LIR_Opr xor_shift_res = lir_generator->new_pointer_register();
150   if (TwoOperandLIRForm) {
151     __ move(addr, xor_res);
152     __ logical_xor(xor_res, new_val, xor_res);
153     __ move(xor_res, xor_shift_res);
154     __ unsigned_shift_right(xor_shift_res,
155                             LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),
156                             xor_shift_res,
157                             LIR_OprDesc::illegalOpr());
158   } else {
159     __ logical_xor(addr, new_val, xor_res);
160     __ unsigned_shift_right(xor_res,
161                             LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),
162                             xor_shift_res,
163                             LIR_OprDesc::illegalOpr());
164   }
165 
166   if (!new_val->is_register()) {
167     LIR_Opr new_val_reg = lir_generator->new_register(T_OBJECT);
168     __ leal(new_val, new_val_reg);
169     new_val = new_val_reg;
170   }
171   assert(new_val->is_register(), "must be a register at this point");
172 
173   __ cmp(lir_cond_notEqual, xor_shift_res, LIR_OprFact::intptrConst(NULL_WORD));
174 
175   CodeStub* slow = new G1PostBarrierStub(addr, new_val);
176   __ branch(lir_cond_notEqual, LP64_ONLY(T_LONG) NOT_LP64(T_INT), slow);
177   __ branch_destination(slow->continuation());
178 }
179 
180 void G1BarrierSetC1::load_at_resolved(LIRGenerator *lir_generator, DecoratorSet decorators, BasicType type,
181                                       LIR_Opr addr, LIRItem& base, LIR_Opr offset, LIR_Opr result,
182                                       CodeEmitInfo* patch_info, CodeEmitInfo* load_emit_info) {
183   bool is_oop = type == T_OBJECT || type == T_ARRAY;
184   bool is_weak = (decorators & ON_WEAK_OOP_REF) != 0;
185   bool is_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
186   bool is_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
187 
188   BarrierSetC1::load_at_resolved(lir_generator, decorators, type, addr, base, offset, result, patch_info, load_emit_info);
189 
190   if (is_oop && (is_weak || is_phantom || is_anonymous)) {
191     // Register the value in the referent field with the pre-barrier
192     LabelObj *Lcont_anonymous;
193     if (is_anonymous) {
194       Lcont_anonymous = new LabelObj();
195       generate_referent_check(lir_generator, base, offset, Lcont_anonymous);
196     }
197     pre_barrier(lir_generator, decorators, LIR_OprFact::illegalOpr /* addr_opr */, result /* pre_val */, patch_info /* info */);
198     if (is_anonymous) {
199       __ branch_destination(Lcont_anonymous->label());
200     }
201   }
202 }
203 
204 class C1G1PreBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
205   virtual OopMapSet* generate_code(StubAssembler* sasm) {
206     G1BarrierSetAssembler* bs = (G1BarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
207     bs->generate_c1_pre_barrier_runtime_stub(sasm);
208     return NULL;
209   }
210 };
211 
212 class C1G1PostBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
213   virtual OopMapSet* generate_code(StubAssembler* sasm) {
214     G1BarrierSetAssembler* bs = (G1BarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
215     bs->generate_c1_post_barrier_runtime_stub(sasm);
216     return NULL;
217   }
218 };
219 
220 void G1BarrierSetC1::generate_c1_runtime_stubs(BufferBlob* buffer_blob) {
221   C1G1PreBarrierCodeGenClosure pre_code_gen_cl;
222   C1G1PostBarrierCodeGenClosure post_code_gen_cl;
223   _pre_barrier_c1_runtime_code_blob = Runtime1::generate_blob(buffer_blob, -1, "g1_pre_barrier_slow", false, &pre_code_gen_cl);
224   _post_barrier_c1_runtime_code_blob = Runtime1::generate_blob(buffer_blob, -1, "g1_post_barrier_slow", false, &post_code_gen_cl);
225 }