[llvm] [CodeGen] Correctly handle non-standard cases in RemoveLoadsIntoFakeUses (PR #111551)

Stephen Tozer via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 8 14:44:36 PDT 2024


================
@@ -109,31 +112,38 @@ bool RemoveLoadsIntoFakeUses::runOnMachineFunction(MachineFunction &MF) {
       // reload of a spilled register.
       if (MI.getRestoreSize(TII)) {
         Register Reg = MI.getOperand(0).getReg();
-        assert(Reg.isPhysical() && "VReg seen in function with NoVRegs set?");
         // Don't delete live physreg defs, or any reserved register defs.
         if (!LivePhysRegs.available(Reg) || MRI->isReserved(Reg))
           continue;
-        // There should be an exact match between the loaded register and the
-        // FAKE_USE use. If not, this is a load that is unused by anything? It
-        // should probably be deleted, but that's outside of this pass' scope.
-        if (RegFakeUses.contains(Reg)) {
+        // There should typically be an exact match between the loaded register
+        // and the FAKE_USE, but sometimes regalloc will choose to load a larger
+        // value than is needed. Therefore, as long as the load isn't used by
+        // anything except at least one FAKE_USE, we will delete it. If it isn't
+        // used by any fake uses, it should still be safe to delete but we
+        // choose to ignore it so that this pass has no side effects unrelated
+        // to fake uses.
+        SmallDenseSet<MachineInstr *> FakeUsesToDelete;
+        for (MCRegUnit Unit : TRI->regunits(Reg)) {
+          if (!RegFakeUses.contains(Unit))
+            continue;
+          for (MachineInstr *FakeUse : RegFakeUses[Unit])
+            FakeUsesToDelete.insert(FakeUse);
+          RegFakeUses.erase(Unit);
----------------
SLTozer wrote:

We don't want to erase any FAKE_USE here unless we know we're deleting the load which feeds into it; we only delete the FAKE_USEs here since leaving it around after the load is deleted would make it a fake use of the wrong value.

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


More information about the llvm-commits mailing list