diff --git a/llvm/lib/CodeGen/MIRParser/MIParser.cpp b/llvm/lib/CodeGen/MIRParser/MIParser.cpp --- a/llvm/lib/CodeGen/MIRParser/MIParser.cpp +++ b/llvm/lib/CodeGen/MIRParser/MIParser.cpp @@ -1011,10 +1011,6 @@ Optional TiedDefIdx; if (parseMachineOperandAndTargetFlags(OpCode, Operands.size(), MO, TiedDefIdx)) return true; - if ((OpCode == TargetOpcode::DBG_VALUE || - OpCode == TargetOpcode::DBG_VALUE_LIST) && - MO.isReg()) - MO.setIsDebug(); Operands.push_back( ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx)); if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) || diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp --- a/llvm/lib/CodeGen/MachineInstr.cpp +++ b/llvm/lib/CodeGen/MachineInstr.cpp @@ -294,6 +294,9 @@ if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1) NewMO->setIsEarlyClobber(true); } + // Ensure DBG_VALUE* sets RegState::Debug on all register use operands. + if (NewMO->isUse() && isDebugValue()) + NewMO->setIsDebug(); } } diff --git a/llvm/lib/CodeGen/MachineOperand.cpp b/llvm/lib/CodeGen/MachineOperand.cpp --- a/llvm/lib/CodeGen/MachineOperand.cpp +++ b/llvm/lib/CodeGen/MachineOperand.cpp @@ -250,6 +250,11 @@ if (RegInfo && WasReg) RegInfo->removeRegOperandFromUseList(this); + // Ensure DBG_VALUE* sets RegState::Debug on all register use operands. + const MachineInstr *MI = getParent(); + if (!isDef && MI && MI->isDebugValue()) + isDebug = true; + // Change this to a register and set the reg#. assert(!(isDead && !isDef) && "Dead flag on non-def"); assert(!(isKill && isDef) && "Kill flag on def"); diff --git a/llvm/unittests/CodeGen/MachineInstrTest.cpp b/llvm/unittests/CodeGen/MachineInstrTest.cpp --- a/llvm/unittests/CodeGen/MachineInstrTest.cpp +++ b/llvm/unittests/CodeGen/MachineInstrTest.cpp @@ -386,6 +386,30 @@ ASSERT_FALSE(MI->getHeapAllocMarker()); } +TEST(MachineInstrDebugValue, AddDebugValueOperand) { + LLVMContext Ctx; + Module Mod("Module", Ctx); + auto MF = createMachineFunction(Ctx, Mod); + + for (const unsigned short OpCode : + {TargetOpcode::DBG_VALUE, TargetOpcode::DBG_VALUE_LIST}) { + const MCInstrDesc MCID = { + OpCode, 0, 0, + 0, 0, (1ULL << MCID::Pseudo) | (1ULL << MCID::Variadic), + 0, nullptr, nullptr, + nullptr}; + + auto MI = MF->CreateMachineInstr(MCID, DebugLoc()); + MI->addOperand(*MF, MachineOperand::CreateReg(0, /*isDef*/ false)); + + MI->addOperand(*MF, MachineOperand::CreateImm(0)); + MI->getOperand(1).ChangeToRegister(0, false); + + ASSERT_TRUE(MI->getOperand(0).isDebug()); + ASSERT_TRUE(MI->getOperand(1).isDebug()); + } +} + static_assert(std::is_trivially_copyable::value, "trivially copyable");