Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MachineBasicBlock] Fix use after free in SplitCriticalEdge #66188

Closed
wants to merge 2 commits into from

Conversation

perlfu
Copy link
Contributor

@perlfu perlfu commented Sep 13, 2023

Remove use after free when attempting to update SlotIndexes in MachineBasicBlock::SplitCriticalEdge.
Add support to targets for updating SlotIndexes when inserting or removing branches.

Remove use after free when attempting to update SlotIndexes
in MachineBasicBlock::SplitCriticalEdge.
Add support to targets for updating SlotIndexes when inserting or
removing branches.
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 13, 2023

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-backend-msp430

Changes Remove use after free when attempting to update SlotIndexes in MachineBasicBlock::SplitCriticalEdge. Add support to targets for updating SlotIndexes when inserting or removing branches. --

Patch is 127.62 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/66188.diff

51 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/MachineBasicBlock.h (+2-1)
  • (modified) llvm/include/llvm/CodeGen/TargetInstrInfo.h (+8-5)
  • (modified) llvm/lib/CodeGen/MachineBasicBlock.cpp (+21-48)
  • (modified) llvm/lib/Target/AArch64/AArch64InstrInfo.cpp (+37-15)
  • (modified) llvm/lib/Target/AArch64/AArch64InstrInfo.h (+6-5)
  • (modified) llvm/lib/Target/AMDGPU/R600InstrInfo.cpp (+29-12)
  • (modified) llvm/lib/Target/AMDGPU/R600InstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/AMDGPU/SIInstrInfo.cpp (+26-16)
  • (modified) llvm/lib/Target/AMDGPU/SIInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/ARC/ARCInstrInfo.cpp (+17-5)
  • (modified) llvm/lib/Target/ARC/ARCInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp (+33-17)
  • (modified) llvm/lib/Target/ARM/ARMBaseInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/AVR/AVRInstrInfo.cpp (+13-3)
  • (modified) llvm/lib/Target/AVR/AVRInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/BPF/BPFInstrInfo.cpp (+10-5)
  • (modified) llvm/lib/Target/BPF/BPFInstrInfo.h (+5-4)
  • (modified) llvm/lib/Target/CSKY/CSKYInstrInfo.cpp (+19-5)
  • (modified) llvm/lib/Target/CSKY/CSKYInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/Hexagon/HexagonInstrInfo.cpp (+41-16)
  • (modified) llvm/lib/Target/Hexagon/HexagonInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/Lanai/LanaiInstrInfo.cpp (+18-7)
  • (modified) llvm/lib/Target/Lanai/LanaiInstrInfo.h (+5-5)
  • (modified) llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp (+19-4)
  • (modified) llvm/lib/Target/LoongArch/LoongArchInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/M68k/M68kInstrInfo.cpp (+20-8)
  • (modified) llvm/lib/Target/M68k/M68kInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/MSP430/MSP430InstrInfo.cpp (+17-6)
  • (modified) llvm/lib/Target/MSP430/MSP430InstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/Mips/MipsInstrInfo.cpp (+21-11)
  • (modified) llvm/lib/Target/Mips/MipsInstrInfo.h (+6-5)
  • (modified) llvm/lib/Target/NVPTX/NVPTXInstrInfo.cpp (+21-8)
  • (modified) llvm/lib/Target/NVPTX/NVPTXInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/PowerPC/PPCInstrInfo.cpp (+38-24)
  • (modified) llvm/lib/Target/PowerPC/PPCInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/RISCV/RISCVInstrInfo.cpp (+18-5)
  • (modified) llvm/lib/Target/RISCV/RISCVInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/SPIRV/SPIRVInstrInfo.cpp (+8-5)
  • (modified) llvm/lib/Target/SPIRV/SPIRVInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/Sparc/SparcInstrInfo.cpp (+19-8)
  • (modified) llvm/lib/Target/Sparc/SparcInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/SystemZ/SystemZInstrInfo.cpp (+18-7)
  • (modified) llvm/lib/Target/SystemZ/SystemZInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/VE/VEInstrInfo.cpp (+27-17)
  • (modified) llvm/lib/Target/VE/VEInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.cpp (+27-10)
  • (modified) llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/X86/X86InstrInfo.cpp (+25-19)
  • (modified) llvm/lib/Target/X86/X86InstrInfo.h (+4-4)
  • (modified) llvm/lib/Target/XCore/XCoreInstrInfo.cpp (+21-10)
  • (modified) llvm/lib/Target/XCore/XCoreInstrInfo.h (+4-4)

