[llvm] [CodeGen] Do not remove IMPLICIT_DEF unless all uses have undef flag added (PR #188133)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 27 04:25:57 PDT 2026


================
@@ -102,29 +102,44 @@ 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()) {
+  // 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 = 60000;
+  unsigned Count = 0;
+  for (++SearchMI; SearchMI != SearchE; ++SearchMI) {
+    bool DefinesReg = false;
+    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;
+      // SearchMI uses or redefines Reg. Set <undef> flags on all uses.
       if (MO.isUse())
         MO.setIsUndef();
----------------
jayfoad wrote:

Pre-existing issue but I am not sure this is safe if SearchReg is larger than Reg. E.g. after an implicit def of %al, this would mark a use of %eax as undef. But that seems wrong because not _all_ of %eax was undefined.

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


More information about the llvm-commits mailing list