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

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 7 20:47:24 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.
----------------
mtrofin wrote:

I see. How about this:
- we add a flag like `-dbginfo-prefer-profiling` or something of the kind
- ideally, we default that based on optimization level, like -O2/3 make the preference default to that, not 100% sure how that'd fly because of layering, mostly wanted to see if there was any opposition to that.
- I also just realized that actually maybe not all metadata should be copied - based on the comments on `Instruction::dropUBImplyingAttrsAndMetadata`. 

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


More information about the llvm-commits mailing list