1 /*
   2  * Copyright (c) 1997, 2013, 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 "interp_masm_x86.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "oops/methodData.hpp"
  29 
  30 #ifndef CC_INTERP
  31 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr) {
  32   Label update, next, none;
  33 
  34   verify_oop(obj);
  35 
  36   testptr(obj, obj);
  37   jccb(Assembler::notZero, update);
  38   orptr(mdo_addr, TypeEntries::null_seen);
  39   jmpb(next);
  40 
  41   bind(update);
  42   load_klass(obj, obj);
  43 
  44   xorptr(obj, mdo_addr);
  45   testptr(obj, TypeEntries::type_klass_mask);
  46   jccb(Assembler::zero, next); // klass seen before, nothing to
  47                                // do. The unknown bit may have been
  48                                // set already but no need to check.
  49 
  50   testptr(obj, TypeEntries::type_unknown);
  51   jccb(Assembler::notZero, next); // already unknown. Nothing to do anymore.
  52 
  53   cmpptr(mdo_addr, 0);
  54   jccb(Assembler::equal, none);
  55   cmpptr(mdo_addr, TypeEntries::null_seen);
  56   jccb(Assembler::equal, none);
  57   // There is a chance that the checks above (re-reading profiling
  58   // data from memory) fail if another thread has just set the
  59   // profiling to this obj's klass
  60   xorptr(obj, mdo_addr);
  61   testptr(obj, TypeEntries::type_klass_mask);
  62   jccb(Assembler::zero, next);
  63 
  64   // different than before. Cannot keep accurate profile.
  65   orptr(mdo_addr, TypeEntries::type_unknown);
  66   jmpb(next);
  67 
  68   bind(none);
  69   // first time here. Set profile type.
  70   movptr(mdo_addr, obj);
  71 
  72   bind(next);
  73 }
  74 
  75 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) {
  76   if (!ProfileInterpreter) {
  77     return;
  78   }
  79 
  80   if (MethodData::profile_arguments() || MethodData::profile_return()) {
  81     Label profile_continue;
  82 
  83     test_method_data_pointer(mdp, profile_continue);
  84 
  85     int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
  86 
  87     cmpb(Address(mdp, in_bytes(DataLayout::tag_offset()) - off_to_start), is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag);
  88     jcc(Assembler::notEqual, profile_continue);
  89 
  90     if (MethodData::profile_arguments()) {
  91       Label done;
  92       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
  93       addptr(mdp, off_to_args);
  94 
  95       for (int i = 0; i < TypeProfileArgsLimit; i++) {
  96         if (i > 0 || MethodData::profile_return()) {
  97           // If return value type is profiled we may have no argument to profile
  98           movptr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args));
  99           subl(tmp, i*TypeStackSlotEntries::per_arg_count());
 100           cmpl(tmp, TypeStackSlotEntries::per_arg_count());
 101           jcc(Assembler::less, done);
 102         }
 103         movptr(tmp, Address(callee, Method::const_offset()));
 104         load_unsigned_short(tmp, Address(tmp, ConstMethod::size_of_parameters_offset()));
 105         // stack offset o (zero based) from the start of the argument
 106         // list, for n arguments translates into offset n - o - 1 from
 107         // the end of the argument list
 108         subptr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))-off_to_args));
 109         subl(tmp, 1);
 110         Address arg_addr = argument_address(tmp);
 111         movptr(tmp, arg_addr);
 112 
 113         Address mdo_arg_addr(mdp, in_bytes(TypeEntriesAtCall::argument_type_offset(i))-off_to_args);
 114         profile_obj_type(tmp, mdo_arg_addr);
 115 
 116         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
 117         addptr(mdp, to_add);
 118         off_to_args += to_add;
 119       }
 120 
 121       if (MethodData::profile_return()) {
 122         movptr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args));
 123         subl(tmp, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
 124       }
 125 
 126       bind(done);
 127 
 128       if (MethodData::profile_return()) {
 129         // We're right after the type profile for the last
 130         // argument. tmp is the number of cells left in the
 131         // CallTypeData/VirtualCallTypeData to reach its end. Non null
 132         // if there's a return to profile.
 133         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
 134         shll(tmp, exact_log2(DataLayout::cell_size));
 135         addptr(mdp, tmp);
 136       }
 137       movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), mdp);
 138     } else {
 139       assert(MethodData::profile_return(), "either profile call args or call ret");
 140       update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size()));
 141     }
 142 
 143     // mdp points right after the end of the
 144     // CallTypeData/VirtualCallTypeData, right after the cells for the
 145     // return value type if there's one
 146 
 147     bind(profile_continue);
 148   }
 149 }
 150 
 151 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) {
 152   assert_different_registers(mdp, ret, tmp, _bcp_register);
 153   if (ProfileInterpreter && MethodData::profile_return()) {
 154     Label profile_continue, done;
 155 
 156     test_method_data_pointer(mdp, profile_continue);
 157 
 158     if (MethodData::profile_return_jsr292_only()) {
 159       // If we don't profile all invoke bytecodes we must make sure
 160       // it's a bytecode we indeed profile. We can't go back to the
 161       // begining of the ProfileData we intend to update to check its
 162       // type because we're right after it and we don't known its
 163       // length
 164       Label do_profile;
 165       cmpb(Address(_bcp_register, 0), Bytecodes::_invokedynamic);
 166       jcc(Assembler::equal, do_profile);
 167       cmpb(Address(_bcp_register, 0), Bytecodes::_invokehandle);
 168       jcc(Assembler::equal, do_profile);
 169       get_method(tmp);
 170       cmpb(Address(tmp, Method::intrinsic_id_offset_in_bytes()), vmIntrinsics::_compiledLambdaForm);
 171       jcc(Assembler::notEqual, profile_continue);
 172 
 173       bind(do_profile);
 174     }
 175 
 176     Address mdo_ret_addr(mdp, -in_bytes(ReturnTypeEntry::size()));
 177     mov(tmp, ret);
 178     profile_obj_type(tmp, mdo_ret_addr);
 179 
 180     bind(profile_continue);
 181   }
 182 }
 183 
 184 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2) {
 185   if (ProfileInterpreter && MethodData::profile_parameters()) {
 186     Label profile_continue, done;
 187 
 188     test_method_data_pointer(mdp, profile_continue);
 189 
 190     // Load the offset of the area within the MDO used for
 191     // parameters. If it's negative we're not profiling any parameters
 192     movl(tmp1, Address(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset())));
 193     testl(tmp1, tmp1);
 194     jcc(Assembler::negative, profile_continue);
 195 
 196     // Compute a pointer to the area for parameters from the offset
 197     // and move the pointer to the slot for the last
 198     // parameters. Collect profiling from last parameter down.
 199     // mdo start + parameters offset + array length - 1
 200     addptr(mdp, tmp1);
 201     movptr(tmp1, Address(mdp, ArrayData::array_len_offset()));
 202     decrement(tmp1, TypeStackSlotEntries::per_arg_count());
 203 
 204     Label loop;
 205     bind(loop);
 206 
 207     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
 208     int type_base = in_bytes(ParametersTypeData::type_offset(0));
 209     Address::ScaleFactor per_arg_scale = Address::times(DataLayout::cell_size);
 210     Address arg_off(mdp, tmp1, per_arg_scale, off_base);
 211     Address arg_type(mdp, tmp1, per_arg_scale, type_base);
 212 
 213     // load offset on the stack from the slot for this parameter
 214     movptr(tmp2, arg_off);
 215     negptr(tmp2);
 216     // read the parameter from the local area
 217     movptr(tmp2, Address(_locals_register, tmp2, Interpreter::stackElementScale()));
 218 
 219     // profile the parameter
 220     profile_obj_type(tmp2, arg_type);
 221 
 222     // go to next parameter
 223     decrement(tmp1, TypeStackSlotEntries::per_arg_count());
 224     jcc(Assembler::positive, loop);
 225 
 226     bind(profile_continue);
 227   }
 228 }
 229 #endif