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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 9 09:14:35 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:

You'll still want to explicitly set the location to dropped here to not run afoul of verification (https://discourse.llvm.org/t/rfc-require-real-or-annotated-source-locations-on-all-instructions/86816).

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


More information about the llvm-commits mailing list