-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[RISCV][TII] Add and use new hook to optimize/canonicalize instructions after MachineCopyPropagation #137973
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
[RISCV][TII] Add and use new hook to optimize/canonicalize instructions after MachineCopyPropagation #137973
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2ad6cc8
[RISCV][TII] Add and use new hook fo optmize/canonicalize instruction…
asb d23c66c
Add changes missed in first push
asb 5d00e6f
Fix errors in some patterns and remove PACK/PACKW
asb 3048ba9
Use commuteInstruction helper
asb 7da3f92
Move optimizeInstructoin to after the preprocessor undefs
asb 109a5ba
(cherry-pick) [RISCV][NFC] Add missed // clang-format on
asb a02dc45
Reformat now issue with clang-format being disabled was fixed
asb 8e73913
Consistently avoid use of pseudoinstructions in comments for transforms
asb 1c8715d
Pull optimizeInstruction call outside of loop
asb 825b1f6
Simplify and/mul* case as suggested in review
asb 9500cee
Re-order instructions in switch
asb bb9e2c1
clang-format
asb 3f2049f
reorder comment to match code logic
asb 47ca1a0
generalise sltiu check to any non-zero immediate
asb ff42dca
Add sll/srl/sra mv-like case
asb dc4b2e5
handle sltiu rd, zero, 0
asb c765d58
Add suggested comments for 'normalize' pre-transforms
asb 3ee7da3
Get rid of redundant newline
asb 436a5e6
Set Changed=true if optimizeInstruction returned true in call from MCP
asb e2a1be0
Cover ADD as well
asb 44f6605
Rename hook to simplifyInstruction
asb c93404a
Merge remote-tracking branch 'origin/main' into 2025q2-riscv-optimize…
asb 19f4dc1
Add comment to .mir file
asb d6f8428
Rename test
asb 920f408
Merge remote-tracking branch 'origin/main' into 2025q2-riscv-optimize…
asb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2344,6 +2344,21 @@ static unsigned getSHXADDShiftAmount(unsigned Opc) { | |
| } | ||
| } | ||
|
|
||
| // Returns the shift amount from a SHXADD.UW instruction. Returns 0 if the | ||
| // instruction is not a SHXADD.UW. | ||
| static unsigned getSHXADDUWShiftAmount(unsigned Opc) { | ||
preames marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| switch (Opc) { | ||
| default: | ||
| return 0; | ||
| case RISCV::SH1ADD_UW: | ||
| return 1; | ||
| case RISCV::SH2ADD_UW: | ||
| return 2; | ||
| case RISCV::SH3ADD_UW: | ||
| return 3; | ||
| } | ||
| } | ||
|
|
||
| // Look for opportunities to combine (sh3add Z, (add X, (slli Y, 5))) into | ||
| // (sh3add (sh2add Y, Z), X). | ||
| static bool getSHXADDPatterns(const MachineInstr &Root, | ||
|
|
@@ -3850,6 +3865,207 @@ MachineInstr *RISCVInstrInfo::commuteInstructionImpl(MachineInstr &MI, | |
| return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); | ||
| } | ||
|
|
||
| bool RISCVInstrInfo::optimizeInstruction(MachineInstr &MI) const { | ||
| switch (MI.getOpcode()) { | ||
| default: | ||
| break; | ||
| case RISCV::OR: | ||
preames marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case RISCV::XOR: | ||
| // Normalize: | ||
preames marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // [x]or rd, zero, rs => [x]or rd, rs, zero | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
| MachineOperand MO1 = MI.getOperand(1); | ||
asb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| MI.removeOperand(1); | ||
| MI.addOperand(MO1); | ||
| } | ||
asb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // [x]or rd, rs, zero => addi rd, rs, 0 | ||
| if (MI.getOperand(2).getReg() == RISCV::X0) { | ||
| MI.getOperand(2).ChangeToImmediate(0); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| // xor rd, rs, rs => li rd, 0 | ||
| if (MI.getOpcode() == RISCV::XOR && MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) { | ||
| MI.getOperand(1).setReg(RISCV::X0); | ||
| MI.getOperand(2).ChangeToImmediate(0); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::ADDW: | ||
| // Normalize: | ||
| // addw rd, zero, rs => addw rd, rs, zero | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
asb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| MachineOperand MO1 = MI.getOperand(1); | ||
| MI.removeOperand(1); | ||
| MI.addOperand(MO1); | ||
| } | ||
| // addw rd, rs, zero => addiw rd, rs, 0 | ||
| if (MI.getOperand(2).getReg() == RISCV::X0) { | ||
| MI.getOperand(2).ChangeToImmediate(0); | ||
| MI.setDesc(get(RISCV::ADDIW)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::SUB: | ||
| // sub rd, rs, zero => addi rd, rs, 0 | ||
| if (MI.getOperand(2).getReg() == RISCV::X0) { | ||
| MI.getOperand(2).ChangeToImmediate(0); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::SUBW: | ||
| // subw rd, rs, zero => addiw rd, rs, 0 | ||
| if (MI.getOperand(2).getReg() == RISCV::X0) { | ||
| MI.getOperand(2).ChangeToImmediate(0); | ||
| MI.setDesc(get(RISCV::ADDIW)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::SH1ADD: | ||
| case RISCV::SH1ADD_UW: | ||
| case RISCV::SH2ADD: | ||
| case RISCV::SH2ADD_UW: | ||
| case RISCV::SH3ADD: | ||
| case RISCV::SH3ADD_UW: | ||
| // shNadd[.uw] rd, zero, rs => addi rd, rs, 0 | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
| MI.removeOperand(1); | ||
preames marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| MI.addOperand(MachineOperand::CreateImm(0)); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| // shNadd[.uw] rd, rs, zero => slli[.uw] rd, rs, N | ||
| if (MI.getOperand(2).getReg() == RISCV::X0) { | ||
| MI.removeOperand(2); | ||
| unsigned Opc = MI.getOpcode(); | ||
| if (Opc == RISCV::SH1ADD_UW || Opc == RISCV::SH2ADD_UW || Opc == RISCV::SH3ADD_UW) { | ||
|
||
| MI.addOperand(MachineOperand::CreateImm(getSHXADDUWShiftAmount(Opc))); | ||
| MI.setDesc(get(RISCV::SLLI_UW)); | ||
| return true; | ||
| } | ||
| MI.addOperand(MachineOperand::CreateImm(getSHXADDShiftAmount(Opc))); | ||
| MI.setDesc(get(RISCV::SLLI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::ANDI: | ||
| // andi rd, zero, C => li rd, 0 | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
| MI.getOperand(2).setImm(0); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::AND: | ||
| case RISCV::MUL: | ||
| case RISCV::MULH: | ||
| case RISCV::MULHSU: | ||
| case RISCV::MULHU: | ||
| case RISCV::MULW: | ||
| // and rd, rs, zero => li rd, 0 | ||
| // and rd, zero, rs => li rd, 0 | ||
| // mul* rd, rs, zero => li rd, 0 | ||
| // mul* rd, zero, rs => li rd, 0 | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
asb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| MI.removeOperand(2); | ||
| MI.addOperand(MachineOperand::CreateImm(0)); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| if (MI.getOperand(2).getReg() == RISCV::X0) { | ||
| MI.removeOperand(1); | ||
| MI.addOperand(MachineOperand::CreateImm(0)); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::SLLI: | ||
| case RISCV::SRLI: | ||
| case RISCV::SRAI: | ||
| case RISCV::SLLIW: | ||
| case RISCV::SRLIW: | ||
| case RISCV::SRAIW: | ||
| case RISCV::SLLI_UW: | ||
| // shiftimm rd, zero, N => li rd, 0 | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
| MI.getOperand(2).setImm(0); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::ORI: | ||
preames marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case RISCV::XORI: | ||
| // [x]ori rd, zero, N => li rd, N | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::SLTIU: | ||
| // seqz rd, zero => li rd, 1 | ||
preames marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (MI.getOperand(1).getReg() == RISCV::X0 && MI.getOperand(2).getImm() == 1) { | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::SLTU: | ||
| case RISCV::ADD_UW: | ||
| // snez rd, zero => li rd, 0 | ||
asb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // zext.w rd, zero => li rd, 0 | ||
| if (MI.getOperand(1).getReg() == RISCV::X0 && MI.getOperand(2).getReg() == RISCV::X0) { | ||
| MI.getOperand(2).ChangeToImmediate(0); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| // add.uw rd, zero, rs => addi rd, rs, 0 | ||
| if (MI.getOpcode() == RISCV::ADD_UW && MI.getOperand(1).getReg() == RISCV::X0) { | ||
| MI.removeOperand(1); | ||
| MI.addOperand(MachineOperand::CreateImm(0)); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| } | ||
| break; | ||
| case RISCV::SEXT_H: | ||
| case RISCV::SEXT_B: | ||
| case RISCV::ZEXT_H_RV32: | ||
| case RISCV::ZEXT_H_RV64: | ||
| // sext.[hb] rd, zero => li rd, 0 | ||
| // zext.h rd, zero => li rd, 0 | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
| MI.addOperand(MachineOperand::CreateImm(0)); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::SLL: | ||
| case RISCV::SRL: | ||
| case RISCV::SRA: | ||
preames marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case RISCV::SLLW: | ||
| case RISCV::SRLW: | ||
| case RISCV::SRAW: | ||
| // shift rd, zero, rs => li rd, 0 | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
| MI.getOperand(2).ChangeToImmediate(0); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::MIN: | ||
| case RISCV::MINU: | ||
| case RISCV::MAX: | ||
| case RISCV::MAXU: | ||
| // min|max rd, rs, rs => addi rd, rs, 0 | ||
| if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) { | ||
| MI.getOperand(2).ChangeToImmediate(0); | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| #undef CASE_RVV_OPCODE_UNMASK_LMUL | ||
| #undef CASE_RVV_OPCODE_MASK_LMUL | ||
| #undef CASE_RVV_OPCODE_LMUL | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
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.
Name is way too general for a very narrowly applied optimization. Can't this just go in a separate post-RA pass
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.
@asb Can you say a bit about why this needs to be in MCP? As opposed to just after MCP? Does doing this in the process of copy propagation expose additional copies? (Not implausible, but do you actually see this?)
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.
The problem this patch is solving is probably not unique to RISC-V. Having the ability for other targets to do this type of canonicalization seems like a good idea.
Are we suggesting a separate target independent pass using this hook? Or a target specific pass?
Uh oh!
There was an error while loading. Please reload this page.
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.
Craig's comment captures well the reason why I looked to do this in MCP. it seems like a transformation that's generally useful to other targets, and by adding and using a TII hook it's easy to reuse if we find any other target-specific or generic passes that might benefit (though as I say in the summary, all of the instances of these instructions I can find come from MCP). It could be done in another pass, but it felt like a simple change to have MCP "check its own work" might be cleaner than having yet another pass.
This specific change doesn't induce additional copy propagation, but looking at the generated diffs there are cases where you would expect copy propagation should be able to run again. I nod to this in the PR description above, but as this change alone is an improvement on before I leave that to a followup.
e.g. this snippet from imagick:
The slli/srli should be removable (canonicalised to loadimm of 0, and then redundant with the previous
li). I haven't yet stepped through to see if this is a matter of doing another iteration in MCP or if there's some other barrier that prevents current MCP from handling the case.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.
This is something that can constant fold, but didn't earlier? At this point I would hope that would have been cleaned long before this
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.
This specific example does look weird. From offline conversation, I'd thought this was mostly catching cleanup after tail duplication, but this looks like some kind of loop structure?
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.
Oh I see the cause of confusion - this is indeed cleaning up after tail duplication - the use of the TailDuplicator utility class (llvm/lib/CodeGen/TailDuplicator.cpp) in MachineBlockPlacement.
Here is a roughly reduced example representing the above snippet:
If you run that through
llc -O3you'll see the tail duplication happening as part of MachineBlockPlacement and then MCP runs.So you have a block:
After tail duplication is applied in MBP you get:
Where obviously bb.2 can be cleaned up which MCP does to a certain extent.