OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 #ifndef DART_PRECOMPILED_RUNTIME | 4 #ifndef DART_PRECOMPILED_RUNTIME |
5 #include "vm/jit_optimizer.h" | 5 #include "vm/jit_optimizer.h" |
6 | 6 |
7 #include "vm/bit_vector.h" | 7 #include "vm/bit_vector.h" |
8 #include "vm/branch_optimizer.h" | 8 #include "vm/branch_optimizer.h" |
9 #include "vm/cha.h" | 9 #include "vm/cha.h" |
10 #include "vm/compiler.h" | 10 #include "vm/compiler.h" |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 if (!call->with_checks()) { | 202 if (!call->with_checks()) { |
203 return; // Already specialized. | 203 return; // Already specialized. |
204 } | 204 } |
205 | 205 |
206 const intptr_t receiver_cid = | 206 const intptr_t receiver_cid = |
207 call->PushArgumentAt(0)->value()->Type()->ToCid(); | 207 call->PushArgumentAt(0)->value()->Type()->ToCid(); |
208 if (receiver_cid == kDynamicCid) { | 208 if (receiver_cid == kDynamicCid) { |
209 return; // No information about receiver was infered. | 209 return; // No information about receiver was infered. |
210 } | 210 } |
211 | 211 |
212 const ICData& ic_data = FlowGraphCompiler::TrySpecializeICDataByReceiverCid( | 212 const ICData& ic_data = *call->instance_call()->ic_data(); |
213 call->ic_data(), receiver_cid); | 213 |
214 if (ic_data.raw() == call->ic_data().raw()) { | 214 const CallTargets* targets = |
| 215 FlowGraphCompiler::ResolveCallTargetsForReceiverCid( |
| 216 receiver_cid, String::Handle(zone(), ic_data.target_name()), |
| 217 Array::Handle(zone(), ic_data.arguments_descriptor())); |
| 218 if (targets == NULL) { |
215 // No specialization. | 219 // No specialization. |
216 return; | 220 return; |
217 } | 221 } |
218 | 222 |
219 const bool with_checks = false; | 223 const bool with_checks = false; |
220 const bool complete = false; | 224 const bool complete = false; |
221 PolymorphicInstanceCallInstr* specialized = | 225 PolymorphicInstanceCallInstr* specialized = |
222 new (Z) PolymorphicInstanceCallInstr(call->instance_call(), ic_data, | 226 new (Z) PolymorphicInstanceCallInstr(call->instance_call(), *targets, |
223 with_checks, complete); | 227 with_checks, complete); |
224 call->ReplaceWith(specialized, current_iterator()); | 228 call->ReplaceWith(specialized, current_iterator()); |
225 } | 229 } |
226 | 230 |
227 | 231 |
228 static bool ClassIdIsOneOf(intptr_t class_id, | 232 static bool ClassIdIsOneOf(intptr_t class_id, |
229 const GrowableArray<intptr_t>& class_ids) { | 233 const GrowableArray<intptr_t>& class_ids) { |
230 for (intptr_t i = 0; i < class_ids.length(); i++) { | 234 for (intptr_t i = 0; i < class_ids.length(); i++) { |
231 ASSERT(class_ids[i] != kIllegalCid); | 235 ASSERT(class_ids[i] != kIllegalCid); |
232 if (class_ids[i] == class_id) { | 236 if (class_ids[i] == class_id) { |
(...skipping 1204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1437 } | 1441 } |
1438 } | 1442 } |
1439 AssertAssignableInstr* assert_as = new (Z) AssertAssignableInstr( | 1443 AssertAssignableInstr* assert_as = new (Z) AssertAssignableInstr( |
1440 call->token_pos(), new (Z) Value(left), new (Z) Value(type_args), | 1444 call->token_pos(), new (Z) Value(left), new (Z) Value(type_args), |
1441 NULL, // TODO(regis): Pass function type arguments. | 1445 NULL, // TODO(regis): Pass function type arguments. |
1442 type, Symbols::InTypeCast(), call->deopt_id()); | 1446 type, Symbols::InTypeCast(), call->deopt_id()); |
1443 ReplaceCall(call, assert_as); | 1447 ReplaceCall(call, assert_as); |
1444 } | 1448 } |
1445 | 1449 |
1446 | 1450 |
1447 bool JitOptimizer::LookupMethodFor(int class_id, | |
1448 const ArgumentsDescriptor& args_desc, | |
1449 const String& name, | |
1450 Function* fn_return) { | |
1451 if (class_id < 0) return false; | |
1452 if (class_id >= I->class_table()->NumCids()) return false; | |
1453 | |
1454 RawClass* raw_class = I->class_table()->At(class_id); | |
1455 if (raw_class == NULL) return false; | |
1456 Class& cls = Class::Handle(Z, raw_class); | |
1457 if (cls.IsNull()) return false; | |
1458 if (!cls.is_finalized()) return false; | |
1459 if (Array::Handle(cls.functions()).IsNull()) return false; | |
1460 | |
1461 Function& target_function = Function::Handle( | |
1462 Z, Resolver::ResolveDynamicForReceiverClass(cls, name, args_desc)); | |
1463 if (target_function.IsNull()) return false; | |
1464 *fn_return ^= target_function.raw(); | |
1465 return true; | |
1466 } | |
1467 | |
1468 | |
1469 static int OrderById(const intptr_t* a, const intptr_t* b) { | |
1470 // Negative if 'a' should sort before 'b'. | |
1471 return *a - *b; | |
1472 } | |
1473 | |
1474 | |
1475 void JitOptimizer::TryExpandClassesInICData(const ICData& ic_data) { | |
1476 if (ic_data.NumberOfChecks() == 0) return; | |
1477 | |
1478 Function& dummy = Function::Handle(Z); | |
1479 | |
1480 GrowableArray<intptr_t> ids; | |
1481 for (int i = 0; i < ic_data.NumberOfChecks(); i++) { | |
1482 // The API works for multi dispatch ICs that check more than one argument, | |
1483 // but we know we only check one arg here, so only the 0th element of id | |
1484 // will be used. | |
1485 GrowableArray<intptr_t> id; | |
1486 ic_data.GetCheckAt(i, &id, &dummy); | |
1487 ids.Add(id[0]); | |
1488 } | |
1489 ids.Sort(OrderById); | |
1490 | |
1491 Array& args_desc_array = Array::Handle(Z, ic_data.arguments_descriptor()); | |
1492 ArgumentsDescriptor args_desc(args_desc_array); | |
1493 String& name = String::Handle(Z, ic_data.target_name()); | |
1494 | |
1495 Function& fn = Function::Handle(Z); | |
1496 Function& fn_high = Function::Handle(Z); | |
1497 Function& possible_match = Function::Handle(Z); | |
1498 | |
1499 for (int cid_index = 0; cid_index < ids.length() - 1; cid_index++) { | |
1500 int low_cid = ids[cid_index]; | |
1501 int high_cid = ids[cid_index + 1]; | |
1502 if (low_cid + 1 == high_cid) continue; | |
1503 if (LookupMethodFor(low_cid, args_desc, name, &fn) && | |
1504 LookupMethodFor(high_cid, args_desc, name, &fn_high) && | |
1505 fn.raw() == fn_high.raw()) { | |
1506 // Try to fill in the IC table by going downwards from a known class-id. | |
1507 bool can_fill_in = true; | |
1508 for (int i = low_cid + 1; i < high_cid; i++) { | |
1509 if (!LookupMethodFor(i, args_desc, name, &possible_match) || | |
1510 possible_match.raw() != fn.raw()) { | |
1511 can_fill_in = false; | |
1512 break; | |
1513 } | |
1514 } | |
1515 if (can_fill_in) { | |
1516 for (int i = low_cid + 1; i < high_cid; i++) { | |
1517 ic_data.AddReceiverCheck(i, fn, 0); | |
1518 } | |
1519 } | |
1520 } | |
1521 } | |
1522 } | |
1523 | |
1524 // Tries to optimize instance call by replacing it with a faster instruction | 1451 // Tries to optimize instance call by replacing it with a faster instruction |
1525 // (e.g, binary op, field load, ..). | 1452 // (e.g, binary op, field load, ..). |
1526 void JitOptimizer::VisitInstanceCall(InstanceCallInstr* instr) { | 1453 void JitOptimizer::VisitInstanceCall(InstanceCallInstr* instr) { |
1527 if (!instr->HasICData() || (instr->ic_data()->NumberOfUsedChecks() == 0)) { | 1454 if (!instr->HasICData() || (instr->ic_data()->NumberOfUsedChecks() == 0)) { |
1528 return; | 1455 return; |
1529 } | 1456 } |
1530 const Token::Kind op_kind = instr->token_kind(); | 1457 const Token::Kind op_kind = instr->token_kind(); |
1531 | 1458 |
1532 // Type test is special as it always gets converted into inlined code. | 1459 // Type test is special as it always gets converted into inlined code. |
1533 if (Token::IsTypeTestOperator(op_kind)) { | 1460 if (Token::IsTypeTestOperator(op_kind)) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1571 return; | 1498 return; |
1572 } | 1499 } |
1573 if ((op_kind == Token::kSET) && | 1500 if ((op_kind == Token::kSET) && |
1574 TryInlineInstanceSetter(instr, unary_checks)) { | 1501 TryInlineInstanceSetter(instr, unary_checks)) { |
1575 return; | 1502 return; |
1576 } | 1503 } |
1577 if (TryInlineInstanceMethod(instr)) { | 1504 if (TryInlineInstanceMethod(instr)) { |
1578 return; | 1505 return; |
1579 } | 1506 } |
1580 | 1507 |
1581 // Now we are done trying the inlining options that benefit from only having | 1508 CallTargets* targets = CallTargets::Create(Z, unary_checks); |
1582 // 1 entry in the IC table. | |
1583 TryExpandClassesInICData(unary_checks); | |
1584 | 1509 |
1585 bool has_one_target = unary_checks.HasOneTarget(); | 1510 bool has_one_target = targets->HasSingleTarget(); |
1586 | 1511 |
1587 if (has_one_target) { | 1512 if (has_one_target) { |
1588 // Check if the single target is a polymorphic target, if it is, | 1513 // Check if the single target is a polymorphic target, if it is, |
1589 // we don't have one target. | 1514 // we don't have one target. |
1590 const Function& target = Function::Handle(Z, unary_checks.GetTargetAt(0)); | 1515 const Function& target = Function::Handle(Z, unary_checks.GetTargetAt(0)); |
1591 if (target.recognized_kind() == MethodRecognizer::kObjectRuntimeType) { | 1516 if (target.recognized_kind() == MethodRecognizer::kObjectRuntimeType) { |
1592 has_one_target = PolymorphicInstanceCallInstr::ComputeRuntimeType( | 1517 has_one_target = PolymorphicInstanceCallInstr::ComputeRuntimeType( |
1593 unary_checks) != Type::null(); | 1518 *targets) != Type::null(); |
1594 } else { | 1519 } else { |
1595 const bool polymorphic_target = | 1520 const bool polymorphic_target = |
1596 MethodRecognizer::PolymorphicTarget(target); | 1521 MethodRecognizer::PolymorphicTarget(target); |
1597 has_one_target = !polymorphic_target; | 1522 has_one_target = !polymorphic_target; |
1598 } | 1523 } |
1599 } | 1524 } |
1600 | 1525 |
1601 if (has_one_target) { | 1526 if (has_one_target) { |
1602 const Function& target = Function::Handle(Z, unary_checks.GetTargetAt(0)); | 1527 const Function& target = Function::Handle(Z, unary_checks.GetTargetAt(0)); |
1603 const RawFunction::Kind function_kind = target.kind(); | 1528 const RawFunction::Kind function_kind = target.kind(); |
1604 if (!flow_graph()->InstanceCallNeedsClassCheck(instr, function_kind)) { | 1529 if (!flow_graph()->InstanceCallNeedsClassCheck(instr, function_kind)) { |
1605 PolymorphicInstanceCallInstr* call = | 1530 PolymorphicInstanceCallInstr* call = |
1606 new (Z) PolymorphicInstanceCallInstr(instr, unary_checks, | 1531 new (Z) PolymorphicInstanceCallInstr(instr, *targets, |
1607 /* call_with_checks = */ false, | 1532 /* call_with_checks = */ false, |
1608 /* complete = */ false); | 1533 /* complete = */ false); |
1609 instr->ReplaceWith(call, current_iterator()); | 1534 instr->ReplaceWith(call, current_iterator()); |
1610 return; | 1535 return; |
1611 } | 1536 } |
1612 } | 1537 } |
1613 | 1538 |
1614 bool call_with_checks; | 1539 bool call_with_checks; |
1615 if (has_one_target && FLAG_polymorphic_with_deopt) { | 1540 if (has_one_target && FLAG_polymorphic_with_deopt) { |
1616 // Type propagation has not run yet, we cannot eliminate the check. | 1541 // Type propagation has not run yet, we cannot eliminate the check. |
| 1542 // TODO(erikcorry): The receiver check should use the off-heap targets |
| 1543 // array, not the IC array. |
1617 AddReceiverCheck(instr); | 1544 AddReceiverCheck(instr); |
1618 // Call can still deoptimize, do not detach environment from instr. | 1545 // Call can still deoptimize, do not detach environment from instr. |
1619 call_with_checks = false; | 1546 call_with_checks = false; |
1620 } else { | 1547 } else { |
1621 call_with_checks = true; | 1548 call_with_checks = true; |
1622 } | 1549 } |
1623 PolymorphicInstanceCallInstr* call = new (Z) | 1550 PolymorphicInstanceCallInstr* call = |
1624 PolymorphicInstanceCallInstr(instr, unary_checks, call_with_checks, | 1551 new (Z) PolymorphicInstanceCallInstr(instr, *targets, call_with_checks, |
1625 /* complete = */ false); | 1552 /* complete = */ false); |
1626 instr->ReplaceWith(call, current_iterator()); | 1553 instr->ReplaceWith(call, current_iterator()); |
1627 } | 1554 } |
1628 | 1555 |
1629 | 1556 |
1630 void JitOptimizer::VisitStaticCall(StaticCallInstr* call) { | 1557 void JitOptimizer::VisitStaticCall(StaticCallInstr* call) { |
1631 MethodRecognizer::Kind recognized_kind = | 1558 MethodRecognizer::Kind recognized_kind = |
1632 MethodRecognizer::RecognizeKind(call->function()); | 1559 MethodRecognizer::RecognizeKind(call->function()); |
1633 switch (recognized_kind) { | 1560 switch (recognized_kind) { |
1634 case MethodRecognizer::kObjectConstructor: | 1561 case MethodRecognizer::kObjectConstructor: |
1635 case MethodRecognizer::kObjectArrayAllocate: | 1562 case MethodRecognizer::kObjectArrayAllocate: |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1870 // Discard the environment from the original instruction because the store | 1797 // Discard the environment from the original instruction because the store |
1871 // can't deoptimize. | 1798 // can't deoptimize. |
1872 instr->RemoveEnvironment(); | 1799 instr->RemoveEnvironment(); |
1873 ReplaceCall(instr, store); | 1800 ReplaceCall(instr, store); |
1874 return true; | 1801 return true; |
1875 } | 1802 } |
1876 | 1803 |
1877 | 1804 |
1878 } // namespace dart | 1805 } // namespace dart |
1879 #endif // DART_PRECOMPILED_RUNTIME | 1806 #endif // DART_PRECOMPILED_RUNTIME |
OLD | NEW |