[llvm] [CodeGen] Do not remove IMPLICIT_DEF unless all uses have undef flag added (PR #188133)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 31 09:47:52 PDT 2026
================
@@ -102,38 +102,61 @@ void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
}
// This is a physreg implicit-def.
- // Look for the first instruction to use or define an alias.
- MachineBasicBlock::instr_iterator UserMI = MI->getIterator();
- MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
- bool Found = false;
- for (++UserMI; UserMI != UserE; ++UserMI) {
- for (MachineOperand &MO : UserMI->operands()) {
+ // Trim any extra operands.
+ for (unsigned i = MI->getNumOperands() - 1; i; --i)
+ MI->removeOperand(i);
+
+ // Try to add undef flag to all uses. If all uses are updated remove
+ // implicit-def.
+ MachineBasicBlock::instr_iterator SearchMI = MI->getIterator();
+ MachineBasicBlock::instr_iterator SearchE = MI->getParent()->instr_end();
+ bool ImplicitDefIsDead = false;
+ bool SearchedWholeBlock = true;
+ constexpr unsigned SearchLimit = 35;
+ unsigned Count = 0;
+ for (++SearchMI; SearchMI != SearchE; ++SearchMI) {
+ if (SearchMI->isDebugInstr())
+ continue;
+ if (++Count > SearchLimit) {
+ SearchedWholeBlock = false;
+ break;
+ }
+ for (MachineOperand &MO : SearchMI->operands()) {
if (!MO.isReg())
continue;
- Register UserReg = MO.getReg();
- if (!UserReg.isPhysical() || !TRI->regsOverlap(Reg, UserReg))
+ Register SearchReg = MO.getReg();
+ if (!SearchReg.isPhysical() || !TRI->regsOverlap(Reg, SearchReg))
continue;
- // UserMI uses or redefines Reg. Set <undef> flags on all uses.
- Found = true;
- if (MO.isUse())
- MO.setIsUndef();
+ // SearchMI uses or redefines Reg. Set <undef> flags on all uses.
+ if (MO.isUse()) {
+ if (TRI->isSubRegisterEq(Reg, SearchReg))
+ MO.setIsUndef();
+ else
+ // Use is larger than Reg. It is not safe to add undef to this use.
+ return;
+ }
+ if (MO.isDef()) {
+ if (TRI->isSubRegisterEq(SearchReg, Reg))
+ ImplicitDefIsDead = true;
+ else
----------------
LU-JOHN wrote:
Added.
https://github.com/llvm/llvm-project/pull/188133
More information about the llvm-commits
mailing list