-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[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
Conversation
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 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. |
@llvm/pr-subscribers-backend-m68k Author: Dan Salvato (dansalvato) ChangesGiven 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 In my testing, I've only been able to get switch statements to lower to This patch simply flips the ternary expression in Full diff: https://github.com/llvm/llvm-project/pull/153086.diff 1 Files Affected:
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);
}
|
Requesting review from @mshockwave |
There was a problem hiding this 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
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks
@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! |
Given the test case:
We currently get this result:
The cause of this is a line that explicitly reverses the
btst
condition code. But on M68k,btst
sets condition codes the same asand
with a bitmask, meaningEQ
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, inLowerAndToBTST()
, a comment specifies that it's lowering a case where theand
result is compared against zero, which means the correspondingbtst
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.