Skip to content

Commit 362e541

Browse files
author
Meghana Gupta
committed
[MERGE #5173 @meg-gupta] OS#17394848: Fix OptConstFoldBr for undefined <=,>=
Merge pull request #5173 from meg-gupta:undefinedcmpbug undefined <= undefined is false, Fix constant folding code to represent this
2 parents 9fcce5d + 2352a5e commit 362e541

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

lib/Backend/GlobOpt.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6505,11 +6505,18 @@ bool BoolAndIntStaticAndTypeMismatch(Value* src1Val, Value* src2Val, Js::Var src
65056505
bool
65066506
GlobOpt::CanProveConditionalBranch(IR::Instr *instr, Value *src1Val, Value *src2Val, Js::Var src1Var, Js::Var src2Var, bool *result)
65076507
{
6508-
auto AreSourcesEqual = [&](Value * val1, Value * val2) -> bool
6508+
auto AreSourcesEqual = [&](Value * val1, Value * val2, bool undefinedCmp) -> bool
65096509
{
65106510
// NaN !== NaN, and objects can have valueOf/toString
6511-
return val1->IsEqualTo(val2) &&
6512-
val1->GetValueInfo()->IsPrimitive() && val1->GetValueInfo()->IsNotFloat();
6511+
if (val1->IsEqualTo(val2))
6512+
{
6513+
if (val1->GetValueInfo()->IsUndefined())
6514+
{
6515+
return undefinedCmp;
6516+
}
6517+
return val1->GetValueInfo()->IsPrimitive() && val1->GetValueInfo()->IsNotFloat();
6518+
}
6519+
return false;
65136520
};
65146521

65156522
// Make sure GetConstantVar only returns primitives.
@@ -6523,7 +6530,7 @@ GlobOpt::CanProveConditionalBranch(IR::Instr *instr, Value *src1Val, Value *src2
65236530

65246531
switch (instr->m_opcode)
65256532
{
6526-
#define BRANCHSIGNED(OPCODE,CMP,TYPE,UNSIGNEDNESS) \
6533+
#define BRANCHSIGNED(OPCODE,CMP,TYPE,UNSIGNEDNESS,UNDEFINEDCMP) \
65276534
case Js::OpCode::##OPCODE: \
65286535
if (src1Val && src2Val) \
65296536
{ \
@@ -6537,7 +6544,7 @@ GlobOpt::CanProveConditionalBranch(IR::Instr *instr, Value *src1Val, Value *src2
65376544
{ \
65386545
*result = (TYPE)left64 CMP(TYPE)right64; \
65396546
} \
6540-
else if (AreSourcesEqual(src1Val, src2Val)) \
6547+
else if (AreSourcesEqual(src1Val, src2Val, UNDEFINEDCMP)) \
65416548
{ \
65426549
*result = 0 CMP 0; \
65436550
} \
@@ -6552,25 +6559,25 @@ GlobOpt::CanProveConditionalBranch(IR::Instr *instr, Value *src1Val, Value *src2
65526559
} \
65536560
break;
65546561

6555-
BRANCHSIGNED(BrEq_I4, == , int64, false)
6556-
BRANCHSIGNED(BrGe_I4, >= , int64, false)
6557-
BRANCHSIGNED(BrGt_I4, > , int64, false)
6558-
BRANCHSIGNED(BrLt_I4, < , int64, false)
6559-
BRANCHSIGNED(BrLe_I4, <= , int64, false)
6560-
BRANCHSIGNED(BrNeq_I4, != , int64, false)
6561-
BRANCHSIGNED(BrUnGe_I4, >= , uint64, true)
6562-
BRANCHSIGNED(BrUnGt_I4, > , uint64, true)
6563-
BRANCHSIGNED(BrUnLt_I4, < , uint64, true)
6564-
BRANCHSIGNED(BrUnLe_I4, <= , uint64, true)
6562+
BRANCHSIGNED(BrEq_I4, == , int64, false, true)
6563+
BRANCHSIGNED(BrGe_I4, >= , int64, false, false)
6564+
BRANCHSIGNED(BrGt_I4, > , int64, false, false)
6565+
BRANCHSIGNED(BrLt_I4, < , int64, false, false)
6566+
BRANCHSIGNED(BrLe_I4, <= , int64, false, false)
6567+
BRANCHSIGNED(BrNeq_I4, != , int64, false, false)
6568+
BRANCHSIGNED(BrUnGe_I4, >= , uint64, true, false)
6569+
BRANCHSIGNED(BrUnGt_I4, > , uint64, true, false)
6570+
BRANCHSIGNED(BrUnLt_I4, < , uint64, true, false)
6571+
BRANCHSIGNED(BrUnLe_I4, <= , uint64, true, false)
65656572
#undef BRANCHSIGNED
6566-
#define BRANCH(OPCODE,CMP,VARCMPFUNC) \
6573+
#define BRANCH(OPCODE,CMP,VARCMPFUNC,UNDEFINEDCMP) \
65676574
case Js::OpCode::##OPCODE: \
65686575
if (src1Val && src2Val && src1Val->GetValueInfo()->TryGetIntConstantValue(&left) && \
65696576
src2Val->GetValueInfo()->TryGetIntConstantValue(&right)) \
65706577
{ \
65716578
*result = left CMP right; \
65726579
} \
6573-
else if (src1Val && src2Val && AreSourcesEqual(src1Val, src2Val)) \
6580+
else if (src1Val && src2Val && AreSourcesEqual(src1Val, src2Val, UNDEFINEDCMP)) \
65746581
{ \
65756582
*result = 0 CMP 0; \
65766583
} \
@@ -6588,14 +6595,14 @@ GlobOpt::CanProveConditionalBranch(IR::Instr *instr, Value *src1Val, Value *src2
65886595
} \
65896596
break;
65906597

6591-
BRANCH(BrGe_A, >= , Js::JavascriptOperators::GreaterEqual)
6592-
BRANCH(BrNotGe_A, <, !Js::JavascriptOperators::GreaterEqual)
6593-
BRANCH(BrLt_A, <, Js::JavascriptOperators::Less)
6594-
BRANCH(BrNotLt_A, >= , !Js::JavascriptOperators::Less)
6595-
BRANCH(BrGt_A, >, Js::JavascriptOperators::Greater)
6596-
BRANCH(BrNotGt_A, <= , !Js::JavascriptOperators::Greater)
6597-
BRANCH(BrLe_A, <= , Js::JavascriptOperators::LessEqual)
6598-
BRANCH(BrNotLe_A, >, !Js::JavascriptOperators::LessEqual)
6598+
BRANCH(BrGe_A, >= , Js::JavascriptOperators::GreaterEqual, /*undefinedEquality*/ false)
6599+
BRANCH(BrNotGe_A, <, !Js::JavascriptOperators::GreaterEqual, false)
6600+
BRANCH(BrLt_A, <, Js::JavascriptOperators::Less, false)
6601+
BRANCH(BrNotLt_A, >= , !Js::JavascriptOperators::Less, false)
6602+
BRANCH(BrGt_A, >, Js::JavascriptOperators::Greater, false)
6603+
BRANCH(BrNotGt_A, <= , !Js::JavascriptOperators::Greater, false)
6604+
BRANCH(BrLe_A, <= , Js::JavascriptOperators::LessEqual, false)
6605+
BRANCH(BrNotLe_A, >, !Js::JavascriptOperators::LessEqual, false)
65996606
#undef BRANCH
66006607
case Js::OpCode::BrEq_A:
66016608
case Js::OpCode::BrNotNeq_A:
@@ -6604,7 +6611,7 @@ GlobOpt::CanProveConditionalBranch(IR::Instr *instr, Value *src1Val, Value *src2
66046611
{
66056612
*result = left == right;
66066613
}
6607-
else if (src1Val && src2Val && AreSourcesEqual(src1Val, src2Val))
6614+
else if (src1Val && src2Val && AreSourcesEqual(src1Val, src2Val, true))
66086615
{
66096616
*result = true;
66106617
}
@@ -6636,7 +6643,7 @@ GlobOpt::CanProveConditionalBranch(IR::Instr *instr, Value *src1Val, Value *src2
66366643
{
66376644
*result = left != right;
66386645
}
6639-
else if (src1Val && src2Val && AreSourcesEqual(src1Val, src2Val))
6646+
else if (src1Val && src2Val && AreSourcesEqual(src1Val, src2Val, true))
66406647
{
66416648
*result = false;
66426649
}
@@ -6683,7 +6690,7 @@ GlobOpt::CanProveConditionalBranch(IR::Instr *instr, Value *src1Val, Value *src2
66836690
{
66846691
*result = false;
66856692
}
6686-
else if (AreSourcesEqual(src1Val, src2Val))
6693+
else if (AreSourcesEqual(src1Val, src2Val, true))
66876694
{
66886695
*result = true;
66896696
}
@@ -6725,7 +6732,7 @@ GlobOpt::CanProveConditionalBranch(IR::Instr *instr, Value *src1Val, Value *src2
67256732
{
67266733
*result = true;
67276734
}
6728-
else if (AreSourcesEqual(src1Val, src2Val))
6735+
else if (AreSourcesEqual(src1Val, src2Val, true))
67296736
{
67306737
*result = false;
67316738
}

0 commit comments

Comments
 (0)