[llvm] [TailDup][MachineSSAUpdater] Let RewriteUse insert a COPY when needed (PR #95553)

Björn Pettersson via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 19 09:00:42 PDT 2024


================
@@ -236,6 +236,19 @@ void MachineSSAUpdater::RewriteUse(MachineOperand &U) {
     NewVR = GetValueInMiddleOfBlock(UseMI->getParent());
   }
 
+  // Insert a COPY if register class contraint isn't fulfilled.
+  if (const TargetRegisterClass *RC =
+          dyn_cast_or_null<const TargetRegisterClass *>(RegAttrs.RCOrRB)) {
+    if (NewVR && !RC->hasSubClassEq(MRI->getRegClass(NewVR))) {
+      MachineBasicBlock *UseBB = UseMI->getParent();
+      MachineInstr *InsertedCopy =
+          InsertNewDef(TargetOpcode::COPY, UseBB, UseBB->getFirstNonPHI(),
----------------
bjope wrote:

I've changed this to first try to constrain the class of NewVR using constrainRegClass. So we only add a COPY if it couldn't be constrained.

I think it is a bit hard to reason about if constraining the class is better then adding a COPY here. It probably depends on the live range and other uses of NewVR, and if passes between early-tail-duplication and the regular register coalescing would suffer from the COPY. But I also don't think it is the responsibility of tail duplication to make a deep analysis of that (or else we would need to duplicate a lot of the logic in the register coalescer/allocator here).

I also intentianally just use the original RC for the MO we are rewriting as input to the constrainRegClass. Another option would be to find the register class defined for the actual machine instruction. But then we would need to consider potential subregister uses as well. I don't think it is worth the complexity. (One could perhaps use recomputeRegClass in some more places, but afaik that is done later by the RegisterCoalescer anyway.)

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


More information about the llvm-commits mailing list