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

Michael Kruse via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 23 23:57:44 PDT 2021


Meinersbur created this revision.
Meinersbur added reviewers: jeroen.dobbelaere, lebedev.ri.
Herald added a subscriber: hiraditya.
Meinersbur requested review of this revision.
Herald added a project: LLVM.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101226

Files:
  llvm/lib/Transforms/Utils/Local.cpp
  llvm/test/Transforms/SimplifyCFG/commondest-loopid.ll


Index: llvm/test/Transforms/SimplifyCFG/commondest-loopid.ll
===================================================================
--- /dev/null
+++ 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"}
Index: llvm/lib/Transforms/Utils/Local.cpp
===================================================================
--- llvm/lib/Transforms/Utils/Local.cpp
+++ llvm/lib/Transforms/Utils/Local.cpp
@@ -148,7 +148,11 @@
       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);
+
       Value *Cond = BI->getCondition();
       BI->eraseFromParent();
       if (DeleteDeadConditions)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101226.340245.patch
Type: text/x-patch
Size: 1316 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210424/69646db2/attachment.bin>


More information about the llvm-commits mailing list