Skip to content

[M68k] Fix reverse BTST condition causing opposite failure/success logic #153086

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

Merged
merged 3 commits into from
Aug 20, 2025

Conversation

dansalvato
Copy link
Contributor

Given the test case:

define fastcc i16 @testbtst(i16 %a) nounwind {
  entry:
    switch i16 %a, label %no [
      i16 11, label %yes
      i16 10, label %yes
      i16 9, label %yes
      i16 4, label %yes
      i16 3, label %yes
      i16 2, label %yes
    ]

  yes:
    ret i16 1

  no:
    ret i16 0
}

We currently get this result:

testbtst:                               ; @testbtst
; %bb.0:                                ; %entry
	move.l	%d0, %d1
	and.l	#65535, %d1
	sub.l	#11, %d1
	bhi	.LBB0_3
; %bb.1:                                ; %entry
	and.l	#65535, %d0
	move.l	#3612, %d1
	btst	%d0, %d1
	bne	.LBB0_3        ; <------- Erroneous condition
; %bb.2:                                ; %yes
	moveq	#1, %d0
	rts
.LBB0_3:                                ; %no
	moveq	#0, %d0
	rts

The cause of this is a line that explicitly reverses the btst condition code. But on M68k, btst sets condition codes the same as and with a bitmask, meaning EQ indicates failure (bit is zero) and not success, so the condition does not need to be reversed.

In my testing, I've only been able to get switch statements to lower to btst, so I wasn't able to explicitly test other options for lowering. But (if possible to trigger) I believe they have the same logical error. For example, in LowerAndToBTST(), a comment specifies that it's lowering a case where the and result is compared against zero, which means the corresponding btst condition should also not be reversed.

This patch simply flips the ternary expression in getBitTestCondition() to match the ISD condition code with the same M68k code, instead of the opposite.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Aug 11, 2025

@llvm/pr-subscribers-backend-m68k

Author: Dan Salvato (dansalvato)

Changes

Given the test case:

define fastcc i16 @<!-- -->testbtst(i16 %a) nounwind {
  entry:
    switch i16 %a, label %no [
      i16 11, label %yes
      i16 10, label %yes
      i16 9, label %yes
      i16 4, label %yes
      i16 3, label %yes
      i16 2, label %yes
    ]

  yes:
    ret i16 1

  no:
    ret i16 0
}

We currently get this result:

testbtst:                               ; @<!-- -->testbtst
; %bb.0:                                ; %entry
	move.l	%d0, %d1
	and.l	#<!-- -->65535, %d1
	sub.l	#<!-- -->11, %d1
	bhi	.LBB0_3
; %bb.1:                                ; %entry
	and.l	#<!-- -->65535, %d0
	move.l	#<!-- -->3612, %d1
	btst	%d0, %d1
	bne	.LBB0_3        ; &lt;------- Erroneous condition
; %bb.2:                                ; %yes
	moveq	#<!-- -->1, %d0
	rts
.LBB0_3:                                ; %no
	moveq	#<!-- -->0, %d0
	rts

The cause of this is a line that explicitly reverses the btst condition code. But on M68k, btst sets condition codes the same as and with a bitmask, meaning EQ indicates failure (bit is zero) and not success, so the condition does not need to be reversed.

In my testing, I've only been able to get switch statements to lower to btst, so I wasn't able to explicitly test other options for lowering. But (if possible to trigger) I believe they have the same logical error. For example, in LowerAndToBTST(), a comment specifies that it's lowering a case where the and result is compared against zero, which means the corresponding btst condition should also not be reversed.

This patch simply flips the ternary expression in getBitTestCondition() to match the ISD condition code with the same M68k code, instead of the opposite.


Full diff: https://github.com/llvm/llvm-project/pull/153086.diff

1 Files Affected:

  • (modified) llvm/lib/Target/M68k/M68kISelLowering.cpp (+2-2)
diff --git a/llvm/lib/Target/M68k/M68kISelLowering.cpp b/llvm/lib/Target/M68k/M68kISelLowering.cpp
index 594ea9f48c201..cf666862b775d 100644
--- a/llvm/lib/Target/M68k/M68kISelLowering.cpp
+++ b/llvm/lib/Target/M68k/M68kISelLowering.cpp
@@ -1667,8 +1667,8 @@ static SDValue getBitTestCondition(SDValue Src, SDValue BitNo, ISD::CondCode CC,
 
   SDValue BTST = DAG.getNode(M68kISD::BTST, DL, MVT::i32, Src, BitNo);
 
-  // NOTE BTST sets CCR.Z flag
-  M68k::CondCode Cond = CC == ISD::SETEQ ? M68k::COND_NE : M68k::COND_EQ;
+  // NOTE BTST sets CCR.Z flag if bit is 0, same as AND with bitmask
+  M68k::CondCode Cond = CC == ISD::SETEQ ? M68k::COND_EQ : M68k::COND_NE;
   return DAG.getNode(M68kISD::SETCC, DL, MVT::i8,
                      DAG.getConstant(Cond, DL, MVT::i8), BTST);
 }

@dansalvato
Copy link
Contributor Author

Requesting review from @mshockwave

@mshockwave mshockwave self-requested a review August 12, 2025 15:43
Copy link
Member

@mshockwave mshockwave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good. The key here is that BEQ branches when Z is one, therefore if we're comparing against zero in the setcc we should not flip the condition.
Could you add a codegen test?

Add a CodeGen test case for a switch statement that emits BTST
Add a CodeGen test case for lowering AND mask to BTST
@dansalvato
Copy link
Contributor Author

I added a test that includes my example, and while doing that I also managed to get a second pattern to lower into BTST (AND with masked bit), so I included that test case as well. Both are branching correctly with this patch.

Copy link
Member

@mshockwave mshockwave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@mshockwave mshockwave merged commit 45e2c50 into llvm:main Aug 20, 2025
9 checks passed
Copy link

@dansalvato Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

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.

3 participants