Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: ee963cf3db75e4ef1dfbdecdebce8fc7e5e52251
      
https://github.com/WebKit/WebKit/commit/ee963cf3db75e4ef1dfbdecdebce8fc7e5e52251
  Author: Sosuke Suzuki <[email protected]>
  Date:   2026-03-03 (Tue, 03 Mar 2026)

  Changed paths:
    A JSTests/stress/conditional-in-condition-context.js
    M Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp
    M Source/JavaScriptCore/parser/Nodes.h

  Log Message:
  -----------
  [JSC] Add emitBytecodeInConditionContext for ConditionalNode
https://bugs.webkit.org/show_bug.cgi?id=308512

Reviewed by Keith Miller.

When a ternary expression (a ? b : c) appears in a condition context
(e.g., if/while/for conditions, logical operator operands), the bytecode
generator previously fell back to 
ExpressionNode::emitBytecodeInConditionContext,
which materializes the ternary result into a temporary register and then
branches on it. This is redundant because the caller only needs to know
whether the result is truthy or falsy.

This patch adds ConditionalNode::emitBytecodeInConditionContext, which
propagates the condition context directly to m_expr1 and m_expr2,
allowing them to jump straight to the caller's true/false targets
without materializing an intermediate value.

For `if (a ? b : c) return 1; return 0;`, before:

    enter
    jfalse  arg1, @else
    mov     loc5, arg2          // materialize b into temp
    jmp     @afterTernary
  @else:
    mov     loc5, arg3          // materialize c into temp
  @afterTernary:
    jfalse  loc5, @ifFalse      // redundant branch on temp
    ret     1
  @ifFalse:
    ret     0

After:

    enter
    jfalse  arg1, @else
    jfalse  arg2, @ifFalse      // branch directly on b
    jmp     @end
  @else:
    jfalse  arg3, @ifFalse      // branch directly on c
  @end:
    ret     1
  @ifFalse:
    ret     0

This eliminates 1 instruction and 1 temporary register in the basic
case. The optimization also chains recursively with existing condition
context nodes: ConstantNode (e.g., `a ? true : false` saves 2
instructions), LogicalOpNode (e.g., `a ? b && c : d || e` saves 3
instructions), LogicalNotNode, and nested ConditionalNodes.

Test: JSTests/stress/conditional-in-condition-context.js

* JSTests/stress/conditional-in-condition-context.js: Added.
(shouldBe):
(testBasic):
(testConstantThen):
(testConstantElse):
(testLogicalNot):
(testLogicalAnd):
(testNested):
(testWhile):
(testFor):
(testLogicalOperand):
(testLogicalOr):
(testDoWhile):
* Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:
(JSC::ConditionalNode::emitBytecodeInConditionContext):
* Source/JavaScriptCore/parser/Nodes.h:

Canonical link: https://commits.webkit.org/308606@main



To unsubscribe from these emails, change your notification settings at 
https://github.com/WebKit/WebKit/settings/notifications

Reply via email to