src/cpu/x86/vm/c1_CodeStubs_x86.cpp

Wed, 07 May 2008 08:06:46 -0700

author
rasbold
date
Wed, 07 May 2008 08:06:46 -0700
changeset 580
f3de1255b035
parent 435
a61af66fc99e
child 739
dc7f315e41f7
child 777
37f87013dfd8
permissions
-rw-r--r--

6603011: RFE: Optimize long division
Summary: Transform long division by constant into multiply
Reviewed-by: never, kvn

duke@435 1 /*
duke@435 2 * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_c1_CodeStubs_x86.cpp.incl"
duke@435 27
duke@435 28
duke@435 29 #define __ ce->masm()->
duke@435 30
duke@435 31 float ConversionStub::float_zero = 0.0;
duke@435 32 double ConversionStub::double_zero = 0.0;
duke@435 33
duke@435 34 void ConversionStub::emit_code(LIR_Assembler* ce) {
duke@435 35 __ bind(_entry);
duke@435 36 assert(bytecode() == Bytecodes::_f2i || bytecode() == Bytecodes::_d2i, "other conversions do not require stub");
duke@435 37
duke@435 38
duke@435 39 if (input()->is_single_xmm()) {
duke@435 40 __ comiss(input()->as_xmm_float_reg(),
duke@435 41 ExternalAddress((address)&float_zero));
duke@435 42 } else if (input()->is_double_xmm()) {
duke@435 43 __ comisd(input()->as_xmm_double_reg(),
duke@435 44 ExternalAddress((address)&double_zero));
duke@435 45 } else {
duke@435 46 __ pushl(rax);
duke@435 47 __ ftst();
duke@435 48 __ fnstsw_ax();
duke@435 49 __ sahf();
duke@435 50 __ popl(rax);
duke@435 51 }
duke@435 52
duke@435 53 Label NaN, do_return;
duke@435 54 __ jccb(Assembler::parity, NaN);
duke@435 55 __ jccb(Assembler::below, do_return);
duke@435 56
duke@435 57 // input is > 0 -> return maxInt
duke@435 58 // result register already contains 0x80000000, so subtracting 1 gives 0x7fffffff
duke@435 59 __ decrement(result()->as_register());
duke@435 60 __ jmpb(do_return);
duke@435 61
duke@435 62 // input is NaN -> return 0
duke@435 63 __ bind(NaN);
duke@435 64 __ xorl(result()->as_register(), result()->as_register());
duke@435 65
duke@435 66 __ bind(do_return);
duke@435 67 __ jmp(_continuation);
duke@435 68 }
duke@435 69
duke@435 70 #ifdef TIERED
duke@435 71 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
duke@435 72 __ bind(_entry);
duke@435 73 ce->store_parameter(_bci, 0);
duke@435 74 __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::counter_overflow_id)));
duke@435 75 ce->add_call_info_here(_info);
duke@435 76 ce->verify_oop_map(_info);
duke@435 77
duke@435 78 __ jmp(_continuation);
duke@435 79 }
duke@435 80 #endif // TIERED
duke@435 81
duke@435 82
duke@435 83
duke@435 84 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index,
duke@435 85 bool throw_index_out_of_bounds_exception)
duke@435 86 : _throw_index_out_of_bounds_exception(throw_index_out_of_bounds_exception)
duke@435 87 , _index(index)
duke@435 88 {
duke@435 89 _info = info == NULL ? NULL : new CodeEmitInfo(info);
duke@435 90 }
duke@435 91
duke@435 92
duke@435 93 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
duke@435 94 __ bind(_entry);
duke@435 95 // pass the array index on stack because all registers must be preserved
duke@435 96 if (_index->is_cpu_register()) {
duke@435 97 ce->store_parameter(_index->as_register(), 0);
duke@435 98 } else {
duke@435 99 ce->store_parameter(_index->as_jint(), 0);
duke@435 100 }
duke@435 101 Runtime1::StubID stub_id;
duke@435 102 if (_throw_index_out_of_bounds_exception) {
duke@435 103 stub_id = Runtime1::throw_index_exception_id;
duke@435 104 } else {
duke@435 105 stub_id = Runtime1::throw_range_check_failed_id;
duke@435 106 }
duke@435 107 __ call(RuntimeAddress(Runtime1::entry_for(stub_id)));
duke@435 108 ce->add_call_info_here(_info);
duke@435 109 debug_only(__ should_not_reach_here());
duke@435 110 }
duke@435 111
duke@435 112
duke@435 113 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
duke@435 114 if (_offset != -1) {
duke@435 115 ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
duke@435 116 }
duke@435 117 __ bind(_entry);
duke@435 118 __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::throw_div0_exception_id)));
duke@435 119 ce->add_call_info_here(_info);
duke@435 120 debug_only(__ should_not_reach_here());
duke@435 121 }
duke@435 122
duke@435 123
duke@435 124 // Implementation of NewInstanceStub
duke@435 125
duke@435 126 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) {
duke@435 127 _result = result;
duke@435 128 _klass = klass;
duke@435 129 _klass_reg = klass_reg;
duke@435 130 _info = new CodeEmitInfo(info);
duke@435 131 assert(stub_id == Runtime1::new_instance_id ||
duke@435 132 stub_id == Runtime1::fast_new_instance_id ||
duke@435 133 stub_id == Runtime1::fast_new_instance_init_check_id,
duke@435 134 "need new_instance id");
duke@435 135 _stub_id = stub_id;
duke@435 136 }
duke@435 137
duke@435 138
duke@435 139 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
duke@435 140 assert(__ rsp_offset() == 0, "frame size should be fixed");
duke@435 141 __ bind(_entry);
duke@435 142 __ movl(rdx, _klass_reg->as_register());
duke@435 143 __ call(RuntimeAddress(Runtime1::entry_for(_stub_id)));
duke@435 144 ce->add_call_info_here(_info);
duke@435 145 ce->verify_oop_map(_info);
duke@435 146 assert(_result->as_register() == rax, "result must in rax,");
duke@435 147 __ jmp(_continuation);
duke@435 148 }
duke@435 149
duke@435 150
duke@435 151 // Implementation of NewTypeArrayStub
duke@435 152
duke@435 153 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
duke@435 154 _klass_reg = klass_reg;
duke@435 155 _length = length;
duke@435 156 _result = result;
duke@435 157 _info = new CodeEmitInfo(info);
duke@435 158 }
duke@435 159
duke@435 160
duke@435 161 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
duke@435 162 assert(__ rsp_offset() == 0, "frame size should be fixed");
duke@435 163 __ bind(_entry);
duke@435 164 assert(_length->as_register() == rbx, "length must in rbx,");
duke@435 165 assert(_klass_reg->as_register() == rdx, "klass_reg must in rdx");
duke@435 166 __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_type_array_id)));
duke@435 167 ce->add_call_info_here(_info);
duke@435 168 ce->verify_oop_map(_info);
duke@435 169 assert(_result->as_register() == rax, "result must in rax,");
duke@435 170 __ jmp(_continuation);
duke@435 171 }
duke@435 172
duke@435 173
duke@435 174 // Implementation of NewObjectArrayStub
duke@435 175
duke@435 176 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
duke@435 177 _klass_reg = klass_reg;
duke@435 178 _result = result;
duke@435 179 _length = length;
duke@435 180 _info = new CodeEmitInfo(info);
duke@435 181 }
duke@435 182
duke@435 183
duke@435 184 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
duke@435 185 assert(__ rsp_offset() == 0, "frame size should be fixed");
duke@435 186 __ bind(_entry);
duke@435 187 assert(_length->as_register() == rbx, "length must in rbx,");
duke@435 188 assert(_klass_reg->as_register() == rdx, "klass_reg must in rdx");
duke@435 189 __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_object_array_id)));
duke@435 190 ce->add_call_info_here(_info);
duke@435 191 ce->verify_oop_map(_info);
duke@435 192 assert(_result->as_register() == rax, "result must in rax,");
duke@435 193 __ jmp(_continuation);
duke@435 194 }
duke@435 195
duke@435 196
duke@435 197 // Implementation of MonitorAccessStubs
duke@435 198
duke@435 199 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info)
duke@435 200 : MonitorAccessStub(obj_reg, lock_reg)
duke@435 201 {
duke@435 202 _info = new CodeEmitInfo(info);
duke@435 203 }
duke@435 204
duke@435 205
duke@435 206 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
duke@435 207 assert(__ rsp_offset() == 0, "frame size should be fixed");
duke@435 208 __ bind(_entry);
duke@435 209 ce->store_parameter(_obj_reg->as_register(), 1);
duke@435 210 ce->store_parameter(_lock_reg->as_register(), 0);
duke@435 211 Runtime1::StubID enter_id;
duke@435 212 if (ce->compilation()->has_fpu_code()) {
duke@435 213 enter_id = Runtime1::monitorenter_id;
duke@435 214 } else {
duke@435 215 enter_id = Runtime1::monitorenter_nofpu_id;
duke@435 216 }
duke@435 217 __ call(RuntimeAddress(Runtime1::entry_for(enter_id)));
duke@435 218 ce->add_call_info_here(_info);
duke@435 219 ce->verify_oop_map(_info);
duke@435 220 __ jmp(_continuation);
duke@435 221 }
duke@435 222
duke@435 223
duke@435 224 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
duke@435 225 __ bind(_entry);
duke@435 226 if (_compute_lock) {
duke@435 227 // lock_reg was destroyed by fast unlocking attempt => recompute it
duke@435 228 ce->monitor_address(_monitor_ix, _lock_reg);
duke@435 229 }
duke@435 230 ce->store_parameter(_lock_reg->as_register(), 0);
duke@435 231 // note: non-blocking leaf routine => no call info needed
duke@435 232 Runtime1::StubID exit_id;
duke@435 233 if (ce->compilation()->has_fpu_code()) {
duke@435 234 exit_id = Runtime1::monitorexit_id;
duke@435 235 } else {
duke@435 236 exit_id = Runtime1::monitorexit_nofpu_id;
duke@435 237 }
duke@435 238 __ call(RuntimeAddress(Runtime1::entry_for(exit_id)));
duke@435 239 __ jmp(_continuation);
duke@435 240 }
duke@435 241
duke@435 242
duke@435 243 // Implementation of patching:
duke@435 244 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
duke@435 245 // - Replace original code with a call to the stub
duke@435 246 // At Runtime:
duke@435 247 // - call to stub, jump to runtime
duke@435 248 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
duke@435 249 // - in runtime: after initializing class, restore original code, reexecute instruction
duke@435 250
duke@435 251 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
duke@435 252
duke@435 253 void PatchingStub::align_patch_site(MacroAssembler* masm) {
duke@435 254 // We're patching a 5-7 byte instruction on intel and we need to
duke@435 255 // make sure that we don't see a piece of the instruction. It
duke@435 256 // appears mostly impossible on Intel to simply invalidate other
duke@435 257 // processors caches and since they may do aggressive prefetch it's
duke@435 258 // very hard to make a guess about what code might be in the icache.
duke@435 259 // Force the instruction to be double word aligned so that it
duke@435 260 // doesn't span a cache line.
duke@435 261 masm->align(round_to(NativeGeneralJump::instruction_size, wordSize));
duke@435 262 }
duke@435 263
duke@435 264 void PatchingStub::emit_code(LIR_Assembler* ce) {
duke@435 265 assert(NativeCall::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF, "not enough room for call");
duke@435 266
duke@435 267 Label call_patch;
duke@435 268
duke@435 269 // static field accesses have special semantics while the class
duke@435 270 // initializer is being run so we emit a test which can be used to
duke@435 271 // check that this code is being executed by the initializing
duke@435 272 // thread.
duke@435 273 address being_initialized_entry = __ pc();
duke@435 274 if (CommentedAssembly) {
duke@435 275 __ block_comment(" patch template");
duke@435 276 }
duke@435 277 if (_id == load_klass_id) {
duke@435 278 // produce a copy of the load klass instruction for use by the being initialized case
duke@435 279 address start = __ pc();
duke@435 280 jobject o = NULL;
duke@435 281 __ movoop(_obj, o);
duke@435 282 #ifdef ASSERT
duke@435 283 for (int i = 0; i < _bytes_to_copy; i++) {
duke@435 284 address ptr = (address)(_pc_start + i);
duke@435 285 int a_byte = (*ptr) & 0xFF;
duke@435 286 assert(a_byte == *start++, "should be the same code");
duke@435 287 }
duke@435 288 #endif
duke@435 289 } else {
duke@435 290 // make a copy the code which is going to be patched.
duke@435 291 for ( int i = 0; i < _bytes_to_copy; i++) {
duke@435 292 address ptr = (address)(_pc_start + i);
duke@435 293 int a_byte = (*ptr) & 0xFF;
duke@435 294 __ a_byte (a_byte);
duke@435 295 *ptr = 0x90; // make the site look like a nop
duke@435 296 }
duke@435 297 }
duke@435 298
duke@435 299 address end_of_patch = __ pc();
duke@435 300 int bytes_to_skip = 0;
duke@435 301 if (_id == load_klass_id) {
duke@435 302 int offset = __ offset();
duke@435 303 if (CommentedAssembly) {
duke@435 304 __ block_comment(" being_initialized check");
duke@435 305 }
duke@435 306 assert(_obj != noreg, "must be a valid register");
duke@435 307 Register tmp = rax;
duke@435 308 if (_obj == tmp) tmp = rbx;
duke@435 309 __ pushl(tmp);
duke@435 310 __ get_thread(tmp);
duke@435 311 __ cmpl(tmp, Address(_obj, instanceKlass::init_thread_offset_in_bytes() + sizeof(klassOopDesc)));
duke@435 312 __ popl(tmp);
duke@435 313 __ jcc(Assembler::notEqual, call_patch);
duke@435 314
duke@435 315 // access_field patches may execute the patched code before it's
duke@435 316 // copied back into place so we need to jump back into the main
duke@435 317 // code of the nmethod to continue execution.
duke@435 318 __ jmp(_patch_site_continuation);
duke@435 319
duke@435 320 // make sure this extra code gets skipped
duke@435 321 bytes_to_skip += __ offset() - offset;
duke@435 322 }
duke@435 323 if (CommentedAssembly) {
duke@435 324 __ block_comment("patch data encoded as movl");
duke@435 325 }
duke@435 326 // Now emit the patch record telling the runtime how to find the
duke@435 327 // pieces of the patch. We only need 3 bytes but for readability of
duke@435 328 // the disassembly we make the data look like a movl reg, imm32,
duke@435 329 // which requires 5 bytes
duke@435 330 int sizeof_patch_record = 5;
duke@435 331 bytes_to_skip += sizeof_patch_record;
duke@435 332
duke@435 333 // emit the offsets needed to find the code to patch
duke@435 334 int being_initialized_entry_offset = __ pc() - being_initialized_entry + sizeof_patch_record;
duke@435 335
duke@435 336 __ a_byte(0xB8);
duke@435 337 __ a_byte(0);
duke@435 338 __ a_byte(being_initialized_entry_offset);
duke@435 339 __ a_byte(bytes_to_skip);
duke@435 340 __ a_byte(_bytes_to_copy);
duke@435 341 address patch_info_pc = __ pc();
duke@435 342 assert(patch_info_pc - end_of_patch == bytes_to_skip, "incorrect patch info");
duke@435 343
duke@435 344 address entry = __ pc();
duke@435 345 NativeGeneralJump::insert_unconditional((address)_pc_start, entry);
duke@435 346 address target = NULL;
duke@435 347 switch (_id) {
duke@435 348 case access_field_id: target = Runtime1::entry_for(Runtime1::access_field_patching_id); break;
duke@435 349 case load_klass_id: target = Runtime1::entry_for(Runtime1::load_klass_patching_id); break;
duke@435 350 default: ShouldNotReachHere();
duke@435 351 }
duke@435 352 __ bind(call_patch);
duke@435 353
duke@435 354 if (CommentedAssembly) {
duke@435 355 __ block_comment("patch entry point");
duke@435 356 }
duke@435 357 __ call(RuntimeAddress(target));
duke@435 358 assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change");
duke@435 359 ce->add_call_info_here(_info);
duke@435 360 int jmp_off = __ offset();
duke@435 361 __ jmp(_patch_site_entry);
duke@435 362 // Add enough nops so deoptimization can overwrite the jmp above with a call
duke@435 363 // and not destroy the world.
duke@435 364 for (int j = __ offset() ; j < jmp_off + 5 ; j++ ) {
duke@435 365 __ nop();
duke@435 366 }
duke@435 367 if (_id == load_klass_id) {
duke@435 368 CodeSection* cs = __ code_section();
duke@435 369 RelocIterator iter(cs, (address)_pc_start, (address)(_pc_start + 1));
duke@435 370 relocInfo::change_reloc_info_for_address(&iter, (address) _pc_start, relocInfo::oop_type, relocInfo::none);
duke@435 371 }
duke@435 372 }
duke@435 373
duke@435 374
duke@435 375 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
duke@435 376 ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
duke@435 377 __ bind(_entry);
duke@435 378 __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id)));
duke@435 379 ce->add_call_info_here(_info);
duke@435 380 debug_only(__ should_not_reach_here());
duke@435 381 }
duke@435 382
duke@435 383
duke@435 384 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
duke@435 385 assert(__ rsp_offset() == 0, "frame size should be fixed");
duke@435 386
duke@435 387 __ bind(_entry);
duke@435 388 // pass the object on stack because all registers must be preserved
duke@435 389 if (_obj->is_cpu_register()) {
duke@435 390 ce->store_parameter(_obj->as_register(), 0);
duke@435 391 }
duke@435 392 __ call(RuntimeAddress(Runtime1::entry_for(_stub)));
duke@435 393 ce->add_call_info_here(_info);
duke@435 394 debug_only(__ should_not_reach_here());
duke@435 395 }
duke@435 396
duke@435 397
duke@435 398 ArrayStoreExceptionStub::ArrayStoreExceptionStub(CodeEmitInfo* info):
duke@435 399 _info(info) {
duke@435 400 }
duke@435 401
duke@435 402
duke@435 403 void ArrayStoreExceptionStub::emit_code(LIR_Assembler* ce) {
duke@435 404 assert(__ rsp_offset() == 0, "frame size should be fixed");
duke@435 405 __ bind(_entry);
duke@435 406 __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::throw_array_store_exception_id)));
duke@435 407 ce->add_call_info_here(_info);
duke@435 408 debug_only(__ should_not_reach_here());
duke@435 409 }
duke@435 410
duke@435 411
duke@435 412 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
duke@435 413 //---------------slow case: call to native-----------------
duke@435 414 __ bind(_entry);
duke@435 415 // Figure out where the args should go
duke@435 416 // This should really convert the IntrinsicID to the methodOop and signature
duke@435 417 // but I don't know how to do that.
duke@435 418 //
duke@435 419 VMRegPair args[5];
duke@435 420 BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
duke@435 421 SharedRuntime::java_calling_convention(signature, args, 5, true);
duke@435 422
duke@435 423 // push parameters
duke@435 424 // (src, src_pos, dest, destPos, length)
duke@435 425 Register r[5];
duke@435 426 r[0] = src()->as_register();
duke@435 427 r[1] = src_pos()->as_register();
duke@435 428 r[2] = dst()->as_register();
duke@435 429 r[3] = dst_pos()->as_register();
duke@435 430 r[4] = length()->as_register();
duke@435 431
duke@435 432 // next registers will get stored on the stack
duke@435 433 for (int i = 0; i < 5 ; i++ ) {
duke@435 434 VMReg r_1 = args[i].first();
duke@435 435 if (r_1->is_stack()) {
duke@435 436 int st_off = r_1->reg2stack() * wordSize;
duke@435 437 __ movl (Address(rsp, st_off), r[i]);
duke@435 438 } else {
duke@435 439 assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
duke@435 440 }
duke@435 441 }
duke@435 442
duke@435 443 ce->align_call(lir_static_call);
duke@435 444
duke@435 445 ce->emit_static_call_stub();
duke@435 446 AddressLiteral resolve(SharedRuntime::get_resolve_static_call_stub(),
duke@435 447 relocInfo::static_call_type);
duke@435 448 __ call(resolve);
duke@435 449 ce->add_call_info_here(info());
duke@435 450
duke@435 451 #ifndef PRODUCT
duke@435 452 __ increment(ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
duke@435 453 #endif
duke@435 454
duke@435 455 __ jmp(_continuation);
duke@435 456 }
duke@435 457
duke@435 458
duke@435 459 #undef __

mercurial