<pre>
diff --git a/llvm/include/llvm/CodeGen/MachineBasicBlock.h b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
index ed9fc8f7ec3d75e..74e7b98ca8a7526 100644
--- a/llvm/include/llvm/CodeGen/MachineBasicBlock.h
+++ b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
@@ -693,7 +693,8 @@ class MachineBasicBlock
/// layout was modified. If the block previously fell through to that block,
/// it may now need a branch. If it previously branched to another block, it
/// may now be able to fallthrough to the current layout successor.

  • void updateTerminator(MachineBasicBlock *PreviousLayoutSuccessor);
  • void updateTerminator(MachineBasicBlock *PreviousLayoutSuccessor,

  •                    SlotIndexes *Indexes = nullptr);
    

    // Machine-CFG mutators

diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index 1c2ca8678346472..8d40b8de8535f98 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -53,6 +53,7 @@ class ScheduleDAGMI;
class ScheduleHazardRecognizer;
class SDNode;
class SelectionDAG;
+class SlotIndexes;
class SMSchedule;
class SwingSchedulerDAG;
class RegScavenger;
@@ -682,7 +683,8 @@ class TargetInstrInfo : public MCInstrInfo {
/// If \p BytesRemoved is non-null, report the change in code size from the
/// removed instructions.
virtual unsigned removeBranch(MachineBasicBlock &amp;MBB,

  •                            int *BytesRemoved = nullptr) const {
    
  •                            int *BytesRemoved = nullptr,
    
  •                            SlotIndexes *Indexes = nullptr) const {
    
    llvm_unreachable(&quot;Target didn&#x27;t implement TargetInstrInfo::removeBranch!&quot;);
    }

@@ -702,17 +704,18 @@ class TargetInstrInfo : public MCInstrInfo {
virtual unsigned insertBranch(MachineBasicBlock &amp;MBB, MachineBasicBlock *TBB,
MachineBasicBlock *FBB,
ArrayRef&lt;MachineOperand&gt; Cond,

  •                            const DebugLoc &amp;amp;DL,
    
  •                            int *BytesAdded = nullptr) const {
    
  •                            const DebugLoc &amp;amp;DL, int *BytesAdded = nullptr,
    
  •                            SlotIndexes *Indexes = nullptr) const {
    

    llvm_unreachable(&quot;Target didn&#x27;t implement TargetInstrInfo::insertBranch!&quot;);
    }

    unsigned insertUnconditionalBranch(MachineBasicBlock &amp;MBB,
    MachineBasicBlock *DestBB,
    const DebugLoc &amp;DL,

  •                                 int *BytesAdded = nullptr) const {
    
  •                                 int *BytesAdded = nullptr,
    
  •                                 SlotIndexes *Indexes = nullptr) const {
    
    return insertBranch(MBB, DestBB, nullptr, ArrayRef&lt;MachineOperand&gt;(), DL,
  •                    BytesAdded);
    
  •                    BytesAdded, Indexes);
    

    }

    /// Object returned by analyzeLoopForPipelining. Allows software pipelining
    diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp
    index 280ced65db7d8c0..09df32afb37c00c 100644
    --- a/llvm/lib/CodeGen/MachineBasicBlock.cpp
    +++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp
    @@ -681,7 +681,7 @@ static int findJumpTableIndex(const MachineBasicBlock &amp;MBB) {
    }

void MachineBasicBlock::updateTerminator(

  • MachineBasicBlock *PreviousLayoutSuccessor) {
  • MachineBasicBlock *PreviousLayoutSuccessor, SlotIndexes *Indexes) {
    LLVM_DEBUG(dbgs() &lt;&lt; &quot;Updating terminators on &quot; &lt;&lt; printMBBReference(*this)
    &lt;&lt; &quot;\n&quot;);

@@ -693,7 +693,7 @@ void MachineBasicBlock::updateTerminator(
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
SmallVector&lt;MachineOperand, 4&gt; Cond;
DebugLoc DL = findBranchDebugLoc();

  • bool B = TII-&gt;analyzeBranch(*this, TBB, FBB, Cond);
  • bool B = TII-&gt;analyzeBranch(*this, TBB, FBB, Cond, /AllowModify=/false);
    (void) B;
    assert(!B &amp;&amp; &quot;UpdateTerminators requires analyzable predecessors!&quot;);
    if (Cond.empty()) {
    @@ -701,7 +701,7 @@ void MachineBasicBlock::updateTerminator(
    // The block has an unconditional branch. If its successor is now its
    // layout successor, delete the branch.
    if (isLayoutSuccessor(TBB))
  •    TII-&amp;gt;removeBranch(*this);
    
  •    TII-&amp;gt;removeBranch(*this, nullptr, Indexes);
    
    } else {
    // The block has an unconditional fallthrough, or the end of the block is
    // unreachable.
    @@ -717,7 +717,8 @@ void MachineBasicBlock::updateTerminator(
    // If the unconditional successor block is not the current layout
    // successor, insert a branch to jump to it.
    if (!isLayoutSuccessor(PreviousLayoutSuccessor))
  •    TII-&amp;gt;insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL);
    
  •    TII-&amp;gt;insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL,
    
  •                      nullptr, Indexes);
    
    }
    return;
    }
    @@ -729,11 +730,11 @@ void MachineBasicBlock::updateTerminator(
    if (isLayoutSuccessor(TBB)) {
    if (TII-&gt;reverseBranchCondition(Cond))
    return;
  •  TII-&amp;gt;removeBranch(*this);
    
  •  TII-&amp;gt;insertBranch(*this, FBB, nullptr, Cond, DL);
    
  •  TII-&amp;gt;removeBranch(*this, nullptr, Indexes);
    
  •  TII-&amp;gt;insertBranch(*this, FBB, nullptr, Cond, DL, nullptr, Indexes);
    
    } else if (isLayoutSuccessor(FBB)) {
  •  TII-&amp;gt;removeBranch(*this);
    
  •  TII-&amp;gt;insertBranch(*this, TBB, nullptr, Cond, DL);
    
  •  TII-&amp;gt;removeBranch(*this, nullptr, Indexes);
    
  •  TII-&amp;gt;insertBranch(*this, TBB, nullptr, Cond, DL, nullptr, Indexes);
    
    }
    return;
    }
    @@ -747,10 +748,10 @@ void MachineBasicBlock::updateTerminator(
    // We had a fallthrough to the same basic block as the conditional jump
    // targets. Remove the conditional jump, leaving an unconditional
    // fallthrough or an unconditional jump.
  • TII-&gt;removeBranch(*this);
  • TII-&gt;removeBranch(*this, nullptr, Indexes);
    if (!isLayoutSuccessor(TBB)) {
    Cond.clear();
  •  TII-&amp;gt;insertBranch(*this, TBB, nullptr, Cond, DL);
    
  •  TII-&amp;gt;insertBranch(*this, TBB, nullptr, Cond, DL, nullptr, Indexes);
    
    }
    return;
    }
    @@ -760,14 +761,17 @@ void MachineBasicBlock::updateTerminator(
    if (TII-&gt;reverseBranchCondition(Cond)) {
    // We can&#x27;t reverse the condition, add an unconditional branch.
    Cond.clear();
  •  TII-&amp;gt;insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL);
    
  •  TII-&amp;gt;insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL,
    
  •                    nullptr, Indexes);
     return;
    
    }
  • TII-&gt;removeBranch(*this);
  • TII-&gt;insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL);
  • TII-&gt;removeBranch(*this, nullptr, Indexes);
  • TII-&gt;insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL,
  •                  nullptr, Indexes);
    
    } else if (!isLayoutSuccessor(PreviousLayoutSuccessor)) {
  • TII-&gt;removeBranch(*this);
  • TII-&gt;insertBranch(*this, TBB, PreviousLayoutSuccessor, Cond, DL);
  • TII-&gt;removeBranch(*this, nullptr, Indexes);
  • TII-&gt;insertBranch(*this, TBB, PreviousLayoutSuccessor, Cond, DL, nullptr,
  •                  Indexes);
    
    }
    }

@@ -1166,51 +1170,20 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(

ReplaceUsesOfBlockWith(Succ, NMBB);

  • // If updateTerminator() removes instructions, we need to remove them from

  • // SlotIndexes.

  • SmallVector&lt;MachineInstr*, 4&gt; Terminators;

  • if (Indexes) {

  • for (MachineInstr &amp;MI :

  •     llvm::make_range(getFirstInstrTerminator(), instr_end()))
    
  •  Terminators.push_back(&amp;amp;MI);
    
  • }

  • // Since we replaced all uses of Succ with NMBB, that should also be treated
    // as the fallthrough successor
    if (Succ == PrevFallthrough)
    PrevFallthrough = NMBB;

    if (!ChangedIndirectJump)

  • updateTerminator(PrevFallthrough);

  • if (Indexes) {

  • SmallVector&lt;MachineInstr*, 4&gt; NewTerminators;

  • for (MachineInstr &amp;MI :

  •     llvm::make_range(getFirstInstrTerminator(), instr_end()))
    
  •  NewTerminators.push_back(&amp;amp;MI);
    
  • for (MachineInstr *Terminator : Terminators) {

  •  if (!is_contained(NewTerminators, Terminator))
    
  •    Indexes-&amp;gt;removeMachineInstrFromMaps(*Terminator);
    
  • }

  • }

  • updateTerminator(PrevFallthrough, Indexes);

// Insert unconditional &quot;jump Succ&quot; instruction in NMBB if necessary.
NMBB-&gt;addSuccessor(Succ);
if (!NMBB-&gt;isLayoutSuccessor(Succ)) {
SmallVector&lt;MachineOperand, 4&gt; Cond;
const TargetInstrInfo *TII = getParent()-&gt;getSubtarget().getInstrInfo();

  • TII-&gt;insertBranch(*NMBB, Succ, nullptr, Cond, DL);
  • if (Indexes) {
  •  for (MachineInstr &amp;amp;MI : NMBB-&amp;gt;instrs()) {
    
  •    // Some instructions may have been moved to NMBB by updateTerminator(),
    
  •    // so we first remove any instruction that already has an index.
    
  •    if (Indexes-&amp;gt;hasIndex(MI))
    
  •      Indexes-&amp;gt;removeMachineInstrFromMaps(MI);
    
  •    Indexes-&amp;gt;insertMachineInstrInMaps(MI);
    
  •  }
    
  • }
  • TII-&gt;insertBranch(*NMBB, Succ, nullptr, Cond, DL, nullptr, Indexes);
    }

// Fix PHI nodes in Succ so they refer to NMBB instead of this.
diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
index a41ac0e44a7700b..012f56f306f0527 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -10,8 +10,8 @@
//
//===----------------------------------------------------------------------===//

-#include &quot;AArch64ExpandImm.h&quot;
#include &quot;AArch64InstrInfo.h&quot;
+#include &quot;AArch64ExpandImm.h&quot;
#include &quot;AArch64FrameLowering.h&quot;
#include &quot;AArch64MachineFunctionInfo.h&quot;
#include &quot;AArch64Subtarget.h&quot;
@@ -31,6 +31,7 @@
#include &quot;llvm/CodeGen/MachineOperand.h&quot;
#include &quot;llvm/CodeGen/MachineRegisterInfo.h&quot;
#include &quot;llvm/CodeGen/RegisterScavenging.h&quot;
+#include &quot;llvm/CodeGen/SlotIndexes.h&quot;
#include &quot;llvm/CodeGen/StackMaps.h&quot;
#include &quot;llvm/CodeGen/TargetRegisterInfo.h&quot;
#include &quot;llvm/CodeGen/TargetSubtargetInfo.h&quot;
@@ -534,7 +535,8 @@ bool AArch64InstrInfo::reverseBranchCondition(
}

unsigned AArch64InstrInfo::removeBranch(MachineBasicBlock &amp;MBB,

  •                                    int *BytesRemoved) const {
    
  •                                    int *BytesRemoved,
    
  •                                    SlotIndexes *Indexes) const {
    

    MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
    if (I == MBB.end())
    return 0;
    @@ -544,6 +546,8 @@ unsigned AArch64InstrInfo::removeBranch(MachineBasicBlock &amp;MBB,
    return 0;

    // Remove the branch.

  • if (Indexes)

  • Indexes-&gt;removeMachineInstrFromMaps(*I);
    I-&gt;eraseFromParent();

I = MBB.end();
@@ -561,6 +565,8 @@ unsigned AArch64InstrInfo::removeBranch(MachineBasicBlock &amp;MBB,
}

// Remove the branch.

  • if (Indexes)
  • Indexes-&gt;removeMachineInstrFromMaps(*I);
    I-&gt;eraseFromParent();
    if (BytesRemoved)
    *BytesRemoved = 8;
    @@ -568,12 +574,18 @@ unsigned AArch64InstrInfo::removeBranch(MachineBasicBlock &amp;MBB,
    return 2;
    }

-void AArch64InstrInfo::instantiateCondBranch(

  • MachineBasicBlock &amp;MBB, const DebugLoc &amp;DL, MachineBasicBlock *TBB,
  • ArrayRef&lt;MachineOperand&gt; Cond) const {
    +void AArch64InstrInfo::instantiateCondBranch(MachineBasicBlock &amp;MBB,
  •                                         const DebugLoc &amp;amp;DL,
    
  •                                         MachineBasicBlock *TBB,
    
  •                                         ArrayRef&amp;lt;MachineOperand&amp;gt; Cond,
    
  •                                         SlotIndexes *Indexes) const {
    
    if (Cond[0].getImm() != -1) {
    // Regular Bcc
  • BuildMI(&amp;MBB, DL, get(AArch64::Bcc)).addImm(Cond[0].getImm()).addMBB(TBB);
  • MachineInstr *MI = BuildMI(&amp;MBB, DL, get(AArch64::Bcc))
  •                       .addImm(Cond[0].getImm())
    
  •                       .addMBB(TBB);
    
  • if (Indexes)
  •  Indexes-&amp;gt;insertMachineInstrInMaps(*MI);
    
    } else {
    // Folded compare-and-branch
    // Note that we use addOperand instead of addReg to keep the flags.
    @@ -582,20 +594,27 @@ void AArch64InstrInfo::instantiateCondBranch(
    if (Cond.size() &gt; 3)
    MIB.addImm(Cond[3].getImm());
    MIB.addMBB(TBB);
  • if (Indexes)
  •  Indexes-&amp;gt;insertMachineInstrInMaps(*MIB);
    
    }
    }

-unsigned AArch64InstrInfo::insertBranch(

  • MachineBasicBlock &amp;MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
  • ArrayRef&lt;MachineOperand&gt; Cond, const DebugLoc &amp;DL, int *BytesAdded) const {
    +unsigned AArch64InstrInfo::insertBranch(MachineBasicBlock &amp;MBB,
  •                                    MachineBasicBlock *TBB,
    
  •                                    MachineBasicBlock *FBB,
    
  •                                    ArrayRef&amp;lt;MachineOperand&amp;gt; Cond,
    
  •                                    const DebugLoc &amp;amp;DL, int *BytesAdded,
    
  •                                    SlotIndexes *Indexes) const {
    

    // Shouldn&#x27;t be a fall through.
    assert(TBB &amp;&amp; &quot;insertBranch must not be told to insert a fallthrough&quot;);

    if (!FBB) {

  • if (Cond.empty()) // Unconditional branch?
  •  BuildMI(&amp;amp;MBB, DL, get(AArch64::B)).addMBB(TBB);
    
  • else
  •  instantiateCondBranch(MBB, DL, TBB, Cond);
    
  • if (Cond.empty()) { // Unconditional branch?

  •  MachineInstr *MI = BuildMI(&amp;amp;MBB, DL, get(AArch64::B)).addMBB(TBB);
    
  •  if (Indexes)
    
  •    Indexes-&amp;gt;insertMachineInstrInMaps(*MI);
    
  • } else

  •  instantiateCondBranch(MBB, DL, TBB, Cond, Indexes);
    

    if (BytesAdded)
    *BytesAdded = 4;
    @@ -604,8 +623,11 @@ unsigned AArch64InstrInfo::insertBranch(
    }

    // Two-way conditional branch.

  • instantiateCondBranch(MBB, DL, TBB, Cond);
  • BuildMI(&amp;MBB, DL, get(AArch64::B)).addMBB(FBB);
  • instantiateCondBranch(MBB, DL, TBB, Cond, Indexes);
  • MachineInstr *MI = BuildMI(&amp;MBB, DL, get(AArch64::B)).addMBB(FBB);
  • if (Indexes)
  • Indexes-&gt;insertMachineInstrInMaps(*MI);

if (BytesAdded)
*BytesAdded = 8;
diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.h b/llvm/lib/Target/AArch64/AArch64InstrInfo.h
index 24ff676218cbe90..cd6fcd32ee8ac11 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.h
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.h
@@ -225,12 +225,12 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
bool analyzeBranchPredicate(MachineBasicBlock &amp;MBB,
MachineBranchPredicate &amp;MBP,
bool AllowModify) const override;

  • unsigned removeBranch(MachineBasicBlock &amp;MBB,
  •                    int *BytesRemoved = nullptr) const override;
    
  • unsigned removeBranch(MachineBasicBlock &amp;MBB, int *BytesRemoved = nullptr,
  •                    SlotIndexes *Indexes = nullptr) const override;
    
    unsigned insertBranch(MachineBasicBlock &amp;MBB, MachineBasicBlock *TBB,
    MachineBasicBlock *FBB, ArrayRef&lt;MachineOperand&gt; Cond,
  •                    const DebugLoc &amp;amp;DL,
    
  •                    int *BytesAdded = nullptr) const override;
    
  •                    const DebugLoc &amp;amp;DL, int *BytesAdded = nullptr,
    
  •                    SlotIndexes *Indexes = nullptr) const override;
    

    bool
    reverseBranchCondition(SmallVectorImpl&lt;MachineOperand&gt; &amp;Cond) const override;
    bool canInsertSelect(const MachineBasicBlock &amp;, ArrayRef&lt;MachineOperand&gt; Cond,
    @@ -378,7 +378,8 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {

    void instantiateCondBranch(MachineBasicBlock &amp;MBB, const DebugLoc &amp;DL,
    MachineBasicBlock *TBB,

  •                         ArrayRef&amp;lt;MachineOperand&amp;gt; Cond) const;
    
  •                         ArrayRef&amp;lt;MachineOperand&amp;gt; Cond,
    
  •                         SlotIndexes *Indexes) const;
    
    bool substituteCmpToZero(MachineInstr &amp;CmpInstr, unsigned SrcReg,
    const MachineRegisterInfo &amp;MRI) const;
    bool removeCmpToZeroOrOne(MachineInstr &amp;CmpInstr, unsigned SrcReg,
    diff --git a/llvm/lib/Target/AMDGPU/R600InstrInfo.cpp b/llvm/lib/Target/AMDGPU/R600InstrInfo.cpp
    index 7f874b245b8f4f4..7d495553e945a7a 100644
    --- a/llvm/lib/Target/AMDGPU/R600InstrInfo.cpp
    +++ b/llvm/lib/Target/AMDGPU/R600InstrInfo.cpp
    @@ -19,6 +19,7 @@
    #include &quot;R600Subtarget.h&quot;
    #include &quot;llvm/ADT/SmallSet.h&quot;
    #include &quot;llvm/CodeGen/MachineFrameInfo.h&quot;
    +#include &quot;llvm/CodeGen/SlotIndexes.h&quot;

using namespace llvm;

@@ -730,14 +731,16 @@ unsigned R600InstrInfo::insertBranch(MachineBasicBlock &amp;MBB,
MachineBasicBlock *TBB,
MachineBasicBlock *FBB,
ArrayRef&lt;MachineOperand&gt; Cond,
-...

@perlfu
Copy link
Contributor Author

perlfu commented Sep 13, 2023

This touches all backends, but the code changes are very fairly straightforward and similar between most.
There is some addition churn from clang-format on code that has not been touched in a long time.

@@ -693,15 +693,15 @@ void MachineBasicBlock::updateTerminator(
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
SmallVector<MachineOperand, 4> Cond;
DebugLoc DL = findBranchDebugLoc();
bool B = TII->analyzeBranch(*this, TBB, FBB, Cond);
bool B = TII->analyzeBranch(*this, TBB, FBB, Cond, /*AllowModify=*/false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is NFC, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is NFC, but I felt it was appropriate to add an explicit configuration of AllowModify=false here as SlotIndexes will be invalidated if analyzeBranch modifies the instructions.

@jayfoad
Copy link
Contributor

jayfoad commented Sep 13, 2023

LGTM overall. I would suggest that approval from a small handful of target maintainers is enough to merge this.

@qcolombet
Copy link
Collaborator

Could we fix SplitCriticalEdges instead of pushing the burden of the SlotIndex update to all backends?

llvm/lib/CodeGen/MachineBasicBlock.cpp Outdated Show resolved Hide resolved
llvm/lib/CodeGen/MachineBasicBlock.cpp Outdated Show resolved Hide resolved
llvm/include/llvm/CodeGen/TargetInstrInfo.h Outdated Show resolved Hide resolved
@perlfu
Copy link
Contributor Author

perlfu commented Sep 13, 2023

Could we fix SplitCriticalEdges instead of pushing the burden of the SlotIndex update to all backends?

I am open to alternatives.
From my perspective it is this, or add some kind of listener pattern to MachineBasicBlock so SplitCriticalEdges can intercept instructions added or removed by target specific code.

- Reverse order of Indexes and BytesAdded/BytesRemoved
- Describe new parameters in comments
@qcolombet
Copy link
Collaborator

Could we fix SplitCriticalEdges instead of pushing the burden of the SlotIndex update to all backends?

I am open to alternatives. From my perspective it is this, or add some kind of listener pattern to MachineBasicBlock so SplitCriticalEdges can intercept instructions added or removed by target specific code.

The listener alternative sounds better to me.
This is how we handle this kind of things in MIR in general (look for Delegate in MachineFunction, MachineRegisterInfo, etc.)

@perlfu
Copy link
Contributor Author

perlfu commented Oct 11, 2023

Close this in favour of observer pattern in #68786.
(Created a fresh PR to avoid noise for all review groups added by files touched in this PR.)

@perlfu perlfu closed this Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants