Change in dart/sdk[master]: [vm/compiler] Strength reduce x * 2^n to x << n for n > 1

2 views
Skip to first unread message

Vyacheslav Egorov (Gerrit)

unread,
Nov 7, 2018, 11:13:54 AM11/7/18
to vm-...@dartlang.org, rev...@dartlang.org, Aart Bik, commi...@chromium.org

PTAL

View Change

    To view, visit change 83420. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sdk
    Gerrit-Branch: master
    Gerrit-Change-Id: If82b3bee7b28357fcc5e0559188ace91a94d1b49
    Gerrit-Change-Number: 83420
    Gerrit-PatchSet: 1
    Gerrit-Owner: Vyacheslav Egorov <veg...@google.com>
    Gerrit-Reviewer: Aart Bik <ajc...@google.com>
    Gerrit-Reviewer: Vyacheslav Egorov <veg...@google.com>
    Gerrit-Comment-Date: Wed, 07 Nov 2018 16:13:52 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Vyacheslav Egorov (Gerrit)

    unread,
    Nov 7, 2018, 11:13:54 AM11/7/18
    to Aart Bik, vm-...@dartlang.org, rev...@dartlang.org

    Vyacheslav Egorov would like Aart Bik to review this change.

    View Change

    [vm/compiler] Strength reduce x * 2^n to x << n for n > 1

    For some unclear reason we only handled n = 0 and n = 1 before.

    Revealed by looking at disassembly from https://github.com/flutter/flutter/issues/19666

    Change-Id: If82b3bee7b28357fcc5e0559188ace91a94d1b49
    ---
    M runtime/vm/compiler/backend/il.cc
    1 file changed, 4 insertions(+), 5 deletions(-)

    diff --git a/runtime/vm/compiler/backend/il.cc b/runtime/vm/compiler/backend/il.cc
    index 2b5574f..709446d 100644
    --- a/runtime/vm/compiler/backend/il.cc
    +++ b/runtime/vm/compiler/backend/il.cc
    @@ -2414,12 +2414,12 @@
    return left()->definition();
    } else if (rhs == 0) {
    return right()->definition();
    - } else if (rhs == 2) {
    - ConstantInstr* constant_1 =
    - flow_graph->GetConstant(Smi::Handle(Smi::New(1)));
    + } else if (Utils::IsPowerOfTwo(static_cast<uword>(rhs))) {
    + ConstantInstr* shift_amount = flow_graph->GetConstant(Smi::Handle(
    + Smi::New(Utils::ShiftForPowerOfTwo(static_cast<uword>(rhs)))));
    BinaryIntegerOpInstr* shift = BinaryIntegerOpInstr::Make(
    representation(), Token::kSHL, left()->CopyWithType(),
    - new Value(constant_1), GetDeoptId(), can_overflow(),
    + new Value(shift_amount), GetDeoptId(), can_overflow(),
    is_truncating(), range(), speculative_mode());
    if (shift != NULL) {
    flow_graph->InsertBefore(this, shift, env(), FlowGraph::kValue);
    @@ -3213,7 +3213,6 @@
    return true;
    }

    - // Recognize
    if (BindsToGivenConstant(mask_op->left(), value) ||
    BindsToGivenConstant(mask_op->right(), value)) {
    // Recognized (a & 2^n) == 2^n pattern. It's equivalent to (a & 2^n) != 0

    To view, visit change 83420. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sdk
    Gerrit-Branch: master
    Gerrit-Change-Id: If82b3bee7b28357fcc5e0559188ace91a94d1b49
    Gerrit-Change-Number: 83420
    Gerrit-PatchSet: 1
    Gerrit-Owner: Vyacheslav Egorov <veg...@google.com>
    Gerrit-Reviewer: Aart Bik <ajc...@google.com>
    Gerrit-Reviewer: Vyacheslav Egorov <veg...@google.com>
    Gerrit-MessageType: newchange

    Aart Bik (Gerrit)

    unread,
    Nov 7, 2018, 12:10:24 PM11/7/18
    to Vyacheslav Egorov, vm-...@dartlang.org, rev...@dartlang.org, commi...@chromium.org

    View Change

    1 comment:

    To view, visit change 83420. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sdk
    Gerrit-Branch: master
    Gerrit-Change-Id: If82b3bee7b28357fcc5e0559188ace91a94d1b49
    Gerrit-Change-Number: 83420
    Gerrit-PatchSet: 1
    Gerrit-Owner: Vyacheslav Egorov <veg...@google.com>
    Gerrit-Reviewer: Aart Bik <ajc...@google.com>
    Gerrit-Reviewer: Vyacheslav Egorov <veg...@google.com>
    Gerrit-Comment-Date: Wed, 07 Nov 2018 17:10:22 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Stephen Adams (Gerrit)

    unread,
    Nov 7, 2018, 12:33:05 PM11/7/18
    to Vyacheslav Egorov, vm-...@dartlang.org, rev...@dartlang.org, Aart Bik, commi...@chromium.org

    "For some unclear reason we only handled n = 0 and n = 1 before"

    Might be a hold-over from unbounded int - on some architectures (e.g. x64) you can't tell if the shift overflowed for shifts by more than 1.

    Now that you don't care about overflow, on x64 you can also use LEA for x*M+C for M∈{2,3,4,5,8,9}, with the added benefit of targeting a different register.

    View Change

      To view, visit change 83420. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: sdk
      Gerrit-Branch: master
      Gerrit-Change-Id: If82b3bee7b28357fcc5e0559188ace91a94d1b49
      Gerrit-Change-Number: 83420
      Gerrit-PatchSet: 1
      Gerrit-Owner: Vyacheslav Egorov <veg...@google.com>
      Gerrit-Reviewer: Aart Bik <ajc...@google.com>
      Gerrit-Reviewer: Vyacheslav Egorov <veg...@google.com>
      Gerrit-CC: Stephen Adams <s...@google.com>
      Gerrit-Comment-Date: Wed, 07 Nov 2018 17:33:03 +0000

      Aart Bik (Gerrit)

      unread,
      Nov 14, 2018, 8:00:46 PM11/14/18
      to Vyacheslav Egorov, vm-...@dartlang.org, rev...@dartlang.org, Stephen Adams, commi...@chromium.org

      View Change

      1 comment:

        • Patch Set #1, Line 2419: Smi::New(Utils::ShiftForPowerOfTwo(static_cast<uword>(rhs)))));

          how about adding some tests to this CL?

        • Slava, are you working on this?

      To view, visit change 83420. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: sdk
      Gerrit-Branch: master
      Gerrit-Change-Id: If82b3bee7b28357fcc5e0559188ace91a94d1b49
      Gerrit-Change-Number: 83420
      Gerrit-PatchSet: 1
      Gerrit-Owner: Vyacheslav Egorov <veg...@google.com>
      Gerrit-Reviewer: Aart Bik <ajc...@google.com>
      Gerrit-Reviewer: Vyacheslav Egorov <veg...@google.com>
      Gerrit-CC: Stephen Adams <s...@google.com>
      Gerrit-Comment-Date: Thu, 15 Nov 2018 01:00:45 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Aart Bik <ajc...@google.com>
      Gerrit-MessageType: comment

      Vyacheslav Egorov (Gerrit)

      unread,
      Nov 15, 2018, 11:31:47 AM11/15/18
      to vm-...@dartlang.org, rev...@dartlang.org, Aart Bik, Stephen Adams, commi...@chromium.org

      View Change

      1 comment:

        • Slava, are you working on this?

        • Yep. It's the next CL in the list of CLs I am going to get to :)

      To view, visit change 83420. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: sdk
      Gerrit-Branch: master
      Gerrit-Change-Id: If82b3bee7b28357fcc5e0559188ace91a94d1b49
      Gerrit-Change-Number: 83420
      Gerrit-PatchSet: 1
      Gerrit-Owner: Vyacheslav Egorov <veg...@google.com>
      Gerrit-Reviewer: Aart Bik <ajc...@google.com>
      Gerrit-Reviewer: Vyacheslav Egorov <veg...@google.com>
      Gerrit-CC: Stephen Adams <s...@google.com>
      Gerrit-Comment-Date: Thu, 15 Nov 2018 16:31:45 +0000

      Slava Egorov (Gerrit)

      unread,
      May 19, 2022, 12:26:13 AM5/19/22
      to vm-...@dartlang.org, rev...@dartlang.org, Stephen Adams, Commit Bot

      Slava Egorov abandoned this change.

      View Change

      Abandoned Ryan has landed similar CL.

      To view, visit change 83420. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: sdk
      Gerrit-Branch: main
      Gerrit-Change-Id: If82b3bee7b28357fcc5e0559188ace91a94d1b49
      Gerrit-Change-Number: 83420
      Gerrit-PatchSet: 1
      Gerrit-Owner: Slava Egorov <veg...@google.com>
      Gerrit-Reviewer: Slava Egorov <veg...@google.com>
      Gerrit-CC: Commit Bot <commi...@chromium.org>
      Gerrit-CC: Stephen Adams <s...@google.com>
      Gerrit-MessageType: abandon
      Reply all
      Reply to author
      Forward
      0 new messages