[llvm] [RFC][Transforms] Prefer unreachable insn over optimizaiton with undef (PR #131020)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 12 13:26:49 PDT 2025


================
@@ -1780,6 +1780,17 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
           LastStore = nullptr;
       }
     }
+
+    // If the ret insn returns an undefined (but not a poinson) value,
+    // change it to unreachable.
+    if (Inst.getOpcode() == Instruction::Ret) {
+      auto *RI = cast<ReturnInst>(&Inst);
+      auto *RetVal = RI->getReturnValue();
+      if (isa<UndefValue>(RetVal) && !isa<PoisonValue>(RetVal)) {
+        changeToUnreachable(&Inst, false, NULL, &*MSSAUpdater);
+        Changed = true;
+      }
+    }
----------------
nikic wrote:

This is also incorrect. A `ret undef` is perfectly legal by itself. It could be converted to unreachable if the return value has a noundef attribute. As written, the transform is incorrect.

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


More information about the llvm-commits mailing list