[llvm] [ControlFlowHub] Preserve scaled branch weights on ControlFlowHub guard branches (PR #230740)

Alok Kumar Sharma via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 9 22:51:30 PDT 2026


https://github.com/alokkrsharma created https://github.com/llvm/llvm-project/pull/230740

Scale each redirected edge by its source block's profile count, or by block frequency when the function has no entry count. Sum those counts onto the guard chain. A guard is unknown only when one of its targets could not be scaled, so a later guard keeps its weight. A taken edge that rounds down to 0 is unknown, and so is the whole hub when a weight needs more than 32 bits.

This supports the FixIrreducible and UnifyLoopExits transforms.

>From 7072a2a5bda3d43a7b364a8e44c982ffbdc24607 Mon Sep 17 00:00:00 2001
From: Alok Kumar Sharma <AlokKumar.Sharma at amd.com>
Date: Sat, 10 Oct 2026 10:55:35 +0530
Subject: [PATCH] [ControlFlowHub] Preserve scaled branch weights on
 ControlFlowHub guard branches

Scale each redirected edge by its source block's profile count, or by block
frequency when the function has no entry count. Sum those counts onto the
guard chain. A guard is unknown only when one of its targets could not be
scaled, so a later guard keeps its weight. A taken edge that rounds down to
0 is unknown, and so is the whole hub when a weight needs more than 32 bits.

This supports the FixIrreducible and UnifyLoopExits transforms.
---
 .../llvm/Transforms/Utils/ControlFlowUtils.h  |  52 ++-
 .../lib/Transforms/Utils/ControlFlowUtils.cpp | 432 +++++++++++++++++-
 llvm/lib/Transforms/Utils/FixIrreducible.cpp  |  61 ++-
 llvm/lib/Transforms/Utils/UnifyLoopExits.cpp  |  66 ++-
 .../FixIrreducible/branch-weights.ll          | 354 ++++++++++++++
 .../UnifyLoopExits/branch-weights.ll          | 205 +++++++++
 6 files changed, 1147 insertions(+), 23 deletions(-)
 create mode 100644 llvm/test/Transforms/FixIrreducible/branch-weights.ll
 create mode 100644 llvm/test/Transforms/UnifyLoopExits/branch-weights.ll

diff --git a/llvm/include/llvm/Transforms/Utils/ControlFlowUtils.h b/llvm/include/llvm/Transforms/Utils/ControlFlowUtils.h
index e4dab90031e818..016ead70ac3d79 100644
--- a/llvm/include/llvm/Transforms/Utils/ControlFlowUtils.h
+++ b/llvm/include/llvm/Transforms/Utils/ControlFlowUtils.h
@@ -13,15 +13,41 @@
 #ifndef LLVM_TRANSFORMS_UTILS_CONTROLFLOWUTILS_H
 #define LLVM_TRANSFORMS_UTILS_CONTROLFLOWUTILS_H
 
+#include <optional>
+
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 
 namespace llvm {
 
 class BasicBlock;
+class BlockFrequencyInfo;
 class CallBrInst;
-class LoopInfo;
 class DomTreeUpdater;
+class Function;
+class LoopInfo;
+
+/// Positive entry count, or branch weights on some terminator.
+LLVM_ABI bool functionHasScalableBranchProfile(const Function &F);
+
+/// True when a branch weight needs more than 32 bits.
+/// BranchProbabilityInfo extracts weights as uint32_t and asserts if a weight
+/// needs more bits, so callers skip BlockFrequencyInfo instead of using
+/// truncated probabilities.
+LLVM_ABI bool functionHasBranchWeightsWiderThan32Bits(const Function &F);
+
+/// Blocks that exist now. Ones added later are not in this set.
+LLVM_ABI void recordBlocksBeforeTransform(
+    const Function &F, SmallPtrSetImpl<const BasicBlock *> &BlocksSeenBefore);
+
+/// Frequency analysis together with the set of blocks that existed when it was
+/// computed. Both pointers are null when the function has no usable profile.
+/// The caller owns both and must keep them alive for this object's lifetime.
+struct ProfileInfo {
+  BlockFrequencyInfo *BFI = nullptr;
+  const SmallPtrSetImpl<const BasicBlock *> *KnownBlocks = nullptr;
+};
 
 /// Given a set of branch descriptors [BB, Succ0, Succ1], create a "hub" such
 /// that the control flow from each BB to a successor is now split into two
@@ -78,8 +104,8 @@ class DomTreeUpdater;
 /// Limitations:
 /// -----------
 /// 1. This assumes that all terminators in the CFG are direct branches (the
-///    "br" instruction). The presence of any other control flow such as
-///    indirectbr, switch or callbr will cause an assert.
+///    "br" instruction). indirectbr still asserts. switch and callbr edges
+///    must first be split and added via addSplitTarget().
 ///
 /// 2. The updates to the PHINodes are not sufficient to restore SSA
 ///    form. Consider a definition Def, its use Use, incoming block In2 and
@@ -110,17 +136,35 @@ struct ControlFlowHub {
                  BasicBlock *Succ1 = nullptr) {
     assert(BB);
     assert(Succ0 || Succ1);
+    assert(!SplitTargets.contains(BB) &&
+           "block was already registered as a split target");
     Branches.emplace_back(BB, Succ0, Succ1);
   }
 
+  /// \p BB was just created by splitting a switch or callbr edge.
+  void addSplitTarget(BasicBlock *BB, BasicBlock *Succ) {
+    assert(!SplitTargets.contains(BB) && "split target already added");
+#ifndef NDEBUG
+    for (const BranchDescriptor &D : Branches)
+      assert(D.BB != BB && "block was already registered with addBranch");
+#endif
+    addBranch(BB, Succ);
+    SplitTargets.insert(BB);
+  }
+
   /// Return the unified loop exit block and a flag indicating if the CFG was
   /// changed at all.
   LLVM_ABI std::pair<BasicBlock *, bool>
   finalize(DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks,
            const StringRef Prefix,
-           std::optional<unsigned> MaxControlFlowBooleans = std::nullopt);
+           std::optional<unsigned> MaxControlFlowBooleans = std::nullopt,
+           ProfileInfo Profile = {});
 
   SmallVector<BranchDescriptor> Branches;
+
+private:
+  /// Blocks created by splitting switch/callbr edges.
+  SmallPtrSet<BasicBlock *, 8> SplitTargets;
 };
 
 } // end namespace llvm
diff --git a/llvm/lib/Transforms/Utils/ControlFlowUtils.cpp b/llvm/lib/Transforms/Utils/ControlFlowUtils.cpp
index cf3bacc08f5a54..a76790248c0dc2 100644
--- a/llvm/lib/Transforms/Utils/ControlFlowUtils.cpp
+++ b/llvm/lib/Transforms/Utils/ControlFlowUtils.cpp
@@ -11,12 +11,19 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Utils/ControlFlowUtils.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SetVector.h"
+#include "llvm/Analysis/BlockFrequencyInfo.h"
 #include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/Analysis/LoopInfo.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/Function.h"
 #include "llvm/IR/Instructions.h"
+#include "llvm/IR/ProfDataUtils.h"
 #include "llvm/IR/ValueHandle.h"
+#include "llvm/Support/BranchProbability.h"
 #include "llvm/Transforms/Utils/Local.h"
 
 #define DEBUG_TYPE "control-flow-hub"
@@ -246,7 +253,7 @@ static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,
     bool AllUndef = true;
     for (auto [BB, Succ0, Succ1] : Incoming) {
       Value *V = PoisonValue::get(Phi->getType());
-      if  (Phi->getBasicBlockIndex(BB) != -1) {
+      if (Phi->getBasicBlockIndex(BB) != -1) {
         V = Phi->removeIncomingValue(BB, false);
         // When both successors are the same (Succ0 == Succ1), there are two
         // edges from BB to Out, so we need to remove the second PHI entry too.
@@ -277,9 +284,290 @@ static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,
   }
 }
 
+bool llvm::functionHasScalableBranchProfile(const Function &F) {
+  if (F.getEntryCount().value_or(0) > 0)
+    return true;
+  for (const BasicBlock &BB : F) {
+    if (const Instruction *Term = BB.getTerminator())
+      if (hasBranchWeightMD(*Term))
+        return true;
+  }
+  return false;
+}
+
+bool llvm::functionHasBranchWeightsWiderThan32Bits(const Function &F) {
+  for (const BasicBlock &BB : F) {
+    const Instruction *Term = BB.getTerminator();
+    if (!Term)
+      continue;
+    const MDNode *MD = getBranchWeightMDNode(*Term);
+    if (!MD)
+      continue;
+    for (unsigned I = getBranchWeightOffset(MD), E = MD->getNumOperands();
+         I != E; ++I) {
+      const auto *Weight = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
+      if (Weight && Weight->getValue().getActiveBits() > 32)
+        return true;
+    }
+  }
+  return false;
+}
+
+void llvm::recordBlocksBeforeTransform(
+    const Function &F, SmallPtrSetImpl<const BasicBlock *> &BlocksSeenBefore) {
+  for (const BasicBlock &BB : F)
+    BlocksSeenBefore.insert(&BB);
+}
+
+// Returns false if adding Amount to Total would overflow.
+static bool addWeight(uint64_t &Total, uint64_t Amount) {
+  if (Total > UINT64_MAX - Amount)
+    return false;
+  Total += Amount;
+  return true;
+}
+
+// Return the execution count for a block that existed when BFI was computed.
+// Prefer profile counts when available; otherwise fall back to the raw block
+// frequency.
+static std::optional<uint64_t>
+getBlockCount(BlockFrequencyInfo &BFI,
+              const SmallPtrSetImpl<const BasicBlock *> &KnownBlocks,
+              const BasicBlock *Block) {
+  if (!KnownBlocks.contains(Block))
+    return std::nullopt;
+  if (std::optional<uint64_t> Count = BFI.getBlockProfileCount(Block))
+    return Count;
+  return BFI.getBlockFreq(Block).getFrequency();
+}
+
+// This edge's share of the block count: EdgeWeight / TotalWeight * block count.
+// Empty when the total wrapped, or when a taken edge scales down to 0.
+static std::optional<uint64_t>
+scaleToBlockCount(BlockFrequencyInfo &BFI,
+                  const SmallPtrSetImpl<const BasicBlock *> &KnownBlocks,
+                  const BasicBlock *FromBlock, uint64_t EdgeWeight,
+                  uint64_t TotalWeight) {
+  if (!TotalWeight || EdgeWeight > TotalWeight)
+    return std::nullopt;
+  std::optional<uint64_t> Count = getBlockCount(BFI, KnownBlocks, FromBlock);
+  if (!Count)
+    return std::nullopt;
+  if (!*Count || !EdgeWeight)
+    return 0;
+  uint64_t Scaled =
+      BranchProbability::getBranchProbability(EdgeWeight, TotalWeight)
+          .scale(*Count);
+  if (!Scaled)
+    return std::nullopt;
+  return Scaled;
+}
+
+// Profile information for an edge redirected through the guard hub.
+struct RedirectedEdge {
+  BasicBlock *Target = nullptr;
+  // Estimated count reaching Target.
+  std::optional<uint64_t> Count;
+  // Source terminator had branch_weights.
+  bool HadBranchWeights = false;
+};
+
+// False when a weight is not an integer or needs more than 64 bits.
+static bool readBranchWeights64(const MDNode &MD,
+                                SmallVectorImpl<uint64_t> &Weights) {
+  Weights.clear();
+  unsigned Offset = getBranchWeightOffset(&MD);
+  if (Offset >= MD.getNumOperands())
+    return false;
+  for (unsigned I = Offset, E = MD.getNumOperands(); I != E; ++I) {
+    const auto *Weight = mdconst::dyn_extract<ConstantInt>(MD.getOperand(I));
+    if (!Weight || Weight->getValue().getActiveBits() > 64)
+      return false;
+    Weights.push_back(Weight->getValue().getZExtValue());
+  }
+  return true;
+}
+
+// Recover the count for a newly-created split block from its predecessor's
+// count and branch weights.
+static RedirectedEdge
+countFromPredecessor(BlockFrequencyInfo &BFI,
+                     const SmallPtrSetImpl<const BasicBlock *> &KnownBlocks,
+                     const BasicBlock *SplitBlock) {
+  RedirectedEdge Result;
+  const BasicBlock *Pred = SplitBlock->getUniquePredecessor();
+  if (!Pred)
+    return Result;
+  const Instruction *Term = Pred->getTerminator();
+  if (!Term)
+    return Result;
+  unsigned NumSuccessors = Term->getNumSuccessors();
+  const MDNode *MD = getBranchWeightMDNode(*Term);
+  // Need per-successor weights to reconstruct the flow into SplitBlock.
+  if (!MD || NumSuccessors < 2) {
+    Result.HadBranchWeights = hasBranchWeightMD(*Term);
+    return Result;
+  }
+  SmallVector<uint64_t, 8> Weights;
+  if (!readBranchWeights64(*MD, Weights) || Weights.size() != NumSuccessors) {
+    Result.HadBranchWeights = true;
+    return Result;
+  }
+  Result.HadBranchWeights = true;
+  uint64_t WeightToSplitBlock = 0;
+  uint64_t TotalWeight = 0;
+  bool SawSplitBlockEdge = false;
+  for (unsigned I = 0; I != NumSuccessors; ++I) {
+    if (!addWeight(TotalWeight, Weights[I]))
+      return Result;
+    if (Term->getSuccessor(I) != SplitBlock)
+      continue;
+    if (!addWeight(WeightToSplitBlock, Weights[I]))
+      return Result;
+    SawSplitBlockEdge = true;
+  }
+  if (!SawSplitBlockEdge)
+    return Result;
+  Result.Count = scaleToBlockCount(BFI, KnownBlocks, Pred, WeightToSplitBlock,
+                                   TotalWeight);
+  return Result;
+}
+
+// Edges redirected through the hub, plus whether the function already has
+// profile data those edges can be annotated from.
+struct CollectedRedirectedFlows {
+  SmallVector<RedirectedEdge, 8> Flows;
+  // Positive function_entry_count.
+  bool HasPositiveEntryCount = false;
+  // A redirected edge had branch_weights.
+  bool SawBranchWeights = false;
+};
+
+// Counts for both edges of a conditional branch. The 32-bit weight helper
+// drops the top bits, so the 64-bit metadata is read here.
+static void addConditionalBranchFlows(
+    const CondBrInst &Br, BasicBlock *FromBlock, BasicBlock *FirstTarget,
+    BasicBlock *SecondTarget, BlockFrequencyInfo *BFI,
+    const SmallPtrSetImpl<const BasicBlock *> &BlocksSeenBefore,
+    function_ref<void(BasicBlock *, bool, std::optional<uint64_t>)> AddFlow) {
+  const MDNode *MD = getBranchWeightMDNode(Br);
+  // Both arms to one block carry that block's whole count.
+  if (FirstTarget == SecondTarget) {
+    std::optional<uint64_t> Count;
+    if (BFI)
+      Count = getBlockCount(*BFI, BlocksSeenBefore, FromBlock);
+    AddFlow(FirstTarget, MD != nullptr, Count);
+    return;
+  }
+  if (!MD) {
+    AddFlow(FirstTarget, /*HadBranchWeights=*/false, std::nullopt);
+    AddFlow(SecondTarget, /*HadBranchWeights=*/false, std::nullopt);
+    return;
+  }
+  // Counts are unused without BFI.
+  if (!BFI) {
+    AddFlow(FirstTarget, /*HadBranchWeights=*/true, std::nullopt);
+    AddFlow(SecondTarget, /*HadBranchWeights=*/true, std::nullopt);
+    return;
+  }
+  SmallVector<uint64_t, 2> Weights;
+  // A weight past 64 bits leaves both targets unknown.
+  if (!readBranchWeights64(*MD, Weights) || Weights.size() != 2) {
+    AddFlow(FirstTarget, /*HadBranchWeights=*/true, std::nullopt);
+    AddFlow(SecondTarget, /*HadBranchWeights=*/true, std::nullopt);
+    return;
+  }
+  uint64_t TrueWeight = Weights[0], FalseWeight = Weights[1];
+  uint64_t TotalWeight = 0;
+  bool CanScale =
+      addWeight(TotalWeight, TrueWeight) && addWeight(TotalWeight, FalseWeight);
+  std::optional<uint64_t> TrueCount, FalseCount;
+  if (CanScale) {
+    TrueCount = scaleToBlockCount(*BFI, BlocksSeenBefore, FromBlock, TrueWeight,
+                                  TotalWeight);
+    FalseCount = scaleToBlockCount(*BFI, BlocksSeenBefore, FromBlock,
+                                   FalseWeight, TotalWeight);
+  }
+  AddFlow(FirstTarget, /*HadBranchWeights=*/true, TrueCount);
+  AddFlow(SecondTarget, /*HadBranchWeights=*/true, FalseCount);
+}
+
+// Record each redirected edge.
+// BFI only covers blocks that existed when it was computed. Newly-created
+// hub/split blocks are therefore excluded from count recovery.
+static CollectedRedirectedFlows
+collectRedirectedFlows(ArrayRef<EdgeDescriptor> Branches,
+                       const SmallPtrSetImpl<BasicBlock *> &SplitTargets,
+                       BlockFrequencyInfo *BFI,
+                       const SmallPtrSetImpl<const BasicBlock *> *KnownBlocks) {
+  CollectedRedirectedFlows Result;
+  SmallPtrSet<const BasicBlock *, 1> Empty;
+  const SmallPtrSetImpl<const BasicBlock *> &BlocksSeenBefore =
+      KnownBlocks ? *KnownBlocks : Empty;
+  if (!Branches.empty()) {
+    const Function *Fn = Branches.front().BB->getParent();
+    Result.HasPositiveEntryCount = Fn->getEntryCount().value_or(0) > 0;
+  }
+  auto addFlow = [&](BasicBlock *Target, bool HadBranchWeights,
+                     std::optional<uint64_t> Count = std::nullopt) {
+    if (!Target)
+      return;
+    RedirectedEdge Edge;
+    Edge.Target = Target;
+    Edge.HadBranchWeights = HadBranchWeights;
+    Edge.Count = Count;
+    Result.Flows.push_back(Edge);
+  };
+  for (auto [FromBlock, FirstTarget, SecondTarget] : Branches) {
+    // For blocks introduced while splitting edges/guards, recover the flow
+    // from the original predecessor rather than from the synthetic block.
+    if (SplitTargets.contains(FromBlock)) {
+      BasicBlock *OriginalTarget = FirstTarget ? FirstTarget : SecondTarget;
+      if (!BFI) {
+        bool HadBranchWeights = false;
+        if (const BasicBlock *Pred = FromBlock->getUniquePredecessor())
+          if (const Instruction *Term = Pred->getTerminator())
+            HadBranchWeights = hasBranchWeightMD(*Term);
+        addFlow(OriginalTarget, HadBranchWeights);
+      } else {
+        RedirectedEdge Edge =
+            countFromPredecessor(*BFI, BlocksSeenBefore, FromBlock);
+        addFlow(OriginalTarget, Edge.HadBranchWeights, Edge.Count);
+      }
+      continue;
+    }
+    if (const auto *Br = dyn_cast<CondBrInst>(FromBlock->getTerminator())) {
+      addConditionalBranchFlows(*Br, FromBlock, FirstTarget, SecondTarget, BFI,
+                                BlocksSeenBefore, addFlow);
+      continue;
+    }
+    if (!FirstTarget && !SecondTarget)
+      continue;
+    // Two different successors that are not a conditional branch. Neither
+    // edge has a weight of its own.
+    if (FirstTarget && SecondTarget && FirstTarget != SecondTarget) {
+      addFlow(FirstTarget, /*HadBranchWeights=*/false);
+      addFlow(SecondTarget, /*HadBranchWeights=*/false);
+      continue;
+    }
+    BasicBlock *OnlyTarget = FirstTarget ? FirstTarget : SecondTarget;
+    std::optional<uint64_t> Count;
+    if (BFI)
+      Count = getBlockCount(*BFI, BlocksSeenBefore, FromBlock);
+    addFlow(OnlyTarget, /*HadBranchWeights=*/false, Count);
+  }
+  Result.SawBranchWeights =
+      any_of(Result.Flows,
+             [](const RedirectedEdge &Edge) { return Edge.HadBranchWeights; });
+  return Result;
+}
+
 std::pair<BasicBlock *, bool> ControlFlowHub::finalize(
     DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks,
-    const StringRef Prefix, std::optional<unsigned> MaxControlFlowBooleans) {
+    const StringRef Prefix, std::optional<unsigned> MaxControlFlowBooleans,
+    ProfileInfo Profile) {
+  BlockFrequencyInfo *BFI = Profile.BFI;
+  const SmallPtrSetImpl<const BasicBlock *> *KnownBlocks = Profile.KnownBlocks;
 #ifndef NDEBUG
   SmallPtrSet<BasicBlock *, 8> Incoming;
 #endif
@@ -297,6 +585,19 @@ std::pair<BasicBlock *, bool> ControlFlowHub::finalize(
       Outgoing.insert(Succ1);
   }
 
+  // Weights for the edges about to be redirected through the guards.
+  CollectedRedirectedFlows Edges =
+      collectRedirectedFlows(Branches, SplitTargets, BFI, KnownBlocks);
+  // Already profiled: a guard with no recovered count is marked unknown.
+  const bool MayAnnotate =
+      Edges.SawBranchWeights || Edges.HasPositiveEntryCount;
+  auto markUnknown = [&](Instruction &I) {
+    if (Edges.HasPositiveEntryCount)
+      setExplicitlyUnknownBranchWeightsIfProfiled(I, DEBUG_TYPE);
+    else if (Edges.SawBranchWeights)
+      setExplicitlyUnknownBranchWeights(I, DEBUG_TYPE);
+  };
+
   assert(Outgoing.size() && "No outgoing edges");
 
   if (Outgoing.size() < 2)
@@ -316,6 +617,133 @@ std::pair<BasicBlock *, bool> ControlFlowHub::finalize(
   SmallVector<WeakVH, 8> DeletionCandidates;
   convertToGuardPredicates(Branches, Outgoing.getArrayRef(), GuardBlocks,
                            DeletionCandidates, Prefix, MaxControlFlowBooleans);
+
+  if (MayAnnotate) {
+    // [src] -> [guard0] -> [Out0]
+    //              |
+    //              +-> [guard1] -> [Out1]
+    //                       |
+    //                       +-> [Out2]
+    // guard0 compares Out0 with the rest of the chain. A target that could
+    // not be scaled makes that guard unknown. guard1 still gets a weight
+    // when Out1 and Out2 could be scaled. The examples below have one guard
+    // each, so both outgoing counts are written on that branch.
+    //
+    // FixIrreducible. Entry count 100. right branches back to left.
+    // Both exits are the same block.
+    //
+    //                     +----------------------------+
+    //                     v                            |
+    // [entry] --25--> [left] --80--+                   |
+    //    |              |          v                   |
+    //    |              v          |                   |
+    //    |           [exit]        |                   |
+    //    +-------------75-----> [right]----------------+
+    //                              |
+    //                              v
+    //                           [exit]
+    //
+    // entry->left  = 100 * 10/40 = 25
+    // entry->right = 100 * 30/40 = 75
+    // left->right scales to 80
+    //
+    // After. One [left]. entry's two edges and left->right are redirected
+    // through [guard]. right->left still targets [left].
+    // In: 100 + 80 = 180. Out: 155 + 25 = 180.
+    // 155 = 75 + 80 to right. 25 is entry->left. 80 is left->guard.
+    //
+    //                                   +-----------------+
+    //                                   v                 |
+    // [entry] --100--> [guard] --25--> [left]             |
+    //                     | ^           |  |              |
+    //                     | |           |  |              |
+    //                     | +----80-----+  |              |
+    //                    155             [exit]           |
+    //                     |                               |
+    //                     v                               |
+    //                  [right]----------------------------+
+    //                     |
+    //                     v
+    //                  [exit]
+    //
+    // UnifyLoopExits. Entry count 100. [latch] branches back to [header].
+    //
+    //                      +------------------------+
+    //                      v                        |
+    // [entry] --100--> [header] --120--> [latch]----+
+    //                     |                 |
+    //                    40                60
+    //                     v                 v
+    //                 [exit.a]          [exit.b]
+    //
+    // header = 100 + 60 = 160
+    // header->latch = 160 * 3/4 = 120
+    // header->exit.a = 160 * 1/4 = 40
+    // latch->header = latch->exit.b = 120 * 1/2 = 60
+    //
+    // After. Both exits go through [guard]. The back edge stays.
+    // In: 40 + 60 = 100. Out: 40 + 60 = 100.
+    //
+    //                      +------------------------+
+    //                      v                        |
+    // [entry] --100--> [header] --120--> [latch]----+
+    //                     |                 |
+    //                    40                60
+    //                     +--------+--------+
+    //                              v
+    //                          [guard] --40--> [exit.a]
+    //                             |
+    //                            60
+    //                             v
+    //                         [exit.b]
+    DenseMap<BasicBlock *, uint64_t> TargetWeight;
+    DenseSet<BasicBlock *> Unresolved;
+    // If a redirected edge has no target, the corresponding flow cannot be
+    // recovered, so every synthesized guard is marked unknown.
+    bool MissingTarget = any_of(Branches, [&](const EdgeDescriptor &Branch) {
+      auto [FromBlock, FirstTarget, SecondTarget] = Branch;
+      return !FirstTarget && !SecondTarget &&
+             !SplitTargets.contains(FromBlock) &&
+             !isa<CondBrInst>(FromBlock->getTerminator());
+    });
+    if (!BFI || MissingTarget) {
+      for (BasicBlock *Out : Outgoing)
+        Unresolved.insert(Out);
+    } else {
+      for (const RedirectedEdge &Edge : Edges.Flows) {
+        // Overflow while accumulating counts makes the target unusable.
+        if (!Edge.Count || !addWeight(TargetWeight[Edge.Target], *Edge.Count))
+          Unresolved.insert(Edge.Target);
+      }
+    }
+    for (unsigned I = 0, E = GuardBlocks.size(); I != E; ++I) {
+      auto *Br = cast<CondBrInst>(GuardBlocks[I]->getTerminator());
+      if (Unresolved.contains(Outgoing[I])) {
+        markUnknown(*Br);
+        continue;
+      }
+      uint64_t ThisTargetCount = TargetWeight.lookup(Outgoing[I]);
+      uint64_t RestCount = 0;
+      bool CanAnnotate = true;
+      for (unsigned J = I + 1; J != Outgoing.size(); ++J) {
+        if (Unresolved.contains(Outgoing[J]) ||
+            !addWeight(RestCount, TargetWeight.lookup(Outgoing[J]))) {
+          CanAnnotate = false;
+          break;
+        }
+      }
+      if (!CanAnnotate) {
+        markUnknown(*Br);
+        continue;
+      }
+      // {0, 0} would look like the block never runs.
+      if (ThisTargetCount || RestCount)
+        setFittedBranchWeights(*Br, {ThisTargetCount, RestCount},
+                               /*IsExpected=*/false);
+      else
+        markUnknown(*Br);
+    }
+  }
   BasicBlock *FirstGuardBlock = GuardBlocks.front();
 
   // Update the PHINodes in each outgoing block to match the new control flow.
diff --git a/llvm/lib/Transforms/Utils/FixIrreducible.cpp b/llvm/lib/Transforms/Utils/FixIrreducible.cpp
index 09e9f426dced64..0346efae3632b5 100644
--- a/llvm/lib/Transforms/Utils/FixIrreducible.cpp
+++ b/llvm/lib/Transforms/Utils/FixIrreducible.cpp
@@ -129,9 +129,12 @@
 
 #include "llvm/Transforms/Utils/FixIrreducible.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/Analysis/BlockFrequencyInfo.h"
+#include "llvm/Analysis/BranchProbabilityInfo.h"
 #include "llvm/Analysis/CycleAnalysis.h"
 #include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
@@ -272,7 +275,7 @@ static void updateLoopInfo(CycleInfo &CI, LoopInfo &LI, CycleRef C,
 // natural loop. Also insert this new loop at its appropriate place in the
 // hierarchy of loops.
 static bool fixIrreducible(CycleRef C, CycleInfo &CI, DominatorTree &DT,
-                           LoopInfo *LI) {
+                           LoopInfo *LI, ProfileInfo Profile) {
   if (CI.isReducible(C))
     return false;
   LLVM_DEBUG(dbgs() << "Processing cycle:\n" << CI.print(C) << "\n";);
@@ -318,7 +321,7 @@ static bool fixIrreducible(CycleRef C, CycleInfo &CI, DominatorTree &DT,
                           << printBasicBlock(Succ) << '\n');
       }
       if (NewSucc)
-        CHub.addBranch(NewSucc, Header);
+        CHub.addSplitTarget(NewSucc, Header);
     } else {
       reportFatalUsageError(
           "unsupported block terminator: fix-irreducible "
@@ -368,7 +371,7 @@ static bool fixIrreducible(CycleRef C, CycleInfo &CI, DominatorTree &DT,
         BasicBlock *NewSucc =
             SplitMultiBrEdge(P, Succ, I, ExistingTarget, &DTU, &CI, LI);
         if (!ExistingTarget) {
-          CHub.addBranch(NewSucc, Succ);
+          CHub.addSplitTarget(NewSucc, Succ);
           MultiBrTargets[Succ] = NewSucc;
         }
         LLVM_DEBUG(dbgs() << "Added external branch: "
@@ -395,7 +398,8 @@ static bool fixIrreducible(CycleRef C, CycleInfo &CI, DominatorTree &DT,
   SetVector<BasicBlock *> Entries;
   Entries.insert(CI.getEntries(C).rbegin(), CI.getEntries(C).rend());
 
-  CHub.finalize(&DTU, GuardBlocks, "irr");
+  CHub.finalize(&DTU, GuardBlocks, "irr",
+                /*MaxControlFlowBooleans=*/std::nullopt, Profile);
 #if defined(EXPENSIVE_CHECKS)
   assert(DT.verify(DominatorTree::VerificationLevel::Full));
 #else
@@ -422,14 +426,21 @@ static bool fixIrreducible(CycleRef C, CycleInfo &CI, DominatorTree &DT,
   return true;
 }
 
+static bool hasIrreducibleCycle(const CycleInfo &CI) {
+  for (auto C : CI.cycles())
+    if (!CI.isReducible(C))
+      return true;
+  return false;
+}
+
 static bool FixIrreducibleImpl(Function &F, CycleInfo &CI, DominatorTree &DT,
-                               LoopInfo *LI) {
+                               LoopInfo *LI, ProfileInfo Profile) {
   LLVM_DEBUG(dbgs() << "===== Fix irreducible control-flow in function: "
                     << F.getName() << "\n");
 
   bool Changed = false;
   for (auto C : CI.cycles())
-    Changed |= fixIrreducible(C, CI, DT, LI);
+    Changed |= fixIrreducible(C, CI, DT, LI, Profile);
 
   if (!Changed)
     return false;
@@ -448,16 +459,48 @@ bool FixIrreducible::runOnFunction(Function &F) {
   LoopInfo *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
   auto &CI = getAnalysis<CycleInfoWrapperPass>().getResult();
   auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
-  return FixIrreducibleImpl(F, CI, DT, LI);
+
+  std::optional<BranchProbabilityInfo> BPI;
+  std::optional<BlockFrequencyInfo> OwnedBFI;
+  ProfileInfo Profile;
+  SmallPtrSet<const BasicBlock *, 32> BlocksSeenBefore;
+  // BPI extracts weights as uint32_t and asserts when one needs more bits.
+  if (hasIrreducibleCycle(CI) && functionHasScalableBranchProfile(F) &&
+      !functionHasBranchWeightsWiderThan32Bits(F)) {
+    // Use TLI if the legacy pipeline already computed it.
+    const TargetLibraryInfo *TLI = nullptr;
+    if (auto *TLIWP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>())
+      TLI = &TLIWP->getTLI(F);
+    BPI.emplace(F, CI, TLI, &DT);
+    OwnedBFI.emplace(F, *BPI, CI);
+    Profile.BFI = &*OwnedBFI;
+    // Record which blocks this BFI covers before transforming the CFG.
+    recordBlocksBeforeTransform(F, BlocksSeenBefore);
+    Profile.KnownBlocks = &BlocksSeenBefore;
+  }
+  return FixIrreducibleImpl(F, CI, DT, LI, Profile);
 }
 
 PreservedAnalyses FixIrreduciblePass::run(Function &F,
                                           FunctionAnalysisManager &AM) {
-  auto *LI = AM.getCachedResult<LoopAnalysis>(F);
   auto &CI = AM.getResult<CycleAnalysis>(F);
+  if (!hasIrreducibleCycle(CI))
+    return PreservedAnalyses::all();
+
+  auto *LI = AM.getCachedResult<LoopAnalysis>(F);
   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
 
-  if (!FixIrreducibleImpl(F, CI, DT, LI))
+  ProfileInfo Profile;
+  SmallPtrSet<const BasicBlock *, 32> BlocksSeenBefore;
+  // BPI extracts weights as uint32_t and asserts when one needs more bits.
+  if (functionHasScalableBranchProfile(F) &&
+      !functionHasBranchWeightsWiderThan32Bits(F)) {
+    Profile.BFI = &AM.getResult<BlockFrequencyAnalysis>(F);
+    // Record which blocks this BFI covers before transforming the CFG.
+    recordBlocksBeforeTransform(F, BlocksSeenBefore);
+    Profile.KnownBlocks = &BlocksSeenBefore;
+  }
+  if (!FixIrreducibleImpl(F, CI, DT, LI, Profile))
     return PreservedAnalyses::all();
 
   PreservedAnalyses PA;
diff --git a/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp b/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp
index b8aba85130368e..da5b3c96c2aa8d 100644
--- a/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp
+++ b/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp
@@ -23,9 +23,13 @@
 #include "llvm/Transforms/Utils/UnifyLoopExits.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/MapVector.h"
+#include "llvm/Analysis/BlockFrequencyInfo.h"
+#include "llvm/Analysis/BranchProbabilityInfo.h"
 #include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/CycleInfo.h"
 #include "llvm/IR/Dominators.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/InitializePasses.h"
@@ -147,7 +151,8 @@ static void restoreSSA(const DominatorTree &DT, const Loop *L,
   }
 }
 
-static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L) {
+static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L,
+                           ProfileInfo Profile) {
   // To unify the loop exits, we need a list of the exiting blocks as
   // well as exit blocks. The functions for locating these lists both
   // traverse the entire loop body. It is more efficient to first
@@ -224,7 +229,7 @@ static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L) {
             ExitingBlocks[I] = NewSucc;
           else
             ExitingBlocks.push_back(NewSucc);
-          CHub.addBranch(NewSucc, Succ);
+          CHub.addSplitTarget(NewSucc, Succ);
           BrTargets[Succ] = NewSucc;
         }
         LLVM_DEBUG(dbgs() << "Added exiting branch: "
@@ -241,8 +246,9 @@ static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L) {
   SmallVector<BasicBlock *, 8> GuardBlocks;
   BasicBlock *LoopExitBlock;
   bool ChangedCFG;
-  std::tie(LoopExitBlock, ChangedCFG) = CHub.finalize(
-      &DTU, GuardBlocks, "loop.exit", MaxBooleansInControlFlowHub.getValue());
+  std::tie(LoopExitBlock, ChangedCFG) =
+      CHub.finalize(&DTU, GuardBlocks, "loop.exit",
+                    MaxBooleansInControlFlowHub.getValue(), Profile);
   ChangedCFG |= Changed;
   if (!ChangedCFG)
     return false;
@@ -281,13 +287,29 @@ static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L) {
   return true;
 }
 
-static bool runImpl(LoopInfo &LI, DominatorTree &DT) {
+static bool hasMultiExitLoop(const LoopInfo &LI) {
+  for (Loop *L : LI.getLoopsInPreorder()) {
+    SmallVector<BasicBlock *, 4> Exits;
+    L->getUniqueExitBlocks(Exits);
+    if (Exits.size() > 1)
+      return true;
+  }
+  return false;
+}
+
+// BPI extracts weights as uint32_t and asserts when one needs more bits.
+static bool shouldCollectProfileInfo(const Function &F, const LoopInfo &LI) {
+  // Profile first. An unprofiled function is one metadata scan.
+  return functionHasScalableBranchProfile(F) &&
+         !functionHasBranchWeightsWiderThan32Bits(F) && hasMultiExitLoop(LI);
+}
 
+static bool runImpl(LoopInfo &LI, DominatorTree &DT, ProfileInfo Profile) {
   bool Changed = false;
   auto Loops = LI.getLoopsInPreorder();
   for (auto *L : Loops) {
     LLVM_DEBUG(dbgs() << "Processing loop:\n"; L->print(dbgs()));
-    Changed |= unifyLoopExits(DT, LI, L);
+    Changed |= unifyLoopExits(DT, LI, L, Profile);
   }
   return Changed;
 }
@@ -298,7 +320,26 @@ bool UnifyLoopExitsLegacyPass::runOnFunction(Function &F) {
   auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
   auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
 
-  return runImpl(LI, DT);
+  std::optional<CycleInfo> CI;
+  std::optional<BranchProbabilityInfo> BPI;
+  std::optional<BlockFrequencyInfo> OwnedBFI;
+  ProfileInfo Profile;
+  SmallPtrSet<const BasicBlock *, 32> BlocksSeenBefore;
+  if (shouldCollectProfileInfo(F, LI)) {
+    CI.emplace();
+    CI->compute(F);
+    // Use TLI if the legacy pipeline already computed it.
+    const TargetLibraryInfo *TLI = nullptr;
+    if (auto *TLIWP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>())
+      TLI = &TLIWP->getTLI(F);
+    BPI.emplace(F, *CI, TLI, &DT);
+    OwnedBFI.emplace(F, *BPI, *CI);
+    Profile.BFI = &*OwnedBFI;
+    // Record which blocks this BFI covers before transforming the CFG.
+    recordBlocksBeforeTransform(F, BlocksSeenBefore);
+    Profile.KnownBlocks = &BlocksSeenBefore;
+  }
+  return runImpl(LI, DT, Profile);
 }
 
 namespace llvm {
@@ -310,7 +351,16 @@ PreservedAnalyses UnifyLoopExitsPass::run(Function &F,
   auto &LI = AM.getResult<LoopAnalysis>(F);
   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
 
-  if (!runImpl(LI, DT))
+  // Always run: a switch can still need splitting with one exit.
+  ProfileInfo Profile;
+  SmallPtrSet<const BasicBlock *, 32> BlocksSeenBefore;
+  if (shouldCollectProfileInfo(F, LI)) {
+    Profile.BFI = &AM.getResult<BlockFrequencyAnalysis>(F);
+    // Record which blocks this BFI covers before transforming the CFG.
+    recordBlocksBeforeTransform(F, BlocksSeenBefore);
+    Profile.KnownBlocks = &BlocksSeenBefore;
+  }
+  if (!runImpl(LI, DT, Profile))
     return PreservedAnalyses::all();
   PreservedAnalyses PA;
   PA.preserve<LoopAnalysis>();
diff --git a/llvm/test/Transforms/FixIrreducible/branch-weights.ll b/llvm/test/Transforms/FixIrreducible/branch-weights.ll
new file mode 100644
index 00000000000000..138ef4aebf6394
--- /dev/null
+++ b/llvm/test/Transforms/FixIrreducible/branch-weights.ll
@@ -0,0 +1,354 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -fix-irreducible --verify-loop-info -S | FileCheck %s
+; RUN: opt < %s -passes='fix-irreducible,verify<loops>' -S | FileCheck %s
+; RUN: opt < %s -passes='verify<loops>,fix-irreducible,verify<loops>' -S | FileCheck %s
+
+; Before. Entry count 100. Labels are absolute counts.
+; right branches back to left. Both exits are the same block.
+;
+;                     +----------------------------+
+;                     v                            |
+; [entry] --25--> [left] --80--+                   |
+;    |              |          v                   |
+;    |              v          |                   |
+;    |           [exit]        |                   |
+;    +-------------75-----> [right]----------------+
+;                              |
+;                              v
+;                           [exit]
+;
+; entry->left  = 100 * 10/40 = 25
+; entry->right = 100 * 30/40 = 75
+; left->right scales to 80
+;
+; After. One [left]. entry's two edges and left->right are redirected
+; through [guard]. right->left is not, so it still targets [left].
+; Both exits stay and are the same block.
+; In: 100 + 80 = 180. Out: 155 + 25 = 180.
+; 155 = 75 + 80 to right. 25 is entry->left. 80 is left->guard.
+;
+;                                   +-----------------+
+;                                   v                 |
+; [entry] --100--> [guard] --25--> [left]             |
+;                     | ^           |  |              |
+;                     | |           |  |              |
+;                     | +----80-----+  |              |
+;                    155             [exit]           |
+;                     |                               |
+;                     v                               |
+;                  [right]----------------------------+
+;                     |
+;                     v
+;                  [exit]
+define void @weights(i1 %PredEntry, i1 %PredLeft, i1 %PredRight) !prof !3 {
+; CHECK-LABEL: define void @weights(
+; CHECK-SAME: i1 [[PREDENTRY:%.*]], i1 [[PREDLEFT:%.*]], i1 [[PREDRIGHT:%.*]]) !prof [[PROF0:![0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[PREDENTRY_INV:%.*]] = xor i1 [[PREDENTRY]], true
+; CHECK-NEXT:    br label %[[IRR_GUARD:.*]]
+; CHECK:       [[LEFT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDLEFT]], label %[[IRR_GUARD]], label %[[EXIT:.*]], !prof [[PROF1:![0-9]+]]
+; CHECK:       [[RIGHT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDRIGHT]], label %[[LEFT]], label %[[EXIT]], !prof [[PROF2:![0-9]+]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[IRR_GUARD]]:
+; CHECK-NEXT:    [[GUARD_RIGHT:%.*]] = phi i1 [ true, %[[LEFT]] ], [ [[PREDENTRY_INV]], %[[ENTRY]] ]
+; CHECK-NEXT:    br i1 [[GUARD_RIGHT]], label %[[RIGHT]], label %[[LEFT]], !prof [[PROF3:![0-9]+]]
+;
+entry:
+  br i1 %PredEntry, label %left, label %right, !prof !0
+
+left:
+  br i1 %PredLeft, label %right, label %exit, !prof !1
+
+right:
+  br i1 %PredRight, label %left, label %exit, !prof !2
+
+exit:
+  ret void
+}
+
+; [left] switch -> [.target.] -> [guard] -> [right]
+; Both cases share .target. Guard is 321 to right and 25 to left.
+; 321 = 75 + 246. entry->right = 75. left is 308; 308 * 4/5 floors to 246.
+define void @shared_switch_header(i32 %X, i1 %PredEntry, i1 %PredRight) !prof !3 {
+; CHECK-LABEL: define void @shared_switch_header(
+; CHECK-SAME: i32 [[X:%.*]], i1 [[PREDENTRY:%.*]], i1 [[PREDRIGHT:%.*]]) !prof [[PROF0]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[PREDENTRY_INV:%.*]] = xor i1 [[PREDENTRY]], true
+; CHECK-NEXT:    br label %[[IRR_GUARD:.*]]
+; CHECK:       [[LEFT:.*]]:
+; CHECK-NEXT:    switch i32 [[X]], label %[[EXIT:.*]] [
+; CHECK-NEXT:      i32 0, label %[[LEFT_TARGET_RIGHT:.*]]
+; CHECK-NEXT:      i32 1, label %[[LEFT_TARGET_RIGHT]]
+; CHECK-NEXT:    ], !prof [[PROF4:![0-9]+]]
+; CHECK:       [[RIGHT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDRIGHT]], label %[[LEFT]], label %[[EXIT]], !prof [[PROF2]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[LEFT_TARGET_RIGHT]]:
+; CHECK-NEXT:    br label %[[IRR_GUARD]]
+; CHECK:       [[IRR_GUARD]]:
+; CHECK-NEXT:    [[GUARD_RIGHT:%.*]] = phi i1 [ true, %[[LEFT_TARGET_RIGHT]] ], [ [[PREDENTRY_INV]], %[[ENTRY]] ]
+; CHECK-NEXT:    br i1 [[GUARD_RIGHT]], label %[[RIGHT]], label %[[LEFT]], !prof [[PROF5:![0-9]+]]
+;
+entry:
+  br i1 %PredEntry, label %left, label %right, !prof !0
+
+left:
+  switch i32 %X, label %exit [
+  i32 0, label %right
+  i32 1, label %right
+  ], !prof !4
+
+right:
+  br i1 %PredRight, label %left, label %exit, !prof !2
+
+exit:
+  ret void
+}
+
+; Entry count 1, weights 1/1. Each half floors to 0, so the guard is unknown.
+define void @zero_scale(i1 %PredEntry, i1 %PredLeft) !prof !6 {
+; CHECK-LABEL: define void @zero_scale(
+; CHECK-SAME: i1 [[PREDENTRY:%.*]], i1 [[PREDLEFT:%.*]]) !prof [[PROF6:![0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[PREDENTRY_INV:%.*]] = xor i1 [[PREDENTRY]], true
+; CHECK-NEXT:    br label %[[IRR_GUARD:.*]]
+; CHECK:       [[LEFT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDLEFT]], label %[[IRR_GUARD]], label %[[EXIT:.*]], !prof [[PROF7:![0-9]+]]
+; CHECK:       [[RIGHT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDLEFT]], label %[[LEFT]], label %[[EXIT]], !prof [[PROF7]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[IRR_GUARD]]:
+; CHECK-NEXT:    [[GUARD_RIGHT:%.*]] = phi i1 [ true, %[[LEFT]] ], [ [[PREDENTRY_INV]], %[[ENTRY]] ]
+; CHECK-NEXT:    br i1 [[GUARD_RIGHT]], label %[[RIGHT]], label %[[LEFT]], !prof [[PROF8:![0-9]+]]
+;
+entry:
+  br i1 %PredEntry, label %left, label %right, !prof !5
+
+left:
+  br i1 %PredLeft, label %right, label %exit, !prof !5
+
+right:
+  br i1 %PredLeft, label %left, label %exit, !prof !5
+
+exit:
+  ret void
+}
+
+; dead never runs, so it adds 0. The guard stays 155 to right and 25 to left.
+define void @dead_pred(i1 %PredEntry, i1 %PredLeft, i1 %PredRight) !prof !3 {
+; CHECK-LABEL: define void @dead_pred(
+; CHECK-SAME: i1 [[PREDENTRY:%.*]], i1 [[PREDLEFT:%.*]], i1 [[PREDRIGHT:%.*]]) !prof [[PROF0]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[PREDENTRY_INV:%.*]] = xor i1 [[PREDENTRY]], true
+; CHECK-NEXT:    br label %[[IRR_GUARD:.*]]
+; CHECK:       [[LEFT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDLEFT]], label %[[IRR_GUARD]], label %[[EXIT:.*]], !prof [[PROF1]]
+; CHECK:       [[RIGHT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDRIGHT]], label %[[LEFT]], label %[[EXIT]], !prof [[PROF2]]
+; CHECK:       [[DEAD:.*]]:
+; CHECK-NEXT:    br label %[[IRR_GUARD]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[IRR_GUARD]]:
+; CHECK-NEXT:    [[GUARD_RIGHT:%.*]] = phi i1 [ true, %[[LEFT]] ], [ true, %[[DEAD]] ], [ [[PREDENTRY_INV]], %[[ENTRY]] ]
+; CHECK-NEXT:    br i1 [[GUARD_RIGHT]], label %[[RIGHT]], label %[[LEFT]], !prof [[PROF3]]
+;
+entry:
+  br i1 %PredEntry, label %left, label %right, !prof !0
+
+left:
+  br i1 %PredLeft, label %right, label %exit, !prof !1
+
+right:
+  br i1 %PredRight, label %left, label %exit, !prof !2
+
+dead:
+  br label %right
+
+exit:
+  ret void
+}
+
+; [entry] -> [l1] / [l2] / [l3]
+; First guard: [irr] -> [l3] or the rest, weight 62 / 50.
+; 62 = 25 + 37. entry arms are 100 * 1/4. l1 is 75; half floors to 37.
+; 50 = 25 + 25, entry to l1 and to l2.
+; That new [irr] feeds the next hub, so the inner guard is unknown.
+define void @later_hub(i32 %val, i1 %a, i1 %b, i1 %c) !prof !3 {
+; CHECK-LABEL: define void @later_hub(
+; CHECK-SAME: i32 [[VAL:%.*]], i1 [[A:%.*]], i1 [[B:%.*]], i1 [[C:%.*]]) !prof [[PROF0]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    switch i32 [[VAL]], label %[[ENTRY_TARGET_L1:.*]] [
+; CHECK-NEXT:      i32 0, label %[[ENTRY_TARGET_L2:.*]]
+; CHECK-NEXT:      i32 1, label %[[EXIT:.*]]
+; CHECK-NEXT:      i32 2, label %[[ENTRY_TARGET_L3:.*]]
+; CHECK-NEXT:    ], !prof [[PROF9:![0-9]+]]
+; CHECK:       [[L1:.*]]:
+; CHECK-NEXT:    br i1 [[A]], label %[[L2:.*]], label %[[IRR_GUARD:.*]], !prof [[PROF7]]
+; CHECK:       [[L2]]:
+; CHECK-NEXT:    br i1 [[B]], label %[[IRR_GUARD2:.*]], label %[[EXIT]], !prof [[PROF7]]
+; CHECK:       [[L3:.*]]:
+; CHECK-NEXT:    br i1 [[C]], label %[[IRR_GUARD2]], label %[[EXIT]], !prof [[PROF7]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[ENTRY_TARGET_L1]]:
+; CHECK-NEXT:    br label %[[IRR_GUARD]]
+; CHECK:       [[ENTRY_TARGET_L2]]:
+; CHECK-NEXT:    br label %[[IRR_GUARD]]
+; CHECK:       [[ENTRY_TARGET_L3]]:
+; CHECK-NEXT:    br label %[[IRR_GUARD]]
+; CHECK:       [[IRR_GUARD]]:
+; CHECK-NEXT:    [[GUARD_L3:%.*]] = phi i1 [ true, %[[L1]] ], [ false, %[[ENTRY_TARGET_L1]] ], [ false, %[[ENTRY_TARGET_L2]] ], [ true, %[[ENTRY_TARGET_L3]] ]
+; CHECK-NEXT:    [[GUARD_L1:%.*]] = phi i1 [ false, %[[L1]] ], [ true, %[[ENTRY_TARGET_L1]] ], [ false, %[[ENTRY_TARGET_L2]] ], [ false, %[[ENTRY_TARGET_L3]] ]
+; CHECK-NEXT:    br i1 [[GUARD_L3]], label %[[L3]], label %[[IRR_GUARD1:.*]], !prof [[PROF10:![0-9]+]]
+; CHECK:       [[IRR_GUARD1]]:
+; CHECK-NEXT:    br label %[[IRR_GUARD2]]
+; CHECK:       [[IRR_GUARD2]]:
+; CHECK-NEXT:    [[GUARD_L13:%.*]] = phi i1 [ true, %[[L2]] ], [ [[GUARD_L1]], %[[IRR_GUARD1]] ], [ true, %[[L3]] ]
+; CHECK-NEXT:    br i1 [[GUARD_L13]], label %[[L1]], label %[[L2]], !prof [[PROF8]]
+;
+entry:
+  switch i32 %val, label %l1 [
+  i32 0, label %l2
+  i32 1, label %exit
+  i32 2, label %l3
+  ], !prof !7
+l1:
+  br i1 %a, label %l2, label %l3, !prof !5
+l2:
+  br i1 %b, label %l1, label %exit, !prof !5
+l3:
+  br i1 %c, label %l1, label %exit, !prof !5
+exit:
+  ret void
+}
+
+; No entry count, so the guard uses block frequency. Same ratio as @weights.
+; Counts past 2^31 print as a negative i32.
+define void @frequency_no_entry(i1 %PredEntry, i1 %PredLeft, i1 %PredRight) {
+; CHECK-LABEL: define void @frequency_no_entry(
+; CHECK-SAME: i1 [[PREDENTRY:%.*]], i1 [[PREDLEFT:%.*]], i1 [[PREDRIGHT:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[PREDENTRY_INV:%.*]] = xor i1 [[PREDENTRY]], true
+; CHECK-NEXT:    br label %[[IRR_GUARD:.*]]
+; CHECK:       [[LEFT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDLEFT]], label %[[IRR_GUARD]], label %[[EXIT:.*]], !prof [[PROF1]]
+; CHECK:       [[RIGHT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDRIGHT]], label %[[LEFT]], label %[[EXIT]], !prof [[PROF2]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[IRR_GUARD]]:
+; CHECK-NEXT:    [[GUARD_RIGHT:%.*]] = phi i1 [ true, %[[LEFT]] ], [ [[PREDENTRY_INV]], %[[ENTRY]] ]
+; CHECK-NEXT:    br i1 [[GUARD_RIGHT]], label %[[RIGHT]], label %[[LEFT]], !prof [[PROF11:![0-9]+]]
+;
+entry:
+  br i1 %PredEntry, label %left, label %right, !prof !0
+
+left:
+  br i1 %PredLeft, label %right, label %exit, !prof !1
+
+right:
+  br i1 %PredRight, label %left, label %exit, !prof !2
+
+exit:
+  ret void
+}
+
+; Entry count 4, weight 1 of 1000001. [entry] -> [left] rounds to 0,
+; so [guard] -> [right] / [left] is unknown.
+define void @floor_one_edge(i1 %PredEntry, i1 %PredLeft, i1 %PredRight) !prof !11 {
+; CHECK-LABEL: define void @floor_one_edge(
+; CHECK-SAME: i1 [[PREDENTRY:%.*]], i1 [[PREDLEFT:%.*]], i1 [[PREDRIGHT:%.*]]) !prof [[PROF14:![0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[PREDENTRY_INV:%.*]] = xor i1 [[PREDENTRY]], true
+; CHECK-NEXT:    br label %[[IRR_GUARD:.*]]
+; CHECK:       [[LEFT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDLEFT]], label %[[IRR_GUARD]], label %[[EXIT:.*]], !prof [[PROF1]]
+; CHECK:       [[RIGHT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDRIGHT]], label %[[LEFT]], label %[[EXIT]], !prof [[PROF2]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[IRR_GUARD]]:
+; CHECK-NEXT:    [[GUARD_RIGHT:%.*]] = phi i1 [ true, %[[LEFT]] ], [ [[PREDENTRY_INV]], %[[ENTRY]] ]
+; CHECK-NEXT:    br i1 [[GUARD_RIGHT]], label %[[RIGHT]], label %[[LEFT]], !prof [[PROF8]]
+;
+entry:
+  br i1 %PredEntry, label %left, label %right, !prof !12
+
+left:
+  br i1 %PredLeft, label %right, label %exit, !prof !1
+
+right:
+  br i1 %PredRight, label %left, label %exit, !prof !2
+
+exit:
+  ret void
+}
+
+; Both of side's arms target right, with weights 1 and 1000000. The small arm
+; would round to 0, so side's whole count is used.
+; 250 = 50 + 200. side is 50, left is 200. 50 to left is entry's other half.
+define void @same_target(i1 %PredEntry, i1 %PredSide, i1 %PredRight) !prof !3 {
+; CHECK-LABEL: define void @same_target(
+; CHECK-SAME: i1 [[PREDENTRY:%.*]], i1 [[PREDSIDE:%.*]], i1 [[PREDRIGHT:%.*]]) !prof [[PROF0]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br i1 [[PREDENTRY]], label %[[IRR_GUARD:.*]], label %[[SIDE:.*]], !prof [[PROF7]]
+; CHECK:       [[SIDE]]:
+; CHECK-NEXT:    br label %[[IRR_GUARD]]
+; CHECK:       [[LEFT:.*]]:
+; CHECK-NEXT:    br label %[[IRR_GUARD]]
+; CHECK:       [[RIGHT:.*]]:
+; CHECK-NEXT:    br i1 [[PREDRIGHT]], label %[[LEFT]], label %[[EXIT:.*]], !prof [[PROF7]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[IRR_GUARD]]:
+; CHECK-NEXT:    [[GUARD_RIGHT:%.*]] = phi i1 [ true, %[[LEFT]] ], [ true, %[[SIDE]] ], [ false, %[[ENTRY]] ]
+; CHECK-NEXT:    br i1 [[GUARD_RIGHT]], label %[[RIGHT]], label %[[LEFT]], !prof [[PROF15:![0-9]+]]
+;
+entry:
+  br i1 %PredEntry, label %left, label %side, !prof !5
+
+side:
+  br i1 %PredSide, label %right, label %right, !prof !12
+
+left:
+  br label %right
+
+right:
+  br i1 %PredRight, label %left, label %exit, !prof !5
+
+exit:
+  ret void
+}
+
+!0 = !{!"branch_weights", i32 10, i32 30}
+!11 = !{!"function_entry_count", i64 4}
+!12 = !{!"branch_weights", i32 1, i32 1000000}
+!7 = !{!"branch_weights", i32 1, i32 1, i32 1, i32 1}
+!5 = !{!"branch_weights", i32 1, i32 1}
+!6 = !{!"function_entry_count", i64 1}
+!4 = !{!"branch_weights", i32 1, i32 2, i32 2}
+!1 = !{!"branch_weights", i32 5, i32 5}
+!2 = !{!"branch_weights", i32 7, i32 1}
+!3 = !{!"function_entry_count", i64 100}
+;.
+; CHECK: [[PROF0]] = !{!"function_entry_count", i64 100}
+; CHECK: [[PROF1]] = !{!"branch_weights", i32 5, i32 5}
+; CHECK: [[PROF2]] = !{!"branch_weights", i32 7, i32 1}
+; CHECK: [[PROF3]] = !{!"branch_weights", i32 155, i32 25}
+; CHECK: [[PROF4]] = !{!"branch_weights", i32 1, i32 2, i32 2}
+; CHECK: [[PROF5]] = !{!"branch_weights", i32 321, i32 25}
+; CHECK: [[PROF6]] = !{!"function_entry_count", i64 1}
+; CHECK: [[PROF7]] = !{!"branch_weights", i32 1, i32 1}
+; CHECK: [[PROF8]] = !{!"unknown", !"control-flow-hub"}
+; CHECK: [[PROF9]] = !{!"branch_weights", i32 1, i32 1, i32 1, i32 1}
+; CHECK: [[PROF10]] = !{!"branch_weights", i32 62, i32 50}
+; CHECK: [[PROF11]] = !{!"branch_weights", i32 -134217729, i32 671088639}
+; CHECK: [[PROF14]] = !{!"function_entry_count", i64 4}
+; CHECK: [[PROF15]] = !{!"branch_weights", i32 250, i32 50}
+;.
diff --git a/llvm/test/Transforms/UnifyLoopExits/branch-weights.ll b/llvm/test/Transforms/UnifyLoopExits/branch-weights.ll
new file mode 100644
index 00000000000000..6f160fcbf05a16
--- /dev/null
+++ b/llvm/test/Transforms/UnifyLoopExits/branch-weights.ll
@@ -0,0 +1,205 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=unify-loop-exits -S | FileCheck %s
+
+; Before. Entry count 100. Labels are absolute counts.
+; [latch] branches back to [header].
+;
+;                      +------------------------+
+;                      v                        |
+; [entry] --100--> [header] --120--> [latch]----+
+;                     |                 |
+;                    40                60
+;                     v                 v
+;                 [exit.a]          [exit.b]
+;
+; header = 100 + 60 = 160
+; header->latch = 160 * 3/4 = 120
+; header->exit.a = 160 * 1/4 = 40
+; latch->header = latch->exit.b = 120 * 1/2 = 60
+;
+; After. Both exits go through [guard]. The back edge stays.
+; In: 40 + 60 = 100. Out: 40 + 60 = 100.
+; 40 is header->exit.a. 60 is latch->exit.b.
+;
+;                      +------------------------+
+;                      v                        |
+; [entry] --100--> [header] --120--> [latch]----+
+;                     |                 |
+;                    40                60
+;                     +--------+--------+
+;                              v
+;                          [guard] --40--> [exit.a]
+;                             |
+;                            60
+;                             v
+;                         [exit.b]
+define void @weights(i1 %PredHeader, i1 %PredLatch) !prof !2 {
+; CHECK-LABEL: define void @weights(
+; CHECK-SAME: i1 [[PREDHEADER:%.*]], i1 [[PREDLATCH:%.*]]) !prof [[PROF0:![0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br label %[[HEADER:.*]]
+; CHECK:       [[HEADER]]:
+; CHECK-NEXT:    br i1 [[PREDHEADER]], label %[[LATCH:.*]], label %[[LOOP_EXIT_GUARD:.*]], !prof [[PROF1:![0-9]+]]
+; CHECK:       [[LATCH]]:
+; CHECK-NEXT:    br i1 [[PREDLATCH]], label %[[HEADER]], label %[[LOOP_EXIT_GUARD]], !prof [[PROF2:![0-9]+]]
+; CHECK:       [[EXIT_A:.*]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[EXIT_B:.*]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[LOOP_EXIT_GUARD]]:
+; CHECK-NEXT:    [[GUARD_EXIT_A:%.*]] = phi i1 [ true, %[[HEADER]] ], [ false, %[[LATCH]] ]
+; CHECK-NEXT:    br i1 [[GUARD_EXIT_A]], label %[[EXIT_A]], label %[[EXIT_B]], !prof [[PROF3:![0-9]+]]
+;
+entry:
+  br label %header
+
+header:
+  br i1 %PredHeader, label %latch, label %exit.a, !prof !0
+
+latch:
+  br i1 %PredLatch, label %header, label %exit.b, !prof !1
+
+exit.a:
+  ret void
+
+exit.b:
+  ret void
+}
+
+; [header] switch -> [.target.a] -> [guard] -> [exit.a]
+;                 \-> [.target.b] -+        \-> [exit.b]
+; Cases 1 and 2 share .target.b. Guard is 20 / 79.
+; header is 200. exit.a is weight 1 of 10: 20.
+; exit.b cases are 4 of 10. 200 * 4/10 floors to 79.
+define void @shared_switch_target(i32 %X) !prof !2 {
+; CHECK-LABEL: define void @shared_switch_target(
+; CHECK-SAME: i32 [[X:%.*]]) !prof [[PROF0]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br label %[[HEADER:.*]]
+; CHECK:       [[HEADER]]:
+; CHECK-NEXT:    switch i32 [[X]], label %[[LATCH:.*]] [
+; CHECK-NEXT:      i32 0, label %[[HEADER_TARGET_EXIT_A:.*]]
+; CHECK-NEXT:      i32 1, label %[[HEADER_TARGET_EXIT_B:.*]]
+; CHECK-NEXT:      i32 2, label %[[HEADER_TARGET_EXIT_B]]
+; CHECK-NEXT:    ], !prof [[PROF4:![0-9]+]]
+; CHECK:       [[LATCH]]:
+; CHECK-NEXT:    br label %[[HEADER]]
+; CHECK:       [[EXIT_A:.*]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[EXIT_B:.*]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[HEADER_TARGET_EXIT_A]]:
+; CHECK-NEXT:    br label %[[LOOP_EXIT_GUARD:.*]]
+; CHECK:       [[HEADER_TARGET_EXIT_B]]:
+; CHECK-NEXT:    br label %[[LOOP_EXIT_GUARD]]
+; CHECK:       [[LOOP_EXIT_GUARD]]:
+; CHECK-NEXT:    [[GUARD_EXIT_A:%.*]] = phi i1 [ true, %[[HEADER_TARGET_EXIT_A]] ], [ false, %[[HEADER_TARGET_EXIT_B]] ]
+; CHECK-NEXT:    br i1 [[GUARD_EXIT_A]], label %[[EXIT_A]], label %[[EXIT_B]], !prof [[PROF5:![0-9]+]]
+;
+entry:
+  br label %header
+
+header:
+  switch i32 %X, label %latch [
+  i32 0, label %exit.a
+  i32 1, label %exit.b
+  i32 2, label %exit.b
+  ], !prof !3
+
+latch:
+  br label %header
+
+exit.a:
+  ret void
+
+exit.b:
+  ret void
+}
+
+; No entry count, so the guard uses block frequency. Same 2:3 ratio as @weights.
+; Counts past 2^31 print as a negative i32.
+define void @frequency_no_entry(i1 %PredHeader, i1 %PredLatch) {
+; CHECK-LABEL: define void @frequency_no_entry(
+; CHECK-SAME: i1 [[PREDHEADER:%.*]], i1 [[PREDLATCH:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br label %[[HEADER:.*]]
+; CHECK:       [[HEADER]]:
+; CHECK-NEXT:    br i1 [[PREDHEADER]], label %[[LATCH:.*]], label %[[LOOP_EXIT_GUARD:.*]], !prof [[PROF1]]
+; CHECK:       [[LATCH]]:
+; CHECK-NEXT:    br i1 [[PREDLATCH]], label %[[HEADER]], label %[[LOOP_EXIT_GUARD]], !prof [[PROF2]]
+; CHECK:       [[EXIT_A:.*]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[EXIT_B:.*]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[LOOP_EXIT_GUARD]]:
+; CHECK-NEXT:    [[GUARD_EXIT_A:%.*]] = phi i1 [ true, %[[HEADER]] ], [ false, %[[LATCH]] ]
+; CHECK-NEXT:    br i1 [[GUARD_EXIT_A]], label %[[EXIT_A]], label %[[EXIT_B]], !prof [[PROF6:![0-9]+]]
+;
+entry:
+  br label %header
+
+header:
+  br i1 %PredHeader, label %latch, label %exit.a, !prof !0
+
+latch:
+  br i1 %PredLatch, label %header, label %exit.b, !prof !1
+
+exit.a:
+  ret void
+
+exit.b:
+  ret void
+}
+
+; header is 8. weight 1 of 1000001 rounds to 0, so the guard is unknown.
+define void @floor_one_edge(i1 %PredHeader, i1 %PredLatch) !prof !6 {
+; CHECK-LABEL: define void @floor_one_edge(
+; CHECK-SAME: i1 [[PREDHEADER:%.*]], i1 [[PREDLATCH:%.*]]) !prof [[PROF7:![0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br label %[[HEADER:.*]]
+; CHECK:       [[HEADER]]:
+; CHECK-NEXT:    br i1 [[PREDHEADER]], label %[[LATCH:.*]], label %[[LOOP_EXIT_GUARD:.*]], !prof [[PROF8:![0-9]+]]
+; CHECK:       [[LATCH]]:
+; CHECK-NEXT:    br i1 [[PREDLATCH]], label %[[HEADER]], label %[[LOOP_EXIT_GUARD]], !prof [[PROF2]]
+; CHECK:       [[EXIT_A:.*]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[EXIT_B:.*]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[LOOP_EXIT_GUARD]]:
+; CHECK-NEXT:    [[GUARD_EXIT_A:%.*]] = phi i1 [ true, %[[HEADER]] ], [ false, %[[LATCH]] ]
+; CHECK-NEXT:    br i1 [[GUARD_EXIT_A]], label %[[EXIT_A]], label %[[EXIT_B]], !prof [[PROF9:![0-9]+]]
+;
+entry:
+  br label %header
+
+header:
+  br i1 %PredHeader, label %latch, label %exit.a, !prof !7
+
+latch:
+  br i1 %PredLatch, label %header, label %exit.b, !prof !1
+
+exit.a:
+  ret void
+
+exit.b:
+  ret void
+}
+
+!0 = !{!"branch_weights", i32 3, i32 1}
+!6 = !{!"function_entry_count", i64 4}
+!7 = !{!"branch_weights", i32 1000000, i32 1}
+!3 = !{!"branch_weights", i32 5, i32 1, i32 2, i32 2}
+!1 = !{!"branch_weights", i32 1, i32 1}
+!2 = !{!"function_entry_count", i64 100}
+;.
+; CHECK: [[PROF0]] = !{!"function_entry_count", i64 100}
+; CHECK: [[PROF1]] = !{!"branch_weights", i32 3, i32 1}
+; CHECK: [[PROF2]] = !{!"branch_weights", i32 1, i32 1}
+; CHECK: [[PROF3]] = !{!"branch_weights", i32 40, i32 60}
+; CHECK: [[PROF4]] = !{!"branch_weights", i32 5, i32 1, i32 2, i32 2}
+; CHECK: [[PROF5]] = !{!"branch_weights", i32 20, i32 79}
+; CHECK: [[PROF6]] = !{!"branch_weights", i32 -2147483648, i32 -1073741824}
+; CHECK: [[PROF7]] = !{!"function_entry_count", i64 4}
+; CHECK: [[PROF8]] = !{!"branch_weights", i32 1000000, i32 1}
+; CHECK: [[PROF9]] = !{!"unknown", !"control-flow-hub"}
+;.



More information about the llvm-commits mailing list