From 085ec5c25f9dd1cc51914d90c3e0aa1c45d002fe Mon Sep 17 00:00:00 2001 From: bmeurer Date: Thu, 21 Jul 2016 21:33:46 -0700 Subject: [PATCH] [turbofan] Improve lowering for NumberAbs to Int32Abs. We can compute the absolute integer value w/o any conditional execution by using the bit trick formula let sign = input >> 31 in (input ^ sign) - sign which generates fairly decent code on all supported architectures. R=jarin@chromium.org Review-Url: https://codereview.chromium.org/2169293002 Cr-Commit-Position: refs/heads/master@{#37965} --- src/compiler/simplified-lowering.cc | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/compiler/simplified-lowering.cc b/src/compiler/simplified-lowering.cc index ad55f1db91..0e0e22e311 100644 --- a/src/compiler/simplified-lowering.cc +++ b/src/compiler/simplified-lowering.cc @@ -3117,14 +3117,18 @@ Node* SimplifiedLowering::Float64Trunc(Node* const node) { } Node* SimplifiedLowering::Int32Abs(Node* const node) { - Node* const zero = jsgraph()->Int32Constant(0); Node* const input = node->InputAt(0); - // if 0 < input then input else 0 - input - return graph()->NewNode( - common()->Select(MachineRepresentation::kWord32, BranchHint::kTrue), - graph()->NewNode(machine()->Int32LessThan(), zero, input), input, - graph()->NewNode(machine()->Int32Sub(), zero, input)); + // Generate case for absolute integer value. + // + // let sign = input >> 31 in + // (input ^ sign) - sign + + Node* sign = graph()->NewNode(machine()->Word32Sar(), input, + jsgraph()->Int32Constant(31)); + return graph()->NewNode(machine()->Int32Sub(), + graph()->NewNode(machine()->Word32Xor(), input, sign), + sign); } Node* SimplifiedLowering::Int32Div(Node* const node) {