Revision: 11109
Author:   [email protected]
Date:     Thu Mar 22 06:53:28 2012
Log:      Support arguments object access from inlined functions.

[email protected]
TEST=test/mjsunit/compiler/inline-arguments.js

Review URL: https://chromiumcodereview.appspot.com/9837002
http://code.google.com/p/v8/source/detail?r=11109

Modified:
 /branches/bleeding_edge/src/arm/lithium-arm.cc
 /branches/bleeding_edge/src/arm/lithium-arm.h
 /branches/bleeding_edge/src/arm/lithium-codegen-arm.cc
 /branches/bleeding_edge/src/hydrogen-instructions.cc
 /branches/bleeding_edge/src/hydrogen-instructions.h
 /branches/bleeding_edge/src/hydrogen.cc
 /branches/bleeding_edge/src/hydrogen.h
 /branches/bleeding_edge/src/ia32/lithium-codegen-ia32.cc
 /branches/bleeding_edge/src/ia32/lithium-ia32.cc
 /branches/bleeding_edge/src/ia32/lithium-ia32.h
 /branches/bleeding_edge/src/runtime.cc
 /branches/bleeding_edge/src/x64/lithium-codegen-x64.cc
 /branches/bleeding_edge/src/x64/lithium-x64.cc
 /branches/bleeding_edge/src/x64/lithium-x64.h
 /branches/bleeding_edge/test/mjsunit/compiler/inline-arguments.js

=======================================
--- /branches/bleeding_edge/src/arm/lithium-arm.cc      Mon Mar 12 05:49:41 2012
+++ /branches/bleeding_edge/src/arm/lithium-arm.cc      Thu Mar 22 06:53:28 2012
@@ -1077,7 +1077,8 @@


LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
-  return DefineAsRegister(new(zone()) LArgumentsElements);
+  return DefineAsRegister(new(zone()) LArgumentsElements(
+      current_block_->last_environment()->outer() != NULL));
 }


@@ -2271,6 +2272,9 @@
                                                undefined,
                                                instr->call_kind(),
                                                instr->is_construct());
+  if (instr->materializes_arguments()) {
+    inner->Bind(instr->arguments(), graph()->GetArgumentsObject());
+  }
   current_block_->UpdateEnvironment(inner);
   chunk_->AddInlinedClosure(instr->closure());
   return NULL;
@@ -2278,10 +2282,21 @@


 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
+  LInstruction* pop = NULL;
+
+  HEnvironment* env = current_block_->last_environment();
+
+  if (instr->arguments_pushed()) {
+    int argument_count = env->arguments_environment()->parameter_count();
+    pop = new(zone()) LPop(argument_count);
+    argument_count_ -= argument_count;
+  }
+
   HEnvironment* outer = current_block_->last_environment()->
       DiscardInlined(false);
   current_block_->UpdateEnvironment(outer);
-  return NULL;
+
+  return pop;
 }


=======================================
--- /branches/bleeding_edge/src/arm/lithium-arm.h       Mon Mar 12 05:49:41 2012
+++ /branches/bleeding_edge/src/arm/lithium-arm.h       Thu Mar 22 06:53:28 2012
@@ -179,7 +179,8 @@
   V(CheckMapValue)                              \
   V(LoadFieldByIndex)                           \
   V(DateField)                                  \
-  V(WrapReceiver)
+  V(WrapReceiver)                               \
+  V(Pop)


 #define DECLARE_CONCRETE_INSTRUCTION(type, mnemonic)              \
@@ -534,9 +535,15 @@

 class LArgumentsElements: public LTemplateInstruction<1, 0, 0> {
  public:
-  LArgumentsElements() { }
+  explicit LArgumentsElements(bool from_inlined)
+      : from_inlined_(from_inlined) { }
+
+  bool from_inlined() const { return from_inlined_; }

   DECLARE_CONCRETE_INSTRUCTION(ArgumentsElements, "arguments-elements")
+
+ private:
+  bool from_inlined_;
 };


@@ -1378,6 +1385,19 @@
 };


+class LPop: public LTemplateInstruction<0, 0, 0> {
+ public:
+  explicit LPop(int count) : count_(count) { }
+
+  int count() const { return count_; }
+
+  DECLARE_CONCRETE_INSTRUCTION(Pop, "pop")
+
+ private:
+  int count_;
+};
+
+
 class LThisFunction: public LTemplateInstruction<1, 0, 0> {
  public:
   DECLARE_CONCRETE_INSTRUCTION(ThisFunction, "this-function")
=======================================
--- /branches/bleeding_edge/src/arm/lithium-codegen-arm.cc Tue Mar 20 01:49:23 2012 +++ /branches/bleeding_edge/src/arm/lithium-codegen-arm.cc Thu Mar 22 06:53:28 2012
@@ -2764,16 +2764,20 @@
   Register scratch = scratch0();
   Register result = ToRegister(instr->result());

-  // Check if the calling frame is an arguments adaptor frame.
-  Label done, adapted;
-  __ ldr(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
- __ ldr(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
-  __ cmp(result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
-
- // Result is the frame pointer for the frame if not adapted and for the real
-  // frame below the adaptor frame if adapted.
-  __ mov(result, fp, LeaveCC, ne);
-  __ mov(result, scratch, LeaveCC, eq);
+  if (instr->from_inlined()) {
+    __ add(result, sp, Operand(-2 * kPointerSize));
+  } else {
+    // Check if the calling frame is an arguments adaptor frame.
+    Label done, adapted;
+ __ ldr(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); + __ ldr(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
+    __ cmp(result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
+
+ // Result is the frame pointer for the frame if not adapted and for the real
+    // frame below the adaptor frame if adapted.
+    __ mov(result, fp, LeaveCC, ne);
+    __ mov(result, scratch, LeaveCC, eq);
+  }
 }


@@ -2905,6 +2909,11 @@
     __ push(argument_reg);
   }
 }
+
+
+void LCodeGen::DoPop(LPop* instr) {
+  __ Drop(instr->count());
+}


 void LCodeGen::DoThisFunction(LThisFunction* instr) {
=======================================
--- /branches/bleeding_edge/src/hydrogen-instructions.cc Thu Mar 15 05:21:29 2012 +++ /branches/bleeding_edge/src/hydrogen-instructions.cc Thu Mar 22 06:53:28 2012
@@ -599,6 +599,9 @@
   SetBlock(block);
   previous->next_ = this;
   if (next != NULL) next->previous_ = this;
+  if (block->last() == previous) {
+    block->set_last(this);
+  }
 }


=======================================
--- /branches/bleeding_edge/src/hydrogen-instructions.h Thu Mar 15 05:21:29 2012 +++ /branches/bleeding_edge/src/hydrogen-instructions.h Thu Mar 22 06:53:28 2012
@@ -1353,12 +1353,15 @@
                 int arguments_count,
                 FunctionLiteral* function,
                 CallKind call_kind,
-                bool is_construct)
+                bool is_construct,
+                Variable* arguments)
       : closure_(closure),
         arguments_count_(arguments_count),
         function_(function),
         call_kind_(call_kind),
-        is_construct_(is_construct) {
+        is_construct_(is_construct),
+        arguments_(arguments),
+        materializes_arguments_(false) {
   }

   virtual void PrintDataTo(StringStream* stream);
@@ -1372,6 +1375,13 @@
   virtual Representation RequiredInputRepresentation(int index) {
     return Representation::None();
   }
+
+  bool materializes_arguments() { return materializes_arguments_; }
+  void set_materializes_arguments(bool materializes_arguments) {
+    materializes_arguments_ = materializes_arguments;
+  }
+
+  Variable* arguments() { return arguments_; }

   DECLARE_CONCRETE_INSTRUCTION(EnterInlined)

@@ -1381,18 +1391,28 @@
   FunctionLiteral* function_;
   CallKind call_kind_;
   bool is_construct_;
+  Variable* arguments_;
+  bool materializes_arguments_;
 };


 class HLeaveInlined: public HTemplateInstruction<0> {
  public:
-  HLeaveInlined() {}
+  explicit HLeaveInlined(bool arguments_pushed)
+      : arguments_pushed_(arguments_pushed) { }

   virtual Representation RequiredInputRepresentation(int index) {
     return Representation::None();
   }
+
+  bool arguments_pushed() {
+    return arguments_pushed_;
+  }

   DECLARE_CONCRETE_INSTRUCTION(LeaveInlined)
+
+ private:
+  bool arguments_pushed_;
 };


=======================================
--- /branches/bleeding_edge/src/hydrogen.cc     Wed Mar 21 02:23:09 2012
+++ /branches/bleeding_edge/src/hydrogen.cc     Thu Mar 22 06:53:28 2012
@@ -113,7 +113,6 @@
     first_ = last_ = entry;
   }
   instr->InsertAfter(last_);
-  last_ = instr;
 }


@@ -165,11 +164,15 @@
 }


-void HBasicBlock::Goto(HBasicBlock* block, bool drop_extra) {
+void HBasicBlock::Goto(HBasicBlock* block, FunctionState* state) {
+  bool drop_extra = state != NULL && state->drop_extra();
+  bool arguments_pushed = state != NULL && state->arguments_pushed();
+
   if (block->IsInlineReturnTarget()) {
-    AddInstruction(new(zone()) HLeaveInlined);
+    AddInstruction(new(zone()) HLeaveInlined(arguments_pushed));
     last_environment_ = last_environment()->DiscardInlined(drop_extra);
   }
+
   AddSimulate(AstNode::kNoNumber);
   HGoto* instr = new(zone()) HGoto(block);
   Finish(instr);
@@ -178,10 +181,13 @@

 void HBasicBlock::AddLeaveInlined(HValue* return_value,
                                   HBasicBlock* target,
-                                  bool drop_extra) {
+                                  FunctionState* state) {
+  bool drop_extra = state != NULL && state->drop_extra();
+  bool arguments_pushed = state != NULL && state->arguments_pushed();
+
   ASSERT(target->IsInlineReturnTarget());
   ASSERT(return_value != NULL);
-  AddInstruction(new(zone()) HLeaveInlined);
+  AddInstruction(new(zone()) HLeaveInlined(arguments_pushed));
   last_environment_ = last_environment()->DiscardInlined(drop_extra);
   last_environment()->Push(return_value);
   AddSimulate(AstNode::kNoNumber);
@@ -2178,6 +2184,8 @@
       return_handling_(return_handling),
       function_return_(NULL),
       test_context_(NULL),
+      entry_(NULL),
+      arguments_elements_(NULL),
       outer_(owner->function_state()) {
   if (outer_ != NULL) {
     // State for an inline function.
@@ -2337,8 +2345,8 @@
   instr->SetSuccessorAt(0, empty_true);
   instr->SetSuccessorAt(1, empty_false);
   owner()->current_block()->Finish(instr);
-  empty_true->Goto(if_true(), owner()->function_state()->drop_extra());
-  empty_false->Goto(if_false(), owner()->function_state()->drop_extra());
+  empty_true->Goto(if_true(), owner()->function_state());
+  empty_false->Goto(if_false(), owner()->function_state());
   owner()->set_current_block(NULL);
 }

@@ -2359,8 +2367,8 @@
HBranch* test = new(zone()) HBranch(value, empty_true, empty_false, expected);
   builder->current_block()->Finish(test);

-  empty_true->Goto(if_true(), owner()->function_state()->drop_extra());
-  empty_false->Goto(if_false(), owner()->function_state()->drop_extra());
+  empty_true->Goto(if_true(), owner()->function_state());
+  empty_false->Goto(if_false(), owner()->function_state());
   builder->set_current_block(NULL);
 }

@@ -2851,10 +2859,10 @@
     if (context->IsTest()) {
       TestContext* test = TestContext::cast(context);
       CHECK_ALIVE(VisitForEffect(stmt->expression()));
- current_block()->Goto(test->if_true(), function_state()->drop_extra());
+      current_block()->Goto(test->if_true(), function_state());
     } else if (context->IsEffect()) {
       CHECK_ALIVE(VisitForEffect(stmt->expression()));
- current_block()->Goto(function_return(), function_state()->drop_extra());
+      current_block()->Goto(function_return(), function_state());
     } else {
       ASSERT(context->IsValue());
       CHECK_ALIVE(VisitForValue(stmt->expression()));
@@ -2871,10 +2879,10 @@
       current_block()->Finish(typecheck);
       if_spec_object->AddLeaveInlined(return_value,
                                       function_return(),
-                                      function_state()->drop_extra());
+                                      function_state());
       not_spec_object->AddLeaveInlined(receiver,
                                        function_return(),
-                                       function_state()->drop_extra());
+                                       function_state());
     }
   } else {
     // Return from an inlined function, visit the subexpression in the
@@ -2886,14 +2894,14 @@
                       test->if_false());
     } else if (context->IsEffect()) {
       CHECK_ALIVE(VisitForEffect(stmt->expression()));
- current_block()->Goto(function_return(), function_state()->drop_extra());
+      current_block()->Goto(function_return(), function_state());
     } else {
       ASSERT(context->IsValue());
       CHECK_ALIVE(VisitForValue(stmt->expression()));
       HValue* return_value = Pop();
       current_block()->AddLeaveInlined(return_value,
                                        function_return(),
-                                       function_state()->drop_extra());
+                                       function_state());
     }
   }
   set_current_block(NULL);
@@ -4932,31 +4940,70 @@
     return false;
   }

-  // Our implementation of arguments (based on this stack frame or an
-  // adapter below it) does not work for inlined functions.
   if (function_state()->outer() != NULL) {
-    Bailout("arguments access in inlined function");
-    return true;
+    // Push arguments when entering inlined function.
+    if (!function_state()->arguments_pushed()) {
+      HEnvironment* arguments_env = environment()->arguments_environment();
+
+      HInstruction* insert_after = function_state()->entry();
+      ASSERT(insert_after->IsEnterInlined());
+      HEnterInlined::cast(insert_after)->set_materializes_arguments(true);
+
+      for (int i = 0; i < arguments_env->parameter_count(); i++) {
+        HValue* argument = arguments_env->Lookup(i);
+        HInstruction* push_argument = new(zone()) HPushArgument(argument);
+        push_argument->InsertAfter(insert_after);
+        insert_after = push_argument;
+      }
+
+      HInstruction* arguments_elements = new(zone()) HArgumentsElements();
+      arguments_elements->ClearFlag(HValue::kUseGVN);
+      arguments_elements->InsertAfter(insert_after);
+      function_state()->set_arguments_elements(arguments_elements);
+    }
   }

   HInstruction* result = NULL;
   if (expr->key()->IsPropertyName()) {
     Handle<String> name = expr->key()->AsLiteral()->AsPropertyName();
     if (!name->IsEqualTo(CStrVector("length"))) return false;
- HInstruction* elements = AddInstruction(new(zone()) HArgumentsElements);
-    result = new(zone()) HArgumentsLength(elements);
+
+    if (function_state()->outer() == NULL) {
+ HInstruction* elements = AddInstruction(new(zone()) HArgumentsElements);
+      result = new(zone()) HArgumentsLength(elements);
+    } else {
+      // Number of arguments without receiver.
+      int argument_count = environment()->
+          arguments_environment()->parameter_count() - 1;
+      result = new(zone()) HConstant(
+        Handle<Object>(Smi::FromInt(argument_count)),
+        Representation::Integer32());
+    }
   } else {
     Push(graph()->GetArgumentsObject());
     VisitForValue(expr->key());
     if (HasStackOverflow() || current_block() == NULL) return true;
     HValue* key = Pop();
     Drop(1);  // Arguments object.
- HInstruction* elements = AddInstruction(new(zone()) HArgumentsElements);
-    HInstruction* length = AddInstruction(
-        new(zone()) HArgumentsLength(elements));
-    HInstruction* checked_key =
-        AddInstruction(new(zone()) HBoundsCheck(key, length));
-    result = new(zone()) HAccessArgumentsAt(elements, length, checked_key);
+    if (function_state()->outer() == NULL) {
+ HInstruction* elements = AddInstruction(new(zone()) HArgumentsElements);
+      HInstruction* length = AddInstruction(
+          new(zone()) HArgumentsLength(elements));
+      HInstruction* checked_key =
+          AddInstruction(new(zone()) HBoundsCheck(key, length));
+ result = new(zone()) HAccessArgumentsAt(elements, length, checked_key);
+    } else {
+      // Number of arguments without receiver.
+      HInstruction* elements = function_state()->arguments_elements();
+      int argument_count = environment()->
+          arguments_environment()->parameter_count() - 1;
+      HInstruction* length = AddInstruction(new(zone()) HConstant(
+        Handle<Object>(Smi::FromInt(argument_count)),
+        Representation::Integer32()));
+      HInstruction* checked_key =
+          AddInstruction(new(zone()) HBoundsCheck(key, length));
+ result = new(zone()) HAccessArgumentsAt(elements, length, checked_key);
+    }
   }
   ast_context()->ReturnInstruction(result, expr->id());
   return true;
@@ -5364,19 +5411,24 @@
   AddInstruction(context);
   inner_env->BindContext(context);
 #endif
-  AddSimulate(return_id);
-  current_block()->UpdateEnvironment(inner_env);
-  AddInstruction(new(zone()) HEnterInlined(target,
-                                           arguments->length(),
-                                           function,
-                                           call_kind,
- function_state()->is_construct()));
   // If the function uses arguments object create and bind one.
   if (function->scope()->arguments() != NULL) {
     ASSERT(function->scope()->arguments()->IsStackAllocated());
-    environment()->Bind(function->scope()->arguments(),
-                        graph()->GetArgumentsObject());
-  }
+    inner_env->Bind(function->scope()->arguments(),
+                    graph()->GetArgumentsObject());
+  }
+
+  AddSimulate(return_id);
+  current_block()->UpdateEnvironment(inner_env);
+
+  HInstruction* enter_inlined =
+      AddInstruction(new(zone()) HEnterInlined(target,
+                                               arguments->length(),
+                                               function,
+                                               call_kind,
+ function_state()->is_construct(), + function->scope()->arguments()));
+  function_state()->set_entry(enter_inlined);
   VisitDeclarations(target_info.scope()->declarations());
   VisitStatements(function->body());
   if (HasStackOverflow()) {
@@ -5405,17 +5457,17 @@
           : undefined;
       current_block()->AddLeaveInlined(return_value,
                                        function_return(),
-                                       function_state()->drop_extra());
+                                       function_state());
     } else if (call_context()->IsEffect()) {
       ASSERT(function_return() != NULL);
- current_block()->Goto(function_return(), function_state()->drop_extra());
+      current_block()->Goto(function_return(), function_state());
     } else {
       ASSERT(call_context()->IsTest());
       ASSERT(inlined_test_context() != NULL);
       HBasicBlock* target = function_state()->is_construct()
           ? inlined_test_context()->if_true()
           : inlined_test_context()->if_false();
-      current_block()->Goto(target, function_state()->drop_extra());
+      current_block()->Goto(target, function_state());
     }
   }

@@ -5433,12 +5485,12 @@
     if (if_true->HasPredecessor()) {
       if_true->SetJoinId(ast_id);
HBasicBlock* true_target = TestContext::cast(ast_context())->if_true();
-      if_true->Goto(true_target, function_state()->drop_extra());
+      if_true->Goto(true_target, function_state());
     }
     if (if_false->HasPredecessor()) {
       if_false->SetJoinId(ast_id);
HBasicBlock* false_target = TestContext::cast(ast_context())->if_false();
-      if_false->Goto(false_target, function_state()->drop_extra());
+      if_false->Goto(false_target, function_state());
     }
     set_current_block(NULL);
     return true;
=======================================
--- /branches/bleeding_edge/src/hydrogen.h      Tue Mar 20 11:15:31 2012
+++ /branches/bleeding_edge/src/hydrogen.h      Thu Mar 22 06:53:28 2012
@@ -42,6 +42,7 @@

 // Forward declarations.
 class BitVector;
+class FunctionState;
 class HEnvironment;
 class HGraph;
 class HLoopInformation;
@@ -121,7 +122,7 @@

   void Finish(HControlInstruction* last);
   void FinishExit(HControlInstruction* instruction);
-  void Goto(HBasicBlock* block, bool drop_extra = false);
+  void Goto(HBasicBlock* block, FunctionState* state = NULL);

   int PredecessorIndexOf(HBasicBlock* predecessor) const;
   void AddSimulate(int ast_id) { AddInstruction(CreateSimulate(ast_id)); }
@@ -136,7 +137,7 @@
   // instruction and updating the bailout environment.
   void AddLeaveInlined(HValue* return_value,
                        HBasicBlock* target,
-                       bool drop_extra = false);
+                       FunctionState* state = NULL);

   // If a target block is tagged as an inline function return, all
   // predecessors should contain the inlined exit sequence:
@@ -714,6 +715,16 @@
   }

   FunctionState* outer() { return outer_; }
+
+  HInstruction* entry() { return entry_; }
+  void set_entry(HInstruction* entry) { entry_ = entry; }
+
+  HInstruction* arguments_elements() { return arguments_elements_; }
+  void set_arguments_elements(HInstruction* arguments_elements) {
+    arguments_elements_ = arguments_elements;
+  }
+
+  bool arguments_pushed() { return arguments_elements() != NULL; }

  private:
   HGraphBuilder* owner_;
@@ -741,6 +752,12 @@
   // return blocks.  NULL in all other cases.
   TestContext* test_context_;

+  // When inlining HEnterInlined instruction corresponding to the function
+  // entry.
+  HInstruction* entry_;
+
+  HInstruction* arguments_elements_;
+
   FunctionState* outer_;
 };

=======================================
--- /branches/bleeding_edge/src/ia32/lithium-codegen-ia32.cc Tue Mar 20 01:49:23 2012 +++ /branches/bleeding_edge/src/ia32/lithium-codegen-ia32.cc Thu Mar 22 06:53:28 2012
@@ -2543,25 +2543,29 @@
 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
   Register result = ToRegister(instr->result());

-  // Check for arguments adapter frame.
-  Label done, adapted;
-  __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
-  __ mov(result, Operand(result, StandardFrameConstants::kContextOffset));
-  __ cmp(Operand(result),
-         Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
-  __ j(equal, &adapted, Label::kNear);
-
-  // No arguments adaptor frame.
-  __ mov(result, Operand(ebp));
-  __ jmp(&done, Label::kNear);
-
-  // Arguments adaptor frame present.
-  __ bind(&adapted);
-  __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
-
- // Result is the frame pointer for the frame if not adapted and for the real
-  // frame below the adaptor frame if adapted.
-  __ bind(&done);
+  if (instr->from_inlined()) {
+    __ lea(result, Operand(esp, -2 * kPointerSize));
+  } else {
+    // Check for arguments adapter frame.
+    Label done, adapted;
+    __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
+ __ mov(result, Operand(result, StandardFrameConstants::kContextOffset));
+    __ cmp(Operand(result),
+           Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
+    __ j(equal, &adapted, Label::kNear);
+
+    // No arguments adaptor frame.
+    __ mov(result, Operand(ebp));
+    __ jmp(&done, Label::kNear);
+
+    // Arguments adaptor frame present.
+    __ bind(&adapted);
+    __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
+
+ // Result is the frame pointer for the frame if not adapted and for the real
+    // frame below the adaptor frame if adapted.
+    __ bind(&done);
+  }
 }


@@ -2681,6 +2685,11 @@
   LOperand* argument = instr->InputAt(0);
   EmitPushTaggedOperand(argument);
 }
+
+
+void LCodeGen::DoPop(LPop* instr) {
+  __ Drop(instr->count());
+}


 void LCodeGen::DoThisFunction(LThisFunction* instr) {
=======================================
--- /branches/bleeding_edge/src/ia32/lithium-ia32.cc Mon Mar 19 00:45:06 2012 +++ /branches/bleeding_edge/src/ia32/lithium-ia32.cc Thu Mar 22 06:53:28 2012
@@ -1083,7 +1083,8 @@


LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
-  return DefineAsRegister(new(zone()) LArgumentsElements);
+  return DefineAsRegister(new(zone()) LArgumentsElements(
+      current_block_->last_environment()->outer() != NULL));
 }


@@ -2380,6 +2381,9 @@
                                                undefined,
                                                instr->call_kind(),
                                                instr->is_construct());
+  if (instr->materializes_arguments()) {
+    inner->Bind(instr->arguments(), graph()->GetArgumentsObject());
+  }
   current_block_->UpdateEnvironment(inner);
   chunk_->AddInlinedClosure(instr->closure());
   return NULL;
@@ -2387,10 +2391,20 @@


 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
+  LInstruction* pop = NULL;
+
+  HEnvironment* env = current_block_->last_environment();
+
+  if (instr->arguments_pushed()) {
+    int argument_count = env->arguments_environment()->parameter_count();
+    pop = new(zone()) LPop(argument_count);
+    argument_count_ -= argument_count;
+  }
+
   HEnvironment* outer = current_block_->last_environment()->
       DiscardInlined(false);
   current_block_->UpdateEnvironment(outer);
-  return NULL;
+  return pop;
 }


=======================================
--- /branches/bleeding_edge/src/ia32/lithium-ia32.h     Mon Mar 19 00:45:06 2012
+++ /branches/bleeding_edge/src/ia32/lithium-ia32.h     Thu Mar 22 06:53:28 2012
@@ -174,7 +174,8 @@
   V(CheckMapValue)                              \
   V(LoadFieldByIndex)                           \
   V(DateField)                                  \
-  V(WrapReceiver)
+  V(WrapReceiver)                               \
+  V(Pop)


 #define DECLARE_CONCRETE_INSTRUCTION(type, mnemonic)              \
@@ -525,9 +526,15 @@

 class LArgumentsElements: public LTemplateInstruction<1, 0, 0> {
  public:
-  LArgumentsElements() { }
+  explicit LArgumentsElements(bool from_inlined)
+      : from_inlined_(from_inlined) { }
+
+  bool from_inlined() const { return from_inlined_; }

   DECLARE_CONCRETE_INSTRUCTION(ArgumentsElements, "arguments-elements")
+
+ private:
+  bool from_inlined_;
 };


@@ -1401,6 +1408,19 @@
 };


+class LPop: public LTemplateInstruction<0, 0, 0> {
+ public:
+  explicit LPop(int count) : count_(count) { }
+
+  int count() const { return count_; }
+
+  DECLARE_CONCRETE_INSTRUCTION(Pop, "pop")
+
+ private:
+  int count_;
+};
+
+
 class LThisFunction: public LTemplateInstruction<1, 0, 0> {
  public:
   DECLARE_CONCRETE_INSTRUCTION(ThisFunction, "this-function")
=======================================
--- /branches/bleeding_edge/src/runtime.cc      Fri Mar 16 06:59:59 2012
+++ /branches/bleeding_edge/src/runtime.cc      Thu Mar 22 06:53:28 2012
@@ -8140,6 +8140,14 @@
         ASSERT(*arguments != isolate->heap()->undefined_value());
       }
       frame->SetExpression(i, *arguments);
+      if (FLAG_trace_deopt) {
+        PrintF("Materializing arguments object for frame %p - %p: %p ",
+               reinterpret_cast<void*>(frame->sp()),
+               reinterpret_cast<void*>(frame->fp()),
+               reinterpret_cast<void*>(*arguments));
+        arguments->ShortPrint();
+        PrintF("\n");
+      }
     }
   }
 }
=======================================
--- /branches/bleeding_edge/src/x64/lithium-codegen-x64.cc Tue Mar 20 01:49:23 2012 +++ /branches/bleeding_edge/src/x64/lithium-codegen-x64.cc Thu Mar 22 06:53:28 2012
@@ -2497,24 +2497,28 @@
 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
   Register result = ToRegister(instr->result());

-  // Check for arguments adapter frame.
-  Label done, adapted;
-  __ movq(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
-  __ Cmp(Operand(result, StandardFrameConstants::kContextOffset),
-         Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
-  __ j(equal, &adapted, Label::kNear);
-
-  // No arguments adaptor frame.
-  __ movq(result, rbp);
-  __ jmp(&done, Label::kNear);
-
-  // Arguments adaptor frame present.
-  __ bind(&adapted);
-  __ movq(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
-
- // Result is the frame pointer for the frame if not adapted and for the real
-  // frame below the adaptor frame if adapted.
-  __ bind(&done);
+  if (instr->from_inlined()) {
+    __ lea(result, Operand(rsp, -2 * kPointerSize));
+  } else {
+    // Check for arguments adapter frame.
+    Label done, adapted;
+    __ movq(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
+    __ Cmp(Operand(result, StandardFrameConstants::kContextOffset),
+           Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
+    __ j(equal, &adapted, Label::kNear);
+
+    // No arguments adaptor frame.
+    __ movq(result, rbp);
+    __ jmp(&done, Label::kNear);
+
+    // Arguments adaptor frame present.
+    __ bind(&adapted);
+    __ movq(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
+
+ // Result is the frame pointer for the frame if not adapted and for the real
+    // frame below the adaptor frame if adapted.
+    __ bind(&done);
+  }
 }


@@ -2638,6 +2642,11 @@
   LOperand* argument = instr->InputAt(0);
   EmitPushTaggedOperand(argument);
 }
+
+
+void LCodeGen::DoPop(LPop* instr) {
+  __ Drop(instr->count());
+}


 void LCodeGen::DoThisFunction(LThisFunction* instr) {
=======================================
--- /branches/bleeding_edge/src/x64/lithium-x64.cc      Mon Mar 12 05:49:41 2012
+++ /branches/bleeding_edge/src/x64/lithium-x64.cc      Thu Mar 22 06:53:28 2012
@@ -1071,7 +1071,8 @@


LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
-  return DefineAsRegister(new(zone()) LArgumentsElements);
+  return DefineAsRegister(new(zone()) LArgumentsElements(
+      current_block_->last_environment()->outer() != NULL));
 }


@@ -2270,6 +2271,9 @@
                                                undefined,
                                                instr->call_kind(),
                                                instr->is_construct());
+  if (instr->materializes_arguments()) {
+    inner->Bind(instr->arguments(), graph()->GetArgumentsObject());
+  }
   current_block_->UpdateEnvironment(inner);
   chunk_->AddInlinedClosure(instr->closure());
   return NULL;
@@ -2277,10 +2281,21 @@


 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
+  LInstruction* pop = NULL;
+
+  HEnvironment* env = current_block_->last_environment();
+
+  if (instr->arguments_pushed()) {
+    int argument_count = env->arguments_environment()->parameter_count();
+    pop = new(zone()) LPop(argument_count);
+    argument_count_ -= argument_count;
+  }
+
   HEnvironment* outer = current_block_->last_environment()->
       DiscardInlined(false);
   current_block_->UpdateEnvironment(outer);
-  return NULL;
+
+  return pop;
 }


=======================================
--- /branches/bleeding_edge/src/x64/lithium-x64.h       Mon Mar 12 05:49:41 2012
+++ /branches/bleeding_edge/src/x64/lithium-x64.h       Thu Mar 22 06:53:28 2012
@@ -179,7 +179,8 @@
   V(CheckMapValue)                              \
   V(LoadFieldByIndex)                           \
   V(DateField)                                  \
-  V(WrapReceiver)
+  V(WrapReceiver)                               \
+  V(Pop)


 #define DECLARE_CONCRETE_INSTRUCTION(type, mnemonic)              \
@@ -535,9 +536,15 @@

 class LArgumentsElements: public LTemplateInstruction<1, 0, 0> {
  public:
-  LArgumentsElements() { }
+  explicit LArgumentsElements(bool from_inlined)
+      : from_inlined_(from_inlined) { }
+
+  bool from_inlined() const { return from_inlined_; }

   DECLARE_CONCRETE_INSTRUCTION(ArgumentsElements, "arguments-elements")
+
+ private:
+  bool from_inlined_;
 };


@@ -1358,6 +1365,19 @@
 };


+class LPop: public LTemplateInstruction<0, 0, 0> {
+ public:
+  explicit LPop(int count) : count_(count) { }
+
+  int count() const { return count_; }
+
+  DECLARE_CONCRETE_INSTRUCTION(Pop, "pop")
+
+ private:
+  int count_;
+};
+
+
 class LThisFunction: public LTemplateInstruction<1, 0, 0> {
  public:
   DECLARE_CONCRETE_INSTRUCTION(ThisFunction, "this-function")
=======================================
--- /branches/bleeding_edge/test/mjsunit/compiler/inline-arguments.js Tue Mar 20 11:15:31 2012 +++ /branches/bleeding_edge/test/mjsunit/compiler/inline-arguments.js Thu Mar 22 06:53:28 2012
@@ -113,3 +113,46 @@
   %OptimizeFunctionOnNextCall(test_adaptation);
   test_adaptation();
 })();
+
+// Test arguments access from the inlined function.
+function uninlinable(v) {
+  assertEquals(0, v);
+  try { } catch (e) { }
+  return 0;
+}
+
+function toarr_inner() {
+  var a = arguments;
+  var marker = a[0];
+  uninlinable(uninlinable(0, 0), marker.x);
+
+  var r = new Array();
+  for (var i = a.length - 1; i >= 1; i--) {
+    r.push(a[i]);
+  }
+
+  return r;
+}
+
+function toarr1(marker, a, b, c) {
+  return toarr_inner(marker, a / 2, b / 2, c / 2);
+}
+
+function toarr2(marker, a, b, c) {
+  var x = 0;
+  return uninlinable(uninlinable(0, 0),
+                    x = toarr_inner(marker, a / 2, b / 2, c / 2)), x;
+}
+
+function test_toarr(toarr) {
+  var marker = { x: 0 };
+  assertArrayEquals([3, 2, 1], toarr(marker, 2, 4, 6));
+  assertArrayEquals([3, 2, 1], toarr(marker, 2, 4, 6));
+  %OptimizeFunctionOnNextCall(toarr);
+  assertArrayEquals([3, 2, 1], toarr(marker, 2, 4, 6));
+  delete marker.x;
+  assertArrayEquals([3, 2, 1], toarr(marker, 2, 4, 6));
+}
+
+test_toarr(toarr1);
+test_toarr(toarr2);

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to