[llvm] b99466e - [SimplifyCFG] Preserve metadata when unconditionalizing branches (same target).

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 26 15:23:12 PDT 2021


Author: Michael Kruse
Date: 2021-04-26T17:23:01-05:00
New Revision: b99466eb4525b151508d8f1a054d7a7fb4731c03

URL: https://github.com/llvm/llvm-project/commit/b99466eb4525b151508d8f1a054d7a7fb4731c03
DIFF: https://github.com/llvm/llvm-project/commit/b99466eb4525b151508d8f1a054d7a7fb4731c03.diff

LOG: [SimplifyCFG] Preserve metadata when unconditionalizing branches (same target).

When replacing a conditional branch by an unconditional one because the targets are identical, transfer the metadata to the new branch instruction.

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D101226

Added: 
    llvm/test/Transforms/SimplifyCFG/commondest-loopid.ll

Modified: 
    llvm/lib/Transforms/Utils/Local.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index b61c369734dfb..253b3cbfe5dc9 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -148,7 +148,12 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
       Dest1->removePredecessor(BI->getParent());
 
       // Replace the conditional branch with an unconditional one.
-      Builder.CreateBr(Dest1);
+      BranchInst *NewBI = Builder.CreateBr(Dest1);
+
+      // Transfer the metadata to the new branch instruction.
+      NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg,
+                                LLVMContext::MD_annotation});
+
       Value *Cond = BI->getCondition();
       BI->eraseFromParent();
       if (DeleteDeadConditions)

diff  --git a/llvm/test/Transforms/SimplifyCFG/commondest-loopid.ll b/llvm/test/Transforms/SimplifyCFG/commondest-loopid.ll
new file mode 100644
index 0000000000000..54d8152cd4251
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/commondest-loopid.ll
@@ -0,0 +1,19 @@
+; RUN: opt < %s -simplifycfg -S | FileCheck %s
+;
+; Ensure that the loop metadata is preserved when converting the
+; conditional branch to an unconditional.
+
+define void @commondest_loopid(i1 %T) {
+; CHECK-LABEL: @commondest_loopid(
+; CHECK: !llvm.loop !0
+; CHECK: !0 = distinct !{!0, !1}
+; CHECK: !1 = !{!"loopprop"}
+entry:
+        br label %loop
+
+loop:
+        br i1 %T, label %loop, label %loop, !llvm.loop !0
+}
+
+!0 = distinct !{!0, !1}
+!1 = !{!"loopprop"}


        


More information about the llvm-commits mailing list