Commit 28f3229a authored by Milad Farazmand's avatar Milad Farazmand Committed by Commit Bot

[Builtins] Use Bitcast instead of Conversion on Math.abs

Changes introduced in a5376b7e "Convert" the Smi values to int64 and
back to Smi. This behaviour fails as the 64-bit overflow check will
not work due to the conversion making Smi to the lower 32-bit.


Change-Id: Ida57baed13d8ad048cf2484e6984b4d26eb3fda5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1917421Reviewed-by: 's avatarJunliang Yan <jyan@ca.ibm.com>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Reviewed-by: 's avatarMichael Stanton <mvstanton@chromium.org>
Commit-Queue: Milad Farazmand <miladfar@ca.ibm.com>
Cr-Commit-Position: refs/heads/master@{#64982}
parent 2ac7ed8b
......@@ -30,8 +30,8 @@ namespace math {
// ES6 #sec-math.abs
extern macro IsIntPtrAbsWithOverflowSupported(): constexpr bool;
extern macro TrySmiSub(Smi, Smi): Smi labels Overflow;
extern macro TrySmiAbs(Smi): Smi labels Overflow;
extern macro Float64Abs(float64): float64;
extern macro EasyIntPtrAbsWithOverflow(intptr): intptr labels Overflow;
const kSmiMaxValuePlusOne:
constexpr float64 generates '0.0 - kSmiMinValue';
......@@ -43,9 +43,9 @@ namespace math {
label SmiResult(s: Smi) {
try {
if constexpr (IsIntPtrAbsWithOverflowSupported()) {
const result: intptr = EasyIntPtrAbsWithOverflow(Convert<intptr>(s))
const result: Smi = TrySmiAbs(s)
otherwise SmiOverflow;
return Convert<Smi>(result);
return result;
} else {
if (0 <= s) {
return s;
......
......@@ -894,6 +894,26 @@ TNode<Smi> CodeStubAssembler::TrySmiSub(TNode<Smi> lhs, TNode<Smi> rhs,
}
}
TNode<Smi> CodeStubAssembler::TrySmiAbs(TNode<Smi> a, Label* if_overflow) {
if (SmiValuesAre32Bits()) {
TNode<PairT<IntPtrT, BoolT>> pair =
IntPtrAbsWithOverflow(BitcastTaggedToWordForTagAndSmiBits(a));
TNode<BoolT> overflow = Projection<1>(pair);
GotoIf(overflow, if_overflow);
TNode<IntPtrT> result = Projection<0>(pair);
return BitcastWordToTaggedSigned(result);
} else {
CHECK(SmiValuesAre31Bits());
CHECK(IsInt32AbsWithOverflowSupported());
TNode<PairT<Int32T, BoolT>> pair = Int32AbsWithOverflow(
TruncateIntPtrToInt32(BitcastTaggedToWordForTagAndSmiBits(a)));
TNode<BoolT> overflow = Projection<1>(pair);
GotoIf(overflow, if_overflow);
TNode<Int32T> result = Projection<0>(pair);
return BitcastWordToTaggedSigned(ChangeInt32ToIntPtr(result));
}
}
TNode<Number> CodeStubAssembler::NumberMax(SloppyTNode<Number> a,
SloppyTNode<Number> b) {
// TODO(danno): This could be optimized by specifically handling smi cases.
......
......@@ -654,20 +654,13 @@ class V8_EXPORT_PRIVATE CodeStubAssembler
Label* if_overflow);
TNode<Smi> TrySmiAdd(TNode<Smi> a, TNode<Smi> b, Label* if_overflow);
TNode<Smi> TrySmiSub(TNode<Smi> a, TNode<Smi> b, Label* if_overflow);
TNode<Smi> TrySmiAbs(TNode<Smi> a, Label* if_overflow);
TNode<Smi> SmiShl(TNode<Smi> a, int shift) {
return BitcastWordToTaggedSigned(
WordShl(BitcastTaggedToWordForTagAndSmiBits(a), shift));
}
TNode<IntPtrT> EasyIntPtrAbsWithOverflow(TNode<IntPtrT> a,
Label* if_overflow) {
TNode<PairT<IntPtrT, BoolT>> pair = IntPtrAbsWithOverflow(a);
Node* overflow = Projection(1, pair);
GotoIf(overflow, if_overflow);
return UncheckedCast<IntPtrT>(Projection(0, pair));
}
TNode<Smi> SmiShr(TNode<Smi> a, int shift) {
if (kTaggedSize == kInt64Size) {
return BitcastWordToTaggedSigned(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment