[llvm] Move SI Lower Control Flow Up (PR #159557)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 18 10:27:56 PDT 2025


================
@@ -0,0 +1,249 @@
+#pragma once
+
+#include "GCNSubtarget.h"
+#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/CodeGen/MachineBasicBlock.h"
+#include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/Support/ErrorHandling.h"
+
+#include "SIInstrInfo.h"
+
+#include <cassert>
+#include <unordered_set>
+
+using namespace llvm;
+
+using std::unordered_set;
+using std::vector;
+
+static inline MachineInstr &get_branch_with_dest(MachineBasicBlock &branching_MBB,
+                                                 MachineBasicBlock &dest_MBB) {
+  auto& TII = *branching_MBB.getParent()->getSubtarget<GCNSubtarget>().getInstrInfo();
+  for (MachineInstr &branch_MI : reverse(branching_MBB.instrs()))
+    if (branch_MI.isBranch() && TII.getBranchDestBlock(branch_MI) == &dest_MBB)
+      return branch_MI;
+
+  llvm_unreachable("Don't call this if there's no branch to the destination.");
+}
+
+static inline void move_ins_before_phis(MachineInstr &MI) {
+  MachineBasicBlock& MBB = *MI.getParent();
+  MachineFunction& MF = *MBB.getParent();
+  auto &TII = *MF.getSubtarget<GCNSubtarget>().getInstrInfo();
+  auto& MRI = MF.getRegInfo();
+
+  bool phi_seen = false;
+  MachineBasicBlock::iterator first_phi;
+  for (first_phi = MBB.begin(); first_phi != MBB.end(); first_phi++)
+    if (first_phi->getOpcode() == AMDGPU::PHI) {
+      phi_seen = true;
+      break;
+    }
+  
+  if (!phi_seen) {
----------------
alex-t wrote:

Not necessary as SILowerControlFlow always insert at block begin.

` MachineBasicBlock::iterator Start = MBB.begin();`
  `Register SaveReg = MRI->createVirtualRegister(BoolRC);`
  `MachineInstr *OrSaveExec =`
      `BuildMI(MBB, Start, DL, TII->get(LMC.OrSaveExecOpc), SaveReg)`
        `  .add(MI.getOperand(1)); // Saved EXEC`
 
 ` move_ins_before_phis(*OrSaveExec);`


https://github.com/llvm/llvm-project/pull/159557


More information about the llvm-commits mailing list