[llvm] [AMDGPU] Form VOPD3 pairs with pair-local literal moves (PR #223431)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 08:53:25 PDT 2026


================
@@ -22,146 +22,283 @@
 #include "GCNVOPDUtils.h"
 #include "SIInstrInfo.h"
 #include "Utils/AMDGPUBaseInfo.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/CodeGen/LiveRegUnits.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "llvm/CodeGen/MachineOperand.h"
 #include "llvm/CodeGen/MachinePassManager.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/Support/Debug.h"
 
 #define DEBUG_TYPE "gcn-create-vopd"
 STATISTIC(NumVOPDCreated, "Number of VOPD Insts Created.");
+STATISTIC(NumLiteralsMaterialized,
+          "Number of immediates moved into a scalar register to allow VOPD3 "
+          "pairing.");
+STATISTIC(NumCandidateEdgesWithoutFreeSGPR,
+          "Number of VOPD3 candidate edges skipped because no scalar register "
+          "was free for their immediate.");
 
 using namespace llvm;
 
 namespace {
 
-class GCNCreateVOPD {
-private:
-  class VOPDCombineInfo {
-  public:
-    VOPDCombineInfo() = default;
-    VOPDCombineInfo(MachineInstr *First, MachineInstr *Second,
-                    bool VOPD3 = false)
-        : FirstMI(First), SecondMI(Second), IsVOPD3(VOPD3) {}
-
-    MachineInstr *FirstMI;
-    MachineInstr *SecondMI;
-    bool IsVOPD3;
+struct VOPDCandidate {
+  VOPDMatchInfo Match;
+  /// The register for Match.LiteralFixups, or a null register if none is free.
+  Register MaterializationReg;
+
+  bool needsMaterialization() const { return !Match.LiteralFixups.empty(); }
+
+  bool isFeasible() const {
+    return !needsMaterialization() || MaterializationReg;
+  }
+};
+
+} // namespace
+
+/// Add everything the instructions in [\p Begin, \p RangeEnd] touch to
+/// \p Live, which already holds what is live after \p RangeEnd.
+static void addRangeUses(LiveRegUnits &Live, MachineBasicBlock::iterator Begin,
+                         MachineInstr &RangeEnd) {
+  MachineBasicBlock::iterator After =
+      std::next(MachineBasicBlock::iterator(&RangeEnd));
+  for (MachineInstr &MI : make_range(Begin, After)) {
+    if (!MI.isDebugInstr())
+      Live.accumulate(MI);
+  }
+}
+
+/// Return a scalar register which every fixup can read and which no value in
+/// \p Live occupies, or a null register. Low registers are preferred, because
+/// those are most likely in use already, so the function's register count does
+/// not grow.
+static Register takeFreeSGPR(const GCNSubtarget &ST,
+                             const MachineRegisterInfo &MRI,
+                             const LiveRegUnits &Live,
+                             ArrayRef<VOPDLiteralFixup> Fixups) {
+  assert(!Fixups.empty());
+  const SIRegisterInfo *TRI = ST.getRegisterInfo();
+  for (MCPhysReg Reg : AMDGPU::SGPR_32RegClass) {
+    // SGPR_32 also holds the halves of VCC. Writing those changes VCCZ,
+    // which is not modelled by \p Live, so a free half is not safe to use.
+    if (MRI.isReserved(Reg) || !Live.available(Reg))
+      continue;
+    if (!all_of(Fixups, [Reg](const VOPDLiteralFixup &Fixup) {
+          return Fixup.SlotRC->contains(Reg);
+        }))
+      continue;
+    if (TRI->isSubRegisterEq(AMDGPU::VCC, Reg))
+      continue;
----------------
shiltian wrote:

Good point. I think I did make this change before, but for some reason it was "reverted".

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


More information about the llvm-commits mailing list