[llvm] [GISel] Fix crash in GlobalISel utils method (PR #153334)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 20 05:43:53 PDT 2025


================
@@ -466,11 +466,13 @@ llvm::getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI) {
 std::optional<DefinitionAndSourceRegister>
 llvm::getDefSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI) {
   Register DefSrcReg = Reg;
-  auto *DefMI = MRI.getVRegDef(Reg);
-  auto &Opnd = DefMI->getOperand(0);
-  if (!Opnd.isReg())
-    return DefinitionAndSourceRegister{DefMI, DefSrcReg};
-  auto DstTy = MRI.getType(Opnd.getReg());
+  // This assumes that the code is in SSA form, so there should only be one
+  // definition.
+  if (MRI.def_empty(Reg))
+    return std::nullopt;
+  MachineOperand &DefOpnd = *MRI.def_begin(Reg);
----------------
arsenm wrote:

```suggestion
  auto DefIt = MRI.def_begin(Reg);
  if (DefIt == MRI.def_end())
    return {};
  MachineOperand &DefOpnd = *DefIt;
```

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


More information about the llvm-commits mailing list