Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(494)

Side by Side Diff: src/ia32/full-codegen-ia32.cc

Issue 6148007: Speed up FastAsciiArrayJoin on ia32 by improving hand-written assembly code. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 3333 matching lines...) Expand 10 before | Expand all | Expand 10 after
3344 } 3344 }
3345 3345
3346 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset)); 3346 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
3347 __ IndexFromHash(eax, eax); 3347 __ IndexFromHash(eax, eax);
3348 3348
3349 context()->Plug(eax); 3349 context()->Plug(eax);
3350 } 3350 }
3351 3351
3352 3352
3353 void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) { 3353 void FullCodeGenerator::EmitFastAsciiArrayJoin(ZoneList<Expression*>* args) {
3354 Label bailout; 3354 Label bailout, done, one_char_separator, long_separator,
3355 Label done; 3355 non_trivial_array, not_size_one_array, loop, loop_condition,
3356 loop_1, loop_1_condition, loop_2, loop_2_entry, loop_3, loop_3_entry;
3356 3357
3357 ASSERT(args->length() == 2); 3358 ASSERT(args->length() == 2);
3358 // We will leave the separator on the stack until the end of the function. 3359 // We will leave the separator on the stack until the end of the function.
3359 VisitForStackValue(args->at(1)); 3360 VisitForStackValue(args->at(1));
3360 // Load this to eax (= array) 3361 // Load this to eax (= array)
3361 VisitForAccumulatorValue(args->at(0)); 3362 VisitForAccumulatorValue(args->at(0));
3362
3363 // All aliases of the same register have disjoint lifetimes. 3363 // All aliases of the same register have disjoint lifetimes.
3364 Register array = eax; 3364 Register array = eax;
3365 Register result_pos = no_reg; 3365 Register elements = no_reg; // Will be eax.
3366 3366
3367 Register index = edi; 3367 Register index = edx;
3368 3368
3369 Register current_string_length = ecx; // Will be ecx when live. 3369 Register string_length = ecx;
3370 3370
3371 Register current_string = edx; 3371 Register string = esi;
3372 3372
3373 Register scratch = ebx; 3373 Register scratch = ebx;
3374 3374
3375 Register scratch_2 = esi; 3375 Register array_length = edi;
3376 Register new_padding_chars = scratch_2; 3376 Register result_pos = no_reg; // Will be edi.
3377 3377
3378 Operand separator = Operand(esp, 4 * kPointerSize); // Already pushed. 3378 // Separator operand is already pushed.
3379 Operand elements = Operand(esp, 3 * kPointerSize); 3379 Operand separator_operand = Operand(esp, 2 * kPointerSize);
3380 Operand result = Operand(esp, 2 * kPointerSize); 3380 Operand result_operand = Operand(esp, 1 * kPointerSize);
3381 Operand padding_chars = Operand(esp, 1 * kPointerSize); 3381 Operand array_length_operand = Operand(esp, 0);
3382 Operand array_length = Operand(esp, 0); 3382 __ sub(Operand(esp), Immediate(2 * kPointerSize));
3383 __ sub(Operand(esp), Immediate(4 * kPointerSize)); 3383 __ cld();
3384 3384 // Check that the array is a JSArray
3385
3386 // Check that eax is a JSArray
3387 __ test(array, Immediate(kSmiTagMask)); 3385 __ test(array, Immediate(kSmiTagMask));
3388 __ j(zero, &bailout); 3386 __ j(zero, &bailout);
3389 __ CmpObjectType(array, JS_ARRAY_TYPE, scratch); 3387 __ CmpObjectType(array, JS_ARRAY_TYPE, scratch);
3390 __ j(not_equal, &bailout); 3388 __ j(not_equal, &bailout);
3391 3389
3392 // Check that the array has fast elements. 3390 // Check that the array has fast elements.
3393 __ test_b(FieldOperand(scratch, Map::kBitField2Offset), 3391 __ test_b(FieldOperand(scratch, Map::kBitField2Offset),
3394 1 << Map::kHasFastElements); 3392 1 << Map::kHasFastElements);
3395 __ j(zero, &bailout); 3393 __ j(zero, &bailout);
3396 3394
3397 // If the array is empty, return the empty string. 3395 // If the array has length zero, return the empty string.
3398 __ mov(scratch, FieldOperand(array, JSArray::kLengthOffset)); 3396 __ mov(array_length, FieldOperand(array, JSArray::kLengthOffset));
3399 __ sar(scratch, 1); 3397 __ sar(array_length, 1);
3400 Label non_trivial; 3398 __ j(not_zero, &non_trivial_array);
3401 __ j(not_zero, &non_trivial); 3399 __ mov(result_operand, Factory::empty_string());
3402 __ mov(result, Factory::empty_string()); 3400 __ jmp(&done);
3403 __ jmp(&done); 3401
3404 3402 // Save the array length.
3405 __ bind(&non_trivial); 3403 __ bind(&non_trivial_array);
3406 __ mov(array_length, scratch); 3404 __ mov(array_length_operand, array_length);
3407 3405
3408 __ mov(scratch, FieldOperand(array, JSArray::kElementsOffset)); 3406 // Save the FixedArray containing array's elements.
3409 __ mov(elements, scratch);
3410
3411 // End of array's live range. 3407 // End of array's live range.
3412 result_pos = array; 3408 elements = array;
3409 __ mov(elements, FieldOperand(array, JSArray::kElementsOffset));
3413 array = no_reg; 3410 array = no_reg;
3414 3411
3415 3412
3416 // Check that the separator is a flat ascii string. 3413 // Check that all array elements are sequential ASCII strings, and
3417 __ mov(current_string, separator); 3414 // accumulate the sum of their lengths, as a smi-encoded value.
3418 __ test(current_string, Immediate(kSmiTagMask)); 3415 __ Set(index, Immediate(0));
3416 __ Set(string_length, Immediate(0));
3417 // Loop condition: while (index < length).
3418 // Live loop registers: index, array_length, string,
3419 // scratch, string_length, elements.
3420 __ jmp(&loop_condition);
3421 __ bind(&loop);
3422 __ cmp(index, Operand(array_length));
3423 __ j(greater_equal, &done);
3424
3425 __ mov(string, FieldOperand(elements, index,
3426 times_pointer_size,
3427 FixedArray::kHeaderSize));
3428 __ test(string, Immediate(kSmiTagMask));
3419 __ j(zero, &bailout); 3429 __ j(zero, &bailout);
3420 __ mov(scratch, FieldOperand(current_string, HeapObject::kMapOffset)); 3430 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3421 __ mov_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset)); 3431 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3422 __ and_(scratch, Immediate( 3432 __ and_(scratch, Immediate(
3423 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask)); 3433 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
3424 __ cmp(scratch, kStringTag | kAsciiStringTag | kSeqStringTag); 3434 __ cmp(scratch, kStringTag | kAsciiStringTag | kSeqStringTag);
3425 __ j(not_equal, &bailout); 3435 __ j(not_equal, &bailout);
3426 // If the separator is the empty string, replace it with NULL. 3436 __ add(string_length,
3427 // The test for NULL is quicker than the empty string test, in a loop. 3437 FieldOperand(string, SeqAsciiString::kLengthOffset));
3428 __ cmp(FieldOperand(current_string, SeqAsciiString::kLengthOffset), 3438 __ j(overflow, &bailout);
3429 Immediate(0)); 3439 __ add(Operand(index), Immediate(1));
3430 Label separator_checked; 3440 __ bind(&loop_condition);
3431 __ j(not_zero, &separator_checked); 3441 __ cmp(index, Operand(array_length));
3432 __ mov(separator, Immediate(0)); 3442 __ j(less, &loop);
3433 __ bind(&separator_checked); 3443
3434 3444 // If array_length is 1, return elements[0], a string.
3435 // Check that elements[0] is a flat ascii string, and copy it in new space. 3445 __ cmp(array_length, 1);
3436 __ mov(scratch, elements); 3446 __ j(not_equal, &not_size_one_array);
3437 __ mov(current_string, FieldOperand(scratch, FixedArray::kHeaderSize)); 3447 __ mov(scratch, FieldOperand(elements, FixedArray::kHeaderSize));
3438 __ test(current_string, Immediate(kSmiTagMask)); 3448 __ mov(result_operand, scratch);
3449 __ jmp(&done);
3450
3451 __ bind(&not_size_one_array);
3452
3453 // End of array_length live range.
3454 result_pos = array_length;
3455 array_length = no_reg;
3456
3457 // Live registers:
3458 // string_length: Sum of string lengths, as a smi.
3459 // elements: FixedArray of strings.
3460
3461 // Check that the separator is a flat ASCII string.
3462 __ mov(string, separator_operand);
3463 __ test(string, Immediate(kSmiTagMask));
3439 __ j(zero, &bailout); 3464 __ j(zero, &bailout);
3440 __ mov(scratch, FieldOperand(current_string, HeapObject::kMapOffset)); 3465 __ mov(scratch, FieldOperand(string, HeapObject::kMapOffset));
3441 __ mov_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset)); 3466 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3442 __ and_(scratch, Immediate( 3467 __ and_(scratch, Immediate(
3443 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask)); 3468 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask));
3444 __ cmp(scratch, kStringTag | kAsciiStringTag | kSeqStringTag); 3469 __ cmp(scratch, kStringTag | kAsciiStringTag | kSeqStringTag);
3445 __ j(not_equal, &bailout); 3470 __ j(not_equal, &bailout);
3446 3471
3447 // Allocate space to copy it. Round up the size to the alignment granularity. 3472 // Add (separator length times array_length) - separator length
3448 __ mov(current_string_length, 3473 // to string_length.
3449 FieldOperand(current_string, String::kLengthOffset)); 3474 __ mov(scratch, separator_operand);
3450 __ shr(current_string_length, 1); 3475 __ mov(scratch, FieldOperand(scratch, SeqAsciiString::kLengthOffset));
3451 3476 __ sub(string_length, Operand(scratch)); // May be negative, temporarily.
3477 __ imul(scratch, array_length_operand);
3478 __ j(overflow, &bailout);
3479 __ add(string_length, Operand(scratch));
3480 __ j(overflow, &bailout);
3481
3482 __ shr(string_length, 1);
3452 // Live registers and stack values: 3483 // Live registers and stack values:
3453 // current_string_length: length of elements[0]. 3484 // string_length
3454 3485 // elements
3455 // New string result in new space = elements[0] 3486 __ AllocateAsciiString(result_pos, string_length, scratch,
3456 __ AllocateAsciiString(result_pos, current_string_length, scratch_2, 3487 index, string, &bailout);
3457 index, no_reg, &bailout); 3488 __ mov(result_operand, result_pos);
3458 __ mov(result, result_pos); 3489 __ lea(result_pos, FieldOperand(result_pos, SeqAsciiString::kHeaderSize));
3459 3490
3460 // Adjust current_string_length to include padding bytes at end of string. 3491
3461 // Keep track of the number of padding bytes. 3492 __ mov(string, separator_operand);
3462 __ mov(new_padding_chars, current_string_length); 3493 __ cmp(FieldOperand(string, SeqAsciiString::kLengthOffset),
3463 __ add(Operand(current_string_length), Immediate(kObjectAlignmentMask)); 3494 Immediate(Smi::FromInt(1)));
3464 __ and_(Operand(current_string_length), Immediate(~kObjectAlignmentMask)); 3495 __ j(equal, &one_char_separator);
3465 __ sub(new_padding_chars, Operand(current_string_length)); 3496 __ j(greater, &long_separator);
3466 __ neg(new_padding_chars); 3497
3467 __ mov(padding_chars, new_padding_chars); 3498
3468 3499 // Empty separator case
3469 Label copy_loop_1_done; 3500 __ mov(index, Immediate(0));
3470 Label copy_loop_1; 3501 __ jmp(&loop_1_condition);
3471 __ test(current_string_length, Operand(current_string_length)); 3502 // Loop condition: while (index < length).
3472 __ j(zero, &copy_loop_1_done); 3503 __ bind(&loop_1);
3473 __ bind(&copy_loop_1); 3504 // Each iteration of the loop concatenates one string to the result.
3474 __ sub(Operand(current_string_length), Immediate(kPointerSize)); 3505 // Live values in registers:
3475 __ mov(scratch, FieldOperand(current_string, current_string_length, 3506 // index: which element of the elements array we are adding to the result.
3476 times_1, SeqAsciiString::kHeaderSize)); 3507 // result_pos: the position to which we are currently copying characters.
3477 __ mov(FieldOperand(result_pos, current_string_length, 3508 // elements: the FixedArray of strings we are joining.
3478 times_1, SeqAsciiString::kHeaderSize), 3509
3479 scratch); 3510 // Get string = array[index].
3480 __ j(not_zero, &copy_loop_1); 3511 __ mov(string, FieldOperand(elements, index,
3481 __ bind(&copy_loop_1_done); 3512 times_pointer_size,
3482 3513 FixedArray::kHeaderSize));
3483 __ mov(index, Immediate(1)); 3514 __ mov(string_length,
3484 // Loop condition: while (index < length). 3515 FieldOperand(string, String::kLengthOffset));
3485 Label loop; 3516 __ shr(string_length, 1);
3486 __ bind(&loop); 3517 __ lea(string,
3487 __ cmp(index, array_length); 3518 FieldOperand(string, SeqAsciiString::kHeaderSize));
3488 __ j(greater_equal, &done); 3519 __ CopyBytes(string, result_pos, string_length, scratch);
3489 3520 __ add(Operand(index), Immediate(1));
3490 // If the separator is the empty string, signalled by NULL, skip it. 3521 __ bind(&loop_1_condition);
3491 Label separator_done; 3522 __ cmp(index, array_length_operand);
3492 __ mov(current_string, separator); 3523 __ j(less, &loop_1); // End while (index < length).
3493 __ test(current_string, Operand(current_string)); 3524 __ jmp(&done);
3494 __ j(zero, &separator_done); 3525
3495 3526
3496 // Append separator to result. It is known to be a flat ascii string. 3527
3497 __ AppendStringToTopOfNewSpace(current_string, current_string_length, 3528 // One-character separator case
3498 result_pos, scratch, scratch_2, result, 3529 __ bind(&one_char_separator);
3499 padding_chars, &bailout); 3530 // Replace separator with its ascii character value.
3500 __ bind(&separator_done); 3531 __ mov_b(scratch, FieldOperand(string, SeqAsciiString::kHeaderSize));
3501 3532 __ mov_b(separator_operand, scratch);
3502 // Add next element of array to the end of the result. 3533
3503 // Get current_string = array[index]. 3534 __ Set(index, Immediate(0));
3504 __ mov(scratch, elements); 3535 // Jump into the loop after the code that copies the separator, so the first
3505 __ mov(current_string, FieldOperand(scratch, index, 3536 // element is not preceded by a separator
3506 times_pointer_size, 3537 __ jmp(&loop_2_entry);
3507 FixedArray::kHeaderSize)); 3538 // Loop condition: while (index < length).
3508 // If current != flat ascii string drop result, return undefined. 3539 __ bind(&loop_2);
3509 __ test(current_string, Immediate(kSmiTagMask)); 3540 // Each iteration of the loop concatenates one string to the result.
3510 __ j(zero, &bailout); 3541 // Live values in registers:
3511 __ mov(scratch, FieldOperand(current_string, HeapObject::kMapOffset)); 3542 // index: which element of the elements array we are adding to the result.
3512 __ mov_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset)); 3543 // result_pos: the position to which we are currently copying characters.
3513 __ and_(scratch, Immediate( 3544
3514 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask)); 3545 // Copy the separator character to the result.
3515 __ cmp(scratch, kStringTag | kAsciiStringTag | kSeqStringTag); 3546 __ mov_b(scratch, separator_operand);
3516 __ j(not_equal, &bailout); 3547 __ mov_b(Operand(result_pos, 0), scratch);
3517 3548 __ inc(result_pos);
3518 // Append current to the result. 3549
3519 __ AppendStringToTopOfNewSpace(current_string, current_string_length, 3550 __ bind(&loop_2_entry);
3520 result_pos, scratch, scratch_2, result, 3551 // Get string = array[index].
3521 padding_chars, &bailout); 3552 __ mov(string, FieldOperand(elements, index,
3522 __ add(Operand(index), Immediate(1)); 3553 times_pointer_size,
3523 __ jmp(&loop); // End while (index < length). 3554 FixedArray::kHeaderSize));
3555 __ mov(string_length,
3556 FieldOperand(string, String::kLengthOffset));
3557 __ shr(string_length, 1);
3558 __ lea(string,
3559 FieldOperand(string, SeqAsciiString::kHeaderSize));
3560 __ CopyBytes(string, result_pos, string_length, scratch);
3561 __ add(Operand(index), Immediate(1));
3562
3563 __ cmp(index, array_length_operand);
3564 __ j(less, &loop_2); // End while (index < length).
3565 __ jmp(&done);
3566
3567
3568 // Long separator case (separator is more than one character).
3569 __ bind(&long_separator);
3570
3571 __ Set(index, Immediate(0));
3572 // Jump into the loop after the code that copies the separator, so the first
3573 // element is not preceded by a separator
3574 __ jmp(&loop_3_entry);
3575 // Loop condition: while (index < length).
3576 __ bind(&loop_3);
3577 // Each iteration of the loop concatenates one string to the result.
3578 // Live values in registers:
3579 // index: which element of the elements array we are adding to the result.
3580 // result_pos: the position to which we are currently copying characters.
3581
3582 // Copy the separator to the result.
3583 __ mov(string, separator_operand);
3584 __ mov(string_length,
3585 FieldOperand(string, String::kLengthOffset));
3586 __ shr(string_length, 1);
3587 __ lea(string,
3588 FieldOperand(string, SeqAsciiString::kHeaderSize));
3589 __ CopyBytes(string, result_pos, string_length, scratch);
3590
3591 __ bind(&loop_3_entry);
3592 // Get string = array[index].
3593 __ mov(string, FieldOperand(elements, index,
3594 times_pointer_size,
3595 FixedArray::kHeaderSize));
3596 __ mov(string_length,
3597 FieldOperand(string, String::kLengthOffset));
3598 __ shr(string_length, 1);
3599 __ lea(string,
3600 FieldOperand(string, SeqAsciiString::kHeaderSize));
3601 __ CopyBytes(string, result_pos, string_length, scratch);
3602 __ add(Operand(index), Immediate(1));
3603
3604 __ cmp(index, array_length_operand);
3605 __ j(less, &loop_3); // End while (index < length).
3606 __ jmp(&done);
3607
3524 3608
3525 __ bind(&bailout); 3609 __ bind(&bailout);
3526 __ mov(result, Factory::undefined_value()); 3610 __ mov(result_operand, Factory::undefined_value());
3527 __ bind(&done); 3611 __ bind(&done);
3528 __ mov(eax, result); 3612 __ mov(eax, result_operand);
3529 // Drop temp values from the stack, and restore context register. 3613 // Drop temp values from the stack, and restore context register.
3530 __ add(Operand(esp), Immediate(5 * kPointerSize)); 3614 __ add(Operand(esp), Immediate(3 * kPointerSize));
3531 3615
3532 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); 3616 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3533 context()->Plug(eax); 3617 context()->Plug(eax);
3534 } 3618 }
3535 3619
3536 3620
3537 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) { 3621 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
3538 Handle<String> name = expr->name(); 3622 Handle<String> name = expr->name();
3539 if (name->length() > 0 && name->Get(0) == '_') { 3623 if (name->length() > 0 && name->Get(0) == '_') {
3540 Comment cmnt(masm_, "[ InlineRuntimeCall"); 3624 Comment cmnt(masm_, "[ InlineRuntimeCall");
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after
4273 // And return. 4357 // And return.
4274 __ ret(0); 4358 __ ret(0);
4275 } 4359 }
4276 4360
4277 4361
4278 #undef __ 4362 #undef __
4279 4363
4280 } } // namespace v8::internal 4364 } } // namespace v8::internal
4281 4365
4282 #endif // V8_TARGET_ARCH_IA32 4366 #endif // V8_TARGET_ARCH_IA32
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698