Reviewers: Paul Lind, kisg, wingo, danno, kilvadyb,

Description:
MIPS: Generators: Avoid calling into runtime if operand stack is empty

Port r15240 (909f716e)

Original commit message:
This patch makes yield sites save the resume continuation and context
inline.  If the operand stack is empty, we can avoid a call into the
runtime.  This also makes the SuspendJSGeneratorObject runtime function
less magical: it just has to save the operand stack and stack handlers.

This speeds up the following case by a factor of 3 or so:

  function* until(n) {
    for (var i = 0; i < n; i++)
      yield i;
  }

  function sum(iter) {
    var sum = 0;
    for (var x of iter) sum += x;
    return sum;
  }

  for (var i = 0; i < 10000; i++) sum(until(1000))

Also, there is no more sentinel value as the generators will resume in
the right place already, allowing me to remove the hack added to the
--debug-code check in r14437.

BUG=


Please review this at https://codereview.chromium.org/17291016/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/mips/full-codegen-mips.cc


Index: src/mips/full-codegen-mips.cc
diff --git a/src/mips/full-codegen-mips.cc b/src/mips/full-codegen-mips.cc
index 06226ca671d0372a2068bbe055fc7d80c3ace75e..7ef9fd6c04031719d2826791f308a264815b44fd 100644
--- a/src/mips/full-codegen-mips.cc
+++ b/src/mips/full-codegen-mips.cc
@@ -2001,14 +2001,28 @@ void FullCodeGenerator::VisitYield(Yield* expr) {
       __ push(result_register());
       // Fall through.
     case Yield::INITIAL: {
-      VisitForStackValue(expr->generator_object());
-      __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
-      __ lw(context_register(),
-            MemOperand(fp, StandardFrameConstants::kContextOffset));
+      Label suspend, continuation, post_runtime, resume;

-      Label resume;
-      __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
-      __ Branch(&resume, ne, result_register(), Operand(at));
+      __ jmp(&suspend);
+
+      __ bind(&continuation);
+      __ jmp(&resume);
+
+      __ bind(&suspend);
+      VisitForAccumulatorValue(expr->generator_object());
+      ASSERT(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
+      __ li(a1, Operand(Smi::FromInt(continuation.pos())));
+ __ sw(a1, FieldMemOperand(v0, JSGeneratorObject::kContinuationOffset));
+      __ sw(cp, FieldMemOperand(v0, JSGeneratorObject::kContextOffset));
+      __ mov(a1, cp);
+      __ RecordWriteField(v0, JSGeneratorObject::kContextOffset, a1, a2,
+                          kRAHasBeenSaved, kDontSaveFPRegs);
+      __ Addu(a1, fp, Operand(StandardFrameConstants::kExpressionsOffset));
+      __ Branch(&post_runtime, eq, sp, Operand(a1));
+      __ push(v0);  // generator object
+      __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
+      __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
+      __ bind(&post_runtime);
       __ pop(result_register());
       EmitReturnSequence();

@@ -2036,7 +2050,8 @@ void FullCodeGenerator::VisitYield(Yield* expr) {
       // [sp + 1 * kPointerSize] iter
       // [sp + 0 * kPointerSize] g

-      Label l_catch, l_try, l_resume, l_next, l_call, l_loop;
+      Label l_catch, l_try, l_suspend, l_continuation, l_resume;
+      Label l_next, l_call, l_loop;
       // Initial send value is undefined.
       __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
       __ Branch(&l_next);
@@ -2059,14 +2074,23 @@ void FullCodeGenerator::VisitYield(Yield* expr) {
       __ PushTryHandler(StackHandler::CATCH, expr->index());
       const int handler_size = StackHandlerConstants::kSize;
       __ push(a0);                                       // result
- __ lw(a3, MemOperand(sp, (0 + 1) * kPointerSize + handler_size)); // g
-      __ push(a3);                                       // g
-      __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
+      __ jmp(&l_suspend);
+      __ bind(&l_continuation);
       __ mov(a0, v0);
-      __ lw(context_register(),
-            MemOperand(fp, StandardFrameConstants::kContextOffset));
-      __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
-      __ Branch(&l_resume, ne, a0, Operand(at));
+      __ jmp(&l_resume);
+      __ bind(&l_suspend);
+      const int generator_object_depth = kPointerSize + handler_size;
+      __ lw(a0, MemOperand(sp, generator_object_depth));
+      __ push(a0);                                       // g
+ ASSERT(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
+      __ li(a1, Operand(Smi::FromInt(l_continuation.pos())));
+ __ sw(a1, FieldMemOperand(a0, JSGeneratorObject::kContinuationOffset));
+      __ sw(cp, FieldMemOperand(a0, JSGeneratorObject::kContextOffset));
+      __ mov(a1, cp);
+      __ RecordWriteField(a0, JSGeneratorObject::kContextOffset, a1, a2,
+                          kRAHasBeenSaved, kDontSaveFPRegs);
+      __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
+      __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
       __ pop(v0);                                        // result
       EmitReturnSequence();
       __ mov(a0, v0);


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to