Skip to content

Commit 6167b3a

Browse files
committed
[MERGE #5195 @rajatd] Make fast path for Math.min and Math.max handle dst == (either) src. OS #17429610
Merge pull request #5195 from rajatd:minmax-master
2 parents ae7360a + eea9310 commit 6167b3a

File tree

3 files changed

+66
-28
lines changed

3 files changed

+66
-28
lines changed

lib/Backend/LowerMDShared.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8127,28 +8127,44 @@ void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMeth
81278127

81288128
bool min = instr->m_opcode == Js::OpCode::InlineMathMin ? true : false;
81298129

8130+
bool dstEqualsSrc1 = dst->IsEqual(src1);
8131+
bool dstEqualsSrc2 = dst->IsEqual(src2);
8132+
8133+
IR::Opnd * otherSrc = src2;
8134+
IR::Opnd * compareSrc1 = src1;
8135+
IR::Opnd * compareSrc2 = src2;
8136+
8137+
if (dstEqualsSrc2)
8138+
{
8139+
otherSrc = src1;
8140+
compareSrc1 = src2;
8141+
compareSrc2 = src1;
8142+
}
8143+
if (!dstEqualsSrc1 && !dstEqualsSrc2)
8144+
{
8145+
//MOV dst, src1;
8146+
this->m_lowerer->InsertMove(dst, src1, instr);
8147+
}
8148+
81308149
// CMP src1, src2
81318150
if(dst->IsInt32())
8132-
{
8133-
//MOV dst, src2;
8134-
Assert(!dst->IsEqual(src2));
8135-
this->m_lowerer->InsertMove(dst, src2, instr);
8151+
{
81368152
if(min)
81378153
{
81388154
// JLT $continueLabel
8139-
branchInstr = IR::BranchInstr::New(Js::OpCode::BrGt_I4, doneLabel, src1, src2, instr->m_func);
8155+
branchInstr = IR::BranchInstr::New(Js::OpCode::BrLt_I4, doneLabel, compareSrc1, compareSrc2, instr->m_func);
81408156
instr->InsertBefore(branchInstr);
81418157
LowererMDArch::EmitInt4Instr(branchInstr);
81428158
}
81438159
else
81448160
{
81458161
// JGT $continueLabel
8146-
branchInstr = IR::BranchInstr::New(Js::OpCode::BrLt_I4, doneLabel, src1, src2, instr->m_func);
8162+
branchInstr = IR::BranchInstr::New(Js::OpCode::BrGt_I4, doneLabel, compareSrc1, compareSrc2, instr->m_func);
81478163
instr->InsertBefore(branchInstr);
81488164
LowererMDArch::EmitInt4Instr(branchInstr);
81498165
}
81508166
// MOV dst, src1
8151-
this->m_lowerer->InsertMove(dst, src1, instr);
8167+
this->m_lowerer->InsertMove(dst, otherSrc, instr);
81528168
}
81538169
else if(dst->IsFloat())
81548170
{
@@ -8177,33 +8193,29 @@ void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMeth
81778193
//
81788194
// $doneLabel
81798195

8180-
//MOVSD/MOVSS dst, src1;
8181-
Assert(!dst->IsEqual(src1));
8182-
8183-
this->m_lowerer->InsertMove(dst, src1, instr);
81848196
if(min)
81858197
{
8186-
this->m_lowerer->InsertCompareBranch(src1, src2, Js::OpCode::BrLt_A, doneLabel, instr); // Lowering of BrLt_A for floats is done to JA with operands swapped
8198+
this->m_lowerer->InsertCompareBranch(compareSrc1, compareSrc2, Js::OpCode::BrLt_A, doneLabel, instr); // Lowering of BrLt_A for floats is done to JA with operands swapped
81878199
}
81888200
else
81898201
{
8190-
this->m_lowerer->InsertCompareBranch(src1, src2, Js::OpCode::BrGt_A, doneLabel, instr);
8202+
this->m_lowerer->InsertCompareBranch(compareSrc1, compareSrc2, Js::OpCode::BrGt_A, doneLabel, instr);
81918203
}
81928204

81938205
instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelNegZeroAndNaNCheckHelper, instr->m_func));
81948206

8195-
this->m_lowerer->InsertMove(dst, src2, instr);
8207+
this->m_lowerer->InsertMove(dst, otherSrc, instr);
81968208
instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, instr->m_func));
81978209

81988210
instr->InsertBefore(labelNegZeroAndNaNCheckHelper);
81998211

82008212
instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JP, labelNaNHelper, instr->m_func));
82018213

82028214
IR::LabelInstr *isNeg0Label = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
8203-
NegZeroBranching(min ? src2 : src1, instr, isNeg0Label, doneLabel);
8215+
NegZeroBranching(min ? compareSrc2 : compareSrc1, instr, isNeg0Label, doneLabel);
82048216
instr->InsertBefore(isNeg0Label);
82058217

8206-
this->m_lowerer->InsertMove(dst, src2, instr);
8218+
this->m_lowerer->InsertMove(dst, otherSrc, instr);
82078219
instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, instr->m_func));
82088220

82098221
instr->InsertBefore(labelNaNHelper);

lib/Backend/arm/LowerMD.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6361,30 +6361,45 @@ void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMeth
63616361

63626362
bool min = instr->m_opcode == Js::OpCode::InlineMathMin ? true : false;
63636363

6364-
//(V)MOV dst, src1;
6365-
Assert(!dst->IsEqual(src1));
6366-
this->m_lowerer->InsertMove(dst, src1, instr);
6364+
bool dstEqualsSrc1 = dst->IsEqual(src1);
6365+
bool dstEqualsSrc2 = dst->IsEqual(src2);
6366+
6367+
IR::Opnd * otherSrc = src2;
6368+
IR::Opnd * compareSrc1 = src1;
6369+
IR::Opnd * compareSrc2 = src2;
6370+
6371+
if (dstEqualsSrc2)
6372+
{
6373+
otherSrc = src1;
6374+
compareSrc1 = src2;
6375+
compareSrc2 = src1;
6376+
}
6377+
if (!dstEqualsSrc1 && !dstEqualsSrc2)
6378+
{
6379+
//(V)MOV dst, src1;
6380+
this->m_lowerer->InsertMove(dst, src1, instr);
6381+
}
63676382

63686383
if(dst->IsInt32())
63696384
{
63706385
// CMP src1, src2
63716386
if(min)
63726387
{
63736388
// BLT $continueLabel
6374-
branchInstr = IR::BranchInstr::New(Js::OpCode::BrLt_I4, doneLabel, src1, src2, instr->m_func);
6389+
branchInstr = IR::BranchInstr::New(Js::OpCode::BrLt_I4, doneLabel, compareSrc1, compareSrc2, instr->m_func);
63756390
instr->InsertBefore(branchInstr);
63766391
this->EmitInt4Instr(branchInstr);
63776392
}
63786393
else
63796394
{
63806395
// BGT $continueLabel
6381-
branchInstr = IR::BranchInstr::New(Js::OpCode::BrGt_I4, doneLabel, src1, src2, instr->m_func);
6396+
branchInstr = IR::BranchInstr::New(Js::OpCode::BrGt_I4, doneLabel, compareSrc1, compareSrc2, instr->m_func);
63826397
instr->InsertBefore(branchInstr);
63836398
this->EmitInt4Instr(branchInstr);
63846399
}
63856400

63866401
// MOV dst, src2
6387-
this->m_lowerer->InsertMove(dst, src2, instr);
6402+
this->m_lowerer->InsertMove(dst, otherSrc, instr);
63886403
}
63896404

63906405
else if(dst->IsFloat64())
@@ -6416,35 +6431,35 @@ void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMeth
64166431

64176432
if(min)
64186433
{
6419-
this->m_lowerer->InsertCompareBranch(src1, src2, Js::OpCode::BrLt_A, doneLabel, instr); // Lowering of BrLt_A for floats is done to JA with operands swapped
6434+
this->m_lowerer->InsertCompareBranch(compareSrc1, compareSrc2, Js::OpCode::BrLt_A, doneLabel, instr); // Lowering of BrLt_A for floats is done to JA with operands swapped
64206435
}
64216436
else
64226437
{
6423-
this->m_lowerer->InsertCompareBranch(src1, src2, Js::OpCode::BrGt_A, doneLabel, instr);
6438+
this->m_lowerer->InsertCompareBranch(compareSrc1, compareSrc2, Js::OpCode::BrGt_A, doneLabel, instr);
64246439
}
64256440

64266441
instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::BVS, labelNaNHelper, instr->m_func));
64276442

64286443
instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::BEQ, labelNegZeroCheckHelper, instr->m_func));
64296444

6430-
this->m_lowerer->InsertMove(dst, src2, instr);
6445+
this->m_lowerer->InsertMove(dst, otherSrc, instr);
64316446
instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::B, doneLabel, instr->m_func));
64326447

64336448
instr->InsertBefore(labelNegZeroCheckHelper);
64346449

64356450
IR::Opnd* isNegZero;
64366451
if(min)
64376452
{
6438-
isNegZero = IsOpndNegZero(src2, instr);
6453+
isNegZero = IsOpndNegZero(compareSrc2, instr);
64396454
}
64406455
else
64416456
{
6442-
isNegZero = IsOpndNegZero(src1, instr);
6457+
isNegZero = IsOpndNegZero(compareSrc1, instr);
64436458
}
64446459

64456460
this->m_lowerer->InsertCompareBranch(isNegZero, IR::IntConstOpnd::New(0x00000000, IRType::TyInt32, this->m_func), Js::OpCode::BrEq_A, doneLabel, instr);
64466461

6447-
this->m_lowerer->InsertMove(dst, src2, instr);
6462+
this->m_lowerer->InsertMove(dst, otherSrc, instr);
64486463
instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::B, doneLabel, instr->m_func));
64496464

64506465
instr->InsertBefore(labelNaNHelper);

test/Math/max.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,14 @@ function findMaxInArray()
180180
}
181181
findMaxInArray();
182182
findMaxInArray();
183+
184+
function OS17429610()
185+
{
186+
var obj0 = {};
187+
for (var __loopvar1 = 6; __loopvar1 < 10; __loopvar1++) {
188+
this.prop1 = Math.max(this.prop1, obj0)
189+
}
190+
}
191+
OS17429610()
192+
OS17429610()
193+
OS17429610()

0 commit comments

Comments
 (0)