[llvm] [Target] Add const MBB overload to analyzeBranch (PR #221847)
Aiden Grossman via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 7 15:56:50 PDT 2026
https://github.com/boomanaiden154 created https://github.com/llvm/llvm-project/pull/221847
There are a couple of callsites that call analyzeBranch with AllowModify set to false with a const_cast. Add a const overload that internally uses a const_cast to better enforce invariants, particularly setting AllowModify to false and not producing non-const references from a const reference.
>From 874a59b6cf5fbe9cd9051f1e541bd117f553dfa9 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Mon, 7 Sep 2026 22:55:06 +0000
Subject: [PATCH] [Target] Add const MBB overload to analyzeBranch
There are a couple of callsites that call analyzeBranch with
AllowModify set to false with a const_cast. Add a const overload that
internally uses a const_cast to better enforce invariants, particularly
setting AllowModify to false and not producing non-const references from
a const reference.
---
llvm/include/llvm/CodeGen/TargetInstrInfo.h | 13 +++++++++++++
llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp | 16 ++++++++--------
llvm/lib/CodeGen/MachineBasicBlock.cpp | 5 ++---
llvm/lib/CodeGen/MachineVerifier.cpp | 5 ++---
llvm/lib/Target/Hexagon/HexagonInstrInfo.h | 2 ++
llvm/lib/Target/Hexagon/HexagonSplitDouble.cpp | 7 +++----
6 files changed, 30 insertions(+), 18 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index 2134b5e15146c..f27261e7be1aa 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -743,6 +743,19 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
return true;
}
+ bool analyzeBranch(const MachineBasicBlock &MBB,
+ const MachineBasicBlock *&TBB,
+ const MachineBasicBlock *&FBB,
+ SmallVectorImpl<MachineOperand> &Cond) const {
+ MachineBasicBlock *TempTBB = nullptr, *TempFBB = nullptr;
+ bool NotUnderstandable = analyzeBranch(const_cast<MachineBasicBlock &>(MBB),
+ TempTBB, TempFBB, Cond,
+ /*AllowModify=*/false);
+ TBB = TempTBB;
+ FBB = TempFBB;
+ return NotUnderstandable;
+ }
+
/// Represents a predicate at the MachineFunction level. The control flow a
/// MachineBranchPredicate represents is:
///
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 7e96872c65ac3..b5828314590a5 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -2724,10 +2724,9 @@ void DwarfDebug::findForceIsStmtInstrs(const MachineFunction *MF) {
// We only need to the predecessors of MBBs that could have is_stmt set by
// this logic.
SmallDenseSet<MachineBasicBlock *, 4> PredMBBsToExamine;
- SmallDenseMap<MachineBasicBlock *, MachineInstr *> PotentialIsStmtMBBInstrs;
- // We use const_cast even though we won't actually modify MF, because some
- // methods we need take a non-const MBB.
- for (auto &MBB : *const_cast<MachineFunction *>(MF)) {
+ SmallDenseMap<const MachineBasicBlock *, const MachineInstr *>
+ PotentialIsStmtMBBInstrs;
+ for (const auto &MBB : *MF) {
if (MBB.empty() || MBB.pred_empty())
continue;
for (auto &MI : MBB) {
@@ -2745,11 +2744,12 @@ void DwarfDebug::findForceIsStmtInstrs(const MachineFunction *MF) {
// multiple branches that each have their own source location); otherwise we
// just use the last line in the block.
for (auto *MBB : PredMBBsToExamine) {
- auto CheckMBBEdge = [&](MachineBasicBlock *Succ, unsigned OutgoingLine) {
+ auto CheckMBBEdge = [&](const MachineBasicBlock *Succ,
+ unsigned OutgoingLine) {
auto MBBInstrIt = PotentialIsStmtMBBInstrs.find(Succ);
if (MBBInstrIt == PotentialIsStmtMBBInstrs.end())
return;
- MachineInstr *MI = MBBInstrIt->second;
+ const MachineInstr *MI = MBBInstrIt->second;
if (MI->getDebugLoc()->getLine() == OutgoingLine)
return;
PotentialIsStmtMBBInstrs.erase(MBBInstrIt);
@@ -2771,10 +2771,10 @@ void DwarfDebug::findForceIsStmtInstrs(const MachineFunction *MF) {
continue;
// If we can't determine what DLs this branch's successors use, just treat
// all the successors as coming from the last DebugLoc.
- SmallVector<MachineBasicBlock *, 2> SuccessorBBs;
+ SmallVector<const MachineBasicBlock *, 2> SuccessorBBs;
auto MIIt = MBB->rbegin();
{
- MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
+ const MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
SmallVector<MachineOperand, 4> Cond;
bool AnalyzeFailed = TII->analyzeBranch(*MBB, TBB, FBB, Cond);
// For a conditional branch followed by unconditional branch where the
diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp
index b58a11efd103c..736e455740547 100644
--- a/llvm/lib/CodeGen/MachineBasicBlock.cpp
+++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp
@@ -1458,11 +1458,10 @@ bool MachineBasicBlock::canSplitCriticalEdge(const MachineBasicBlock *Succ,
// We may need to update this's terminator, but we can't do that if
// analyzeBranch fails.
const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
- MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
+ const MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
SmallVector<MachineOperand, 4> Cond;
// AnalyzeBanch should modify this, since we did not allow modification.
- if (TII->analyzeBranch(*const_cast<MachineBasicBlock *>(this), TBB, FBB, Cond,
- /*AllowModify*/ false))
+ if (TII->analyzeBranch(*this, TBB, FBB, Cond))
return false;
// Handle weird inputs (e.g., generated by a test case reducer/fuzzer): A
diff --git a/llvm/lib/CodeGen/MachineVerifier.cpp b/llvm/lib/CodeGen/MachineVerifier.cpp
index 0d3015e098cf2..072b5e61d04fd 100644
--- a/llvm/lib/CodeGen/MachineVerifier.cpp
+++ b/llvm/lib/CodeGen/MachineVerifier.cpp
@@ -800,10 +800,9 @@ MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
report("MBB has more than one landing pad successor", MBB);
// Call analyzeBranch. If it succeeds, there several more conditions to check.
- MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
+ const MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
SmallVector<MachineOperand, 4> Cond;
- if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
- Cond)) {
+ if (!TII->analyzeBranch(*MBB, TBB, FBB, Cond)) {
// Ok, analyzeBranch thinks it knows what's going on with this block. Let's
// check whether its answers match up with reality.
if (!TBB && !FBB) {
diff --git a/llvm/lib/Target/Hexagon/HexagonInstrInfo.h b/llvm/lib/Target/Hexagon/HexagonInstrInfo.h
index 1901b260926d2..ef9bf7052c48e 100644
--- a/llvm/lib/Target/Hexagon/HexagonInstrInfo.h
+++ b/llvm/lib/Target/Hexagon/HexagonInstrInfo.h
@@ -87,6 +87,8 @@ class HexagonInstrInfo : public HexagonGenInstrInfo {
const MachineInstr &MI,
SmallVectorImpl<const MachineMemOperand *> &Accesses) const override;
+ using TargetInstrInfo::analyzeBranch;
+
/// Analyze the branching code at the end of MBB, returning
/// true if it cannot be understood (e.g. it's a switch dispatch or isn't
/// implemented for a target). Upon success, this returns false and returns
diff --git a/llvm/lib/Target/Hexagon/HexagonSplitDouble.cpp b/llvm/lib/Target/Hexagon/HexagonSplitDouble.cpp
index 6a901013106d8..b57aeb4541b4c 100644
--- a/llvm/lib/Target/Hexagon/HexagonSplitDouble.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonSplitDouble.cpp
@@ -474,10 +474,9 @@ void HexagonSplitDoubleRegs::collectIndRegsForLoop(const MachineLoop *L,
// Examine the latch branch. Expect it to be a conditional branch to
// the header (either "br-cond header" or "br-cond exit; br header").
- MachineBasicBlock *TB = nullptr, *FB = nullptr;
- MachineBasicBlock *TmpLB = const_cast<MachineBasicBlock*>(LB);
- SmallVector<MachineOperand,2> Cond;
- bool BadLB = TII->analyzeBranch(*TmpLB, TB, FB, Cond, false);
+ const MachineBasicBlock *TB = nullptr, *FB = nullptr;
+ SmallVector<MachineOperand, 2> Cond;
+ bool BadLB = TII->analyzeBranch(*LB, TB, FB, Cond);
// Only analyzable conditional branches. HII::analyzeBranch will put
// the branch opcode as the first element of Cond, and the predicate
// operand as the second.
More information about the llvm-commits
mailing list