-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
JitArm64: Use LogicalImm in boolX #12060
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,8 +323,10 @@ void JitArm64::boolX(UGeckoInstruction inst) | |
PanicAlertFmt("WTF!"); | ||
} | ||
} | ||
else if ((gpr.IsImm(s) && (gpr.GetImm(s) == 0 || gpr.GetImm(s) == 0xFFFFFFFF)) || | ||
(gpr.IsImm(b) && (gpr.GetImm(b) == 0 || gpr.GetImm(b) == 0xFFFFFFFF))) | ||
else if ((gpr.IsImm(s) && | ||
(gpr.GetImm(s) == 0 || gpr.GetImm(s) == 0xFFFFFFFF || LogicalImm(gpr.GetImm(s), 32))) || | ||
(gpr.IsImm(b) && | ||
(gpr.GetImm(b) == 0 || gpr.GetImm(b) == 0xFFFFFFFF || LogicalImm(gpr.GetImm(b), 32)))) | ||
{ | ||
int i, j; | ||
if (gpr.IsImm(s)) | ||
|
@@ -337,7 +339,6 @@ void JitArm64::boolX(UGeckoInstruction inst) | |
i = b; | ||
j = s; | ||
} | ||
bool is_zero = gpr.GetImm(i) == 0; | ||
|
||
bool complement_b = (inst.SUBOP10 == 60 /* andcx */) || (inst.SUBOP10 == 412 /* orcx */); | ||
const bool final_not = (inst.SUBOP10 == 476 /* nandx */) || (inst.SUBOP10 == 124 /* norx */); | ||
|
@@ -347,23 +348,39 @@ void JitArm64::boolX(UGeckoInstruction inst) | |
(inst.SUBOP10 == 124 /* norx */); | ||
const bool is_xor = (inst.SUBOP10 == 316 /* xorx */) || (inst.SUBOP10 == 284 /* eqvx */); | ||
|
||
u32 imm = gpr.GetImm(i); | ||
if ((complement_b && i == b) || (inst.SUBOP10 == 284 /* eqvx */)) | ||
{ | ||
is_zero = !is_zero; | ||
imm = ~imm; | ||
complement_b = false; | ||
} | ||
|
||
const bool is_zero = imm == 0; | ||
const bool is_ones = imm == 0xFFFFFFFF; | ||
// If imm can be represented as LogicalImm, so can ~imm. | ||
const auto log_imm = LogicalImm(imm, 32); | ||
|
||
if (is_xor) | ||
{ | ||
if (!is_zero) | ||
if (is_zero) | ||
{ | ||
gpr.BindToRegister(a, a == j); | ||
MVN(gpr.R(a), gpr.R(j)); | ||
if (a != j) | ||
{ | ||
gpr.BindToRegister(a, false); | ||
MOV(gpr.R(a), gpr.R(j)); | ||
} | ||
} | ||
else if (a != j) | ||
else | ||
{ | ||
gpr.BindToRegister(a, false); | ||
MOV(gpr.R(a), gpr.R(j)); | ||
gpr.BindToRegister(a, a == j); | ||
if (is_ones) | ||
{ | ||
MVN(gpr.R(a), gpr.R(j)); | ||
} | ||
else | ||
{ | ||
EOR(gpr.R(a), gpr.R(j), log_imm); | ||
} | ||
} | ||
if (inst.Rc) | ||
ComputeRC0(gpr.R(a)); | ||
|
@@ -376,45 +393,81 @@ void JitArm64::boolX(UGeckoInstruction inst) | |
if (inst.Rc) | ||
ComputeRC0(gpr.GetImm(a)); | ||
} | ||
else if (final_not || complement_b) | ||
else if (is_ones) | ||
{ | ||
gpr.BindToRegister(a, a == j); | ||
MVN(gpr.R(a), gpr.R(j)); | ||
if (final_not || complement_b) | ||
{ | ||
gpr.BindToRegister(a, a == j); | ||
MVN(gpr.R(a), gpr.R(j)); | ||
} | ||
else if (a != j) | ||
{ | ||
gpr.BindToRegister(a, false); | ||
MOV(gpr.R(a), gpr.R(j)); | ||
} | ||
if (inst.Rc) | ||
ComputeRC0(gpr.R(a)); | ||
} | ||
else | ||
{ | ||
if (a != j) | ||
if (!complement_b) | ||
{ | ||
gpr.BindToRegister(a, false); | ||
MOV(gpr.R(a), gpr.R(j)); | ||
gpr.BindToRegister(a, a == j); | ||
AND(gpr.R(a), gpr.R(j), log_imm); | ||
if (final_not) | ||
MVN(gpr.R(a), gpr.R(a)); | ||
} | ||
else | ||
{ | ||
// No shorter instruction sequence is possible. Just materialize the | ||
// immediate in a register as usual, so subsequent uses can leech off | ||
// of it. | ||
gpr.BindToRegister(a, (a == i) || (a == j)); | ||
BIC(gpr.R(a), gpr.R(i), gpr.R(j)); | ||
} | ||
if (inst.Rc) | ||
ComputeRC0(gpr.R(a)); | ||
} | ||
} | ||
else if (is_or) | ||
{ | ||
if (!is_zero) | ||
if (is_ones) | ||
{ | ||
gpr.SetImmediate(a, final_not ? 0 : 0xFFFFFFFF); | ||
if (inst.Rc) | ||
ComputeRC0(gpr.GetImm(a)); | ||
} | ||
else if (final_not || complement_b) | ||
else if (is_zero) | ||
{ | ||
gpr.BindToRegister(a, a == j); | ||
MVN(gpr.R(a), gpr.R(j)); | ||
if (final_not || complement_b) | ||
{ | ||
gpr.BindToRegister(a, a == j); | ||
MVN(gpr.R(a), gpr.R(j)); | ||
JosJuice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
else if (a != j) | ||
{ | ||
gpr.BindToRegister(a, false); | ||
MOV(gpr.R(a), gpr.R(j)); | ||
} | ||
if (inst.Rc) | ||
ComputeRC0(gpr.R(a)); | ||
} | ||
else | ||
{ | ||
if (a != j) | ||
if (!complement_b) | ||
{ | ||
gpr.BindToRegister(a, false); | ||
MOV(gpr.R(a), gpr.R(j)); | ||
gpr.BindToRegister(a, a == j); | ||
ORR(gpr.R(a), gpr.R(j), log_imm); | ||
if (final_not) | ||
MVN(gpr.R(a), gpr.R(a)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here regarding de Morgan's laws. |
||
} | ||
else | ||
{ | ||
// No shorter instruction sequence is possible. Just materialize the | ||
// immediate in a register as usual, so subsequent uses can leech off | ||
// of it. | ||
gpr.BindToRegister(a, (a == i) || (a == j)); | ||
ORN(gpr.R(a), gpr.R(i), gpr.R(j)); | ||
} | ||
if (inst.Rc) | ||
ComputeRC0(gpr.R(a)); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In the
final_not
case, we could make use of de Morgan's laws and turn ~(s & b) into ~s | ~b. Since inverting the immediate has no runtime cost, this would let us replace the AND+MVN with ORN. But inverting the immediate after we already havelog_imm
seems like effort... So I'll leave it up to you if you want to try implementing this in this PR or not.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.
Sorry, forgot about this for a while.
Not a bad idea. You can even use this approach for any immediate, not just those that can be expressed as LogicalImm. But you still need to materialize the immediate somehow and that might take more than one MOV instruction, in which case using LogicalImm might still be preferable...
I should also note that I haven't seen a single game use
nand
with immediates. And the only game that I've seen usenor
is Zelda Master Quest.So given the complexity and how uncommon these instruction patterns are, I think it would be better to leave this for a follow-up PR for now, if that's alright with you.