[llvm] [licm] clone metadata when hoisting conditional branch (PR #152232)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 6 13:41:59 PDT 2025


================
@@ -857,9 +859,18 @@ class ControlFlowHoister {
     }
 
     // Now finally clone BI.
-    ReplaceInstWithInst(
-        HoistTarget->getTerminator(),
-        BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition()));
+    auto *NewBI =
+        BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition(),
+                           HoistTarget->getTerminator()->getIterator());
+    HoistTarget->getTerminator()->eraseFromParent();
+    // Copy all the metadata. In particular:
+    // - debug info (critical to Sample-based profiling) should be the same
+    // as the original branch, not that of HoistTarget->getTerminator(),
+    // which is what ReplaceInstWithInst would use.
----------------
nikic wrote:

Hm, looking at https://llvm.org/docs/HowToUpdateDebugInfo.html#when-to-preserve-an-instruction-location again, it specifically calls out LICM as a case where the debug location should be dropped, not preserved.

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


More information about the llvm-commits mailing list