[llvm] [RegisterCoalescer] Don't commute two-address instructions which only define a subregister (PR #169031)

via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 1 01:59:34 PST 2025


================
@@ -869,6 +869,14 @@ RegisterCoalescer::removeCopyByCommutingDef(const CoalescerPair &CP,
   if (!DefMI->isRegTiedToUseOperand(DefIdx, &UseOpIdx))
     return {false, false};
 
+  // If DefMI only defines the register partially, we can't replace uses of the
+  // full register with the new destination register after commuting it.
+  if (IntA.reg().isVirtual() &&
+      none_of(DefMI->all_defs(), [&](const MachineOperand &DefMO) {
----------------
KRM7 wrote:

> Can you move this down where the DstOperand is handled?

Do you mean the part below the FIXME? Those checks are for what will be the dst operand after commuting the instruction, while this is checking the original dst, so this seemed like the more logical place.

Regarding the multiple defs, I don't see anything in the code here that would assume a single def. There are also some tests where the commuted instruction has multiple defs, in `X86/coalesce-commutative-implicit-def.mir` (though only 1 is explicit). Without checking all the defs, there would be regressions in those test cases.

https://github.com/llvm/llvm-project/pull/169031


More information about the llvm-commits mailing list