Skip to content

Commit a3737b0

Browse files
committed
[MERGE #5673 @sigatrev] oopjit branch fold BrTrue and BrFalse for true, false, and numbers.
Merge pull request #5673 from sigatrev:branchFold allows the branch from ```if (a)``` to be folded when ```a``` is known to be true, false, or a number. This will also improve the IsIn optimization which currently replaces the instr with LdTrue when it is can be proven, and will improve any potential wins of allowing bools to be fixed fields.
2 parents e565611 + aef954a commit a3737b0

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

lib/Backend/GlobOpt.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6765,16 +6765,36 @@ GlobOpt::CanProveConditionalBranch(IR::Instr *instr, Value *src1Val, Value *src2
67656765
break;
67666766
}
67676767

6768-
if (func->IsOOPJIT() || !CONFIG_FLAG(OOPJITMissingOpts))
6768+
if (!src1Var)
67696769
{
6770-
// TODO: OOP JIT, const folding
67716770
return false;
67726771
}
6773-
if (!src1Var)
6772+
6773+
// Set *result = (evaluates true) and negate it later for BrFalse
6774+
if (src1Var == reinterpret_cast<Js::Var>(this->func->GetScriptContextInfo()->GetTrueAddr()))
6775+
{
6776+
*result = true;
6777+
}
6778+
else if (src1Var == reinterpret_cast<Js::Var>(this->func->GetScriptContextInfo()->GetFalseAddr()))
6779+
{
6780+
*result = false;
6781+
}
6782+
else if (Js::TaggedInt::Is(src1Var))
6783+
{
6784+
*result = (src1Var != reinterpret_cast<Js::Var>(Js::AtomTag_IntPtr));
6785+
}
6786+
#if FLOATVAR
6787+
else if (Js::JavascriptNumber::Is_NoTaggedIntCheck(src1Var))
6788+
{
6789+
double value = Js::JavascriptNumber::GetValue(src1Var);
6790+
*result = (!Js::JavascriptNumber::IsNan(value)) && (!Js::JavascriptNumber::IsZero(value));
6791+
}
6792+
#endif
6793+
else
67746794
{
67756795
return false;
67766796
}
6777-
*result = Js::JavascriptConversion::ToBoolean(src1Var, this->func->GetScriptContext());
6797+
67786798
if (instr->m_opcode == Js::OpCode::BrFalse_A)
67796799
{
67806800
*result = !(*result);

test/Optimizer/testsimplepathbrfold.baseline

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ Done bareq
4646
Done barnt
4747

4848
false
49+
TRACE PathDependentBranchFolding: Can prove retarget of branch in Block 1 from Block 3 to Block 4 in func barntbool
50+
TRACE PathDependentBranchFolding: Can prove retarget of branch in Block 2 from Block 3 to Block 5 in func barntbool
4951
false
5052
Done barntbool
5153

5254
true
55+
TRACE PathDependentBranchFolding: Can prove retarget of branch in Block 1 from Block 3 to Block 5 in func barfalse
56+
TRACE PathDependentBranchFolding: Can prove retarget of branch in Block 2 from Block 3 to Block 4 in func barfalse
5357
Done barfalse
5458

5559
5
@@ -58,6 +62,7 @@ TRACE PathDependentBranchFolding: Can prove retarget of branch in Block 1 from B
5862
Done iterator
5963

6064
5
65+
TRACE PathDependentBranchFolding: Can prove retarget of branch in Block 1 from Block 3 to Block 5 in func iterator2
6166
5
6267
Done iterator2
6368

test/PRE/pre1.baseline

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ TestTrace fieldcopyprop [in landing pad]: function inlinee ( (#1.3), #4) inlined
2222
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdFld field: count
2323
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdRootFld field: Direction
2424
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdFld field: FORWARD
25+
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdFld field: count
2526
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdRootFld field: Direction
2627
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdFld field: FORWARD
2728
undefined
@@ -32,6 +33,7 @@ TestTrace fieldcopyprop [in landing pad]: function inlinee ( (#1.3), #4) inlined
3233
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdFld field: count
3334
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdRootFld field: Direction
3435
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdFld field: FORWARD
36+
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdFld field: count
3537
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdRootFld field: Direction
3638
TestTrace fieldcopyprop: function inlinee ( (#1.3), #4) inlined caller function testInlined ( (#1.4), #5) opcode: LdFld field: FORWARD
3739
2001

0 commit comments

Comments
 (0)