[llvm] [RISCV][TII] Add and use new hook to optimize/canonicalize instructions after MachineCopyPropagation (PR #137973)
Alex Bradbury via llvm-commits
llvm-commits at lists.llvm.org
Thu May 1 05:50:53 PDT 2025
================
@@ -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:
+ case RISCV::XOR:
+ // Normalize:
+ // [x]or rd, zero, rs => [x]or rd, rs, zero
+ if (MI.getOperand(1).getReg() == RISCV::X0) {
+ MachineOperand MO1 = MI.getOperand(1);
+ MI.removeOperand(1);
+ MI.addOperand(MO1);
+ }
----------------
asb wrote:
We do need to be careful of that, but note that the precondition for commuting the operands means that the following conditional is always true. i.e. it should not be possible to commute the instruction and return false.
I thought about a comment along these lines, but failed to come up with anything that didn't make it seem more mysterious or complicated than the reality. Do you think this needs explanation?
https://github.com/llvm/llvm-project/pull/137973
More information about the llvm-commits
mailing list