Skip to content

[SelectionDAG] Prevent converting a virtual register to an MCRegister. #122857

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

Merged
merged 2 commits into from
Jan 14, 2025

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Jan 14, 2025

I believe the goal is that MCRegister is only for physical registers.

I believe the goal is that MCRegister is only for physical registers.
@topperc topperc requested review from arsenm and RKSimon January 14, 2025 05:10
@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Jan 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 14, 2025

@llvm/pr-subscribers-llvm-selectiondag

Author: Craig Topper (topperc)

Changes

I believe the goal is that MCRegister is only for physical registers.


Full diff: https://github.com/llvm/llvm-project/pull/122857.diff

1 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp (+50-46)
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index d64a90bcaae7da..c918ca3df7cd21 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -705,53 +705,57 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
     if (InstrRef)
       continue;
 
-    // If Reg is live-in then update debug info to track its copy in a vreg.
-    DenseMap<MCRegister, Register>::iterator LDI = LiveInMap.find(Reg);
-    if (LDI != LiveInMap.end()) {
-      assert(!hasFI && "There's no handling of frame pointer updating here yet "
-                       "- add if needed");
-      MachineInstr *Def = RegInfo->getVRegDef(LDI->second);
-      MachineBasicBlock::iterator InsertPos = Def;
-      const MDNode *Variable = MI->getDebugVariable();
-      const MDNode *Expr = MI->getDebugExpression();
-      DebugLoc DL = MI->getDebugLoc();
-      bool IsIndirect = MI->isIndirectDebugValue();
-      if (IsIndirect)
-        assert(MI->getDebugOffset().getImm() == 0 &&
-               "DBG_VALUE with nonzero offset");
-      assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
-             "Expected inlined-at fields to agree");
-      assert(MI->getOpcode() != TargetOpcode::DBG_VALUE_LIST &&
-             "Didn't expect to see a DBG_VALUE_LIST here");
-      // Def is never a terminator here, so it is ok to increment InsertPos.
-      BuildMI(*EntryMBB, ++InsertPos, DL, TII->get(TargetOpcode::DBG_VALUE),
-              IsIndirect, LDI->second, Variable, Expr);
-
-      // If this vreg is directly copied into an exported register then
-      // that COPY instructions also need DBG_VALUE, if it is the only
-      // user of LDI->second.
-      MachineInstr *CopyUseMI = nullptr;
-      for (MachineInstr &UseMI : RegInfo->use_instructions(LDI->second)) {
-        if (UseMI.isDebugValue())
-          continue;
-        if (UseMI.isCopy() && !CopyUseMI && UseMI.getParent() == EntryMBB) {
-          CopyUseMI = &UseMI;
-          continue;
+    if (Reg.isPhysical()) {
+      // If Reg is live-in then update debug info to track its copy in a vreg.
+      DenseMap<MCRegister, Register>::iterator LDI = LiveInMap.find(Reg);
+      if (LDI != LiveInMap.end()) {
+        assert(!hasFI &&
+               "There's no handling of frame pointer updating here yet "
+               "- add if needed");
+        MachineInstr *Def = RegInfo->getVRegDef(LDI->second);
+        MachineBasicBlock::iterator InsertPos = Def;
+        const MDNode *Variable = MI->getDebugVariable();
+        const MDNode *Expr = MI->getDebugExpression();
+        DebugLoc DL = MI->getDebugLoc();
+        bool IsIndirect = MI->isIndirectDebugValue();
+        if (IsIndirect)
+          assert(MI->getDebugOffset().getImm() == 0 &&
+                 "DBG_VALUE with nonzero offset");
+        assert(
+            cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
+            "Expected inlined-at fields to agree");
+        assert(MI->getOpcode() != TargetOpcode::DBG_VALUE_LIST &&
+               "Didn't expect to see a DBG_VALUE_LIST here");
+        // Def is never a terminator here, so it is ok to increment InsertPos.
+        BuildMI(*EntryMBB, ++InsertPos, DL, TII->get(TargetOpcode::DBG_VALUE),
+                IsIndirect, LDI->second, Variable, Expr);
+
+        // If this vreg is directly copied into an exported register then
+        // that COPY instructions also need DBG_VALUE, if it is the only
+        // user of LDI->second.
+        MachineInstr *CopyUseMI = nullptr;
+        for (MachineInstr &UseMI : RegInfo->use_instructions(LDI->second)) {
+          if (UseMI.isDebugValue())
+            continue;
+          if (UseMI.isCopy() && !CopyUseMI && UseMI.getParent() == EntryMBB) {
+            CopyUseMI = &UseMI;
+            continue;
+          }
+          // Otherwise this is another use or second copy use.
+          CopyUseMI = nullptr;
+          break;
+        }
+        if (CopyUseMI &&
+            TRI.getRegSizeInBits(LDI->second, MRI) ==
+                TRI.getRegSizeInBits(CopyUseMI->getOperand(0).getReg(), MRI)) {
+          // Use MI's debug location, which describes where Variable was
+          // declared, rather than whatever is attached to CopyUseMI.
+          MachineInstr *NewMI =
+              BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE), IsIndirect,
+                      CopyUseMI->getOperand(0).getReg(), Variable, Expr);
+          MachineBasicBlock::iterator Pos = CopyUseMI;
+          EntryMBB->insertAfter(Pos, NewMI);
         }
-        // Otherwise this is another use or second copy use.
-        CopyUseMI = nullptr;
-        break;
-      }
-      if (CopyUseMI &&
-          TRI.getRegSizeInBits(LDI->second, MRI) ==
-              TRI.getRegSizeInBits(CopyUseMI->getOperand(0).getReg(), MRI)) {
-        // Use MI's debug location, which describes where Variable was
-        // declared, rather than whatever is attached to CopyUseMI.
-        MachineInstr *NewMI =
-            BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE), IsIndirect,
-                    CopyUseMI->getOperand(0).getReg(), Variable, Expr);
-        MachineBasicBlock::iterator Pos = CopyUseMI;
-        EntryMBB->insertAfter(Pos, NewMI);
       }
     }
   }

CopyUseMI = &UseMI;
continue;
if (Reg.isPhysical()) {
// If Reg is live-in then update debug info to track its copy in a vreg.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should extract into its own function at this point

@topperc topperc merged commit b1edfa1 into llvm:main Jan 14, 2025
8 checks passed
@topperc topperc deleted the pr/selectiondag-mcregister branch January 14, 2025 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants