Skip to content

[BranchFolding] Add an optional target hook to skip branch folding when it's unsafe #147029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

csstormq
Copy link
Contributor

@csstormq csstormq commented Jul 4, 2025

Assume the indirect branch implementation is as follows:

mov r0.lo BB0.label_lo
mov r0.hi BB0.label_hi
add r2 r1 r0
jr r2

Here, the base address and offset are stored in r0  and r1 respectively, with r2 = r1 + r0 as the jump target. Analyzing the target of the indirect branch jr  is often challenging, particularly when register r2 is spilled.

When basic block BB0 is empty, BranchFolding may optimize it, causing a dangling label. To address this, the target can override  isMBBSafeToBranchFolding  and return  false.

This issue was found in an out-of-tree target, hence the lack of testcase in this commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants