Skip to content

fix: don't re-invert a shared 'if' condition in a duplicated block#2907

Open
obus-globus wants to merge 1 commit into
skylot:masterfrom
obus-globus:fix/duplicated-block-invert
Open

fix: don't re-invert a shared 'if' condition in a duplicated block#2907
obus-globus wants to merge 1 commit into
skylot:masterfrom
obus-globus:fix/duplicated-block-invert

Conversation

@obus-globus

Copy link
Copy Markdown
Contributor

NOTE: This is what one may call "AI slop". Feel free to close this PR if it's not up to your quality standards or even if you just aren't interested in reviewing it. I use these changes downstream on an APK and thought I might as well make a PR offering to upstream them, as your CONTRIBUTING.md doesn't say anything discouraging this.

Description

Demonstrated on the java/class input path. A condition that heads a block belonging to more than one region is inverted one extra time, flipping its first operand. For

static int test(boolean a, boolean b, boolean x, boolean y) {
    int r = 2;
    if (a || b) {
        r = (x || y) ? 1 : 0;
    }
    return r;
}

the inner (x || y) decompiled as (!x || y): the first operand picked up a spurious negation.

IfRegionMaker.process inverts a simple condition in place: IfInfo.invert (via IfCondition.invert) calls Compare.invert, which mutates the block's shared IfNode in place. The (x || y) block heads a merged short-circuit condition and is also reached from two predecessors (AFlag.DUPLICATED), so it is processed as a region head more than once. The DONT_INVERT guard that prevents a second in-place inversion was only set on the simple-invert path, not when the condition was built by mergeNestedIfNodes, so the later standalone pass inverted the already-built condition again and flipped its first operand.

Set AFlag.DONT_INVERT after both the merge and the simple-invert branches, so a shared block's condition is inverted at most once regardless of how it was built.

Includes an integration test.

@obus-globus
obus-globus force-pushed the fix/duplicated-block-invert branch from 355cf98 to b27565a Compare July 4, 2026 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant