-
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
[RISCV][TII] Add and use new hook to optimize/canonicalize instructions after MachineCopyPropagation #137973
Changes from 8 commits
2ad6cc8
d23c66c
5d00e6f
3048ba9
7da3f92
109a5ba
a02dc45
8e73913
1c8715d
825b1f6
9500cee
bb9e2c1
3f2049f
47ca1a0
ff42dca
dc4b2e5
c765d58
3ee7da3
436a5e6
e2a1be0
44f6605
c93404a
19f4dc1
d6f8428
920f408
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
|
|
@@ -3734,6 +3749,7 @@ bool RISCVInstrInfo::findCommutedOpIndices(const MachineInstr &MI, | |
| CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP, NEWOP, VFPR16, E16) \ | ||
| CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, VFPR32, E32) \ | ||
| CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, VFPR64, E64) | ||
| // clang-format on | ||
|
|
||
| MachineInstr *RISCVInstrInfo::commuteInstructionImpl(MachineInstr &MI, | ||
| bool NewMI, | ||
|
|
@@ -3872,6 +3888,206 @@ MachineInstr *RISCVInstrInfo::commuteInstructionImpl(MachineInstr &MI, | |
| #undef CASE_VFMA_OPCODE_VV | ||
| #undef CASE_VFMA_SPLATS | ||
|
|
||
| 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: | ||
| // [x]or rd, zero, rs => [x]or rd, rs, zero | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) | ||
| commuteInstruction(MI); | ||
| // [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 => addi rd, zero, 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) | ||
| commuteInstruction(MI); | ||
| // 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 | ||
|
Collaborator
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. Does
Contributor
Author
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. It does show up, but we do have a a CompressPat that produces c.mv for either case. So there's no real difference if C is enabled, and I've been running my script on build directories that have C enabled. But there's no reason not to handle ADD here, and in the case of a target without the C extension it at least means you'll get a canonical |
||
| 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); | ||
| 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 => addi rd, zero, 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 => addi rd, zero, 0 | ||
asb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // and rd, zero, rs => addi rd, zero, 0 | ||
| // mul* rd, rs, zero => addi rd, zero, 0 | ||
| // mul* rd, zero, rs => addi rd, zero, 0 | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
| 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 => addi rd, zero, 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 => addi rd, zero, N | ||
| if (MI.getOperand(1).getReg() == RISCV::X0) { | ||
| MI.setDesc(get(RISCV::ADDI)); | ||
| return true; | ||
| } | ||
| break; | ||
| case RISCV::SLTIU: | ||
| // sltiu rd, zero, 1 => addi rd, zero, 1 | ||
| 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: | ||
| // sltu rd, zero, zero => addi rd, zero, 0 | ||
| // add.uw rd, zero, zero => addi rd, zero, 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 => addi rd, zero, 0 | ||
| // zext.h rd, zero => addi rd, zero, 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 => addi rd, zero, 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; | ||
| } | ||
|
|
||
| // clang-format off | ||
| #define CASE_WIDEOP_OPCODE_COMMON(OP, LMUL) \ | ||
| RISCV::PseudoV##OP##_##LMUL##_TIED | ||
|
|
||
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.