[llvm] AMDGPU: Add NextUseAnalysis Pass (PR #178873)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 16 10:24:56 PDT 2026


================
@@ -0,0 +1,378 @@
+//===---------------------- AMDGPUNextUseAnalysis.h  ----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements Next Use Analysis.
+//
+// For each register it goes over all uses and returns the estimated distance of
+// the nearest use. This will be used for selecting which registers to spill
+// before register allocation.
+//
+// This is based on ideas from the paper:
+// "Register Spilling and Live-Range Splitting for SSA-Form Programs"
+// Matthias Braun and Sebastian Hack, CC'09
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUNEXTUSEANALYSIS_H
+#define LLVM_LIB_TARGET_AMDGPU_AMDGPUNEXTUSEANALYSIS_H
+
+#include "SIInstrInfo.h"
+#include "SIRegisterInfo.h"
+#include "llvm/CodeGen/LiveIntervals.h"
+#include "llvm/CodeGen/LiveVariables.h"
+#include "llvm/CodeGen/MachineLoopInfo.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/CodeGen/TargetRegisterInfo.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/Support/Format.h"
+#include "llvm/Support/JSON.h"
+#include <limits>
+#include <optional>
+
+namespace llvm {
+
+class AMDGPUNextUseAnalysisImpl;
+
+//==============================================================================
+// NextUseDistance - Represents a distance in the next-use analysis. Currently
+// wraps a 64-bit int with special encoding for loop depth and unreachable
+// distances.
+//==============================================================================
+class NextUseDistance {
+public:
+  constexpr static NextUseDistance unreachable() {
+    return NextUseDistance(std::numeric_limits<int64_t>::max());
+  }
+
+  constexpr static NextUseDistance fromSize(unsigned Size, unsigned Depth) {
+    return NextUseDistance(Size).applyLoopWeight(Depth);
+  }
+
+  constexpr NextUseDistance(unsigned V) : Value(V) {}
+  constexpr NextUseDistance(int V) : Value(V) {}
+  constexpr NextUseDistance(const NextUseDistance &B) : Value(B.Value) {}
+
+  constexpr bool isUnreachable() const { return *this == unreachable(); }
+  constexpr bool isReachable() const { return !isUnreachable(); }
+
+  //----------------------------------------------------------------------------
+  // Assignment
+  //----------------------------------------------------------------------------
+  constexpr NextUseDistance &operator=(const NextUseDistance &B) {
+    Value = B.Value;
+    return *this;
+  }
+
+  constexpr NextUseDistance &operator=(unsigned V) {
+    Value = V;
+    return *this;
+  }
+
+  constexpr NextUseDistance &operator=(int V) {
+    Value = V;
+    return *this;
+  }
+
+  //----------------------------------------------------------------------------
+  // Arithmetic operators
+  //----------------------------------------------------------------------------
+  constexpr NextUseDistance &operator+=(const NextUseDistance &B) {
+    Value += B.Value;
+    return *this;
+  }
+
+  constexpr NextUseDistance &operator-=(const NextUseDistance &B) {
+    Value -= B.Value;
+    return *this;
+  }
+
+  constexpr NextUseDistance operator-() const {
+    return NextUseDistance(-Value);
+  }
+
+  constexpr NextUseDistance applyLoopWeight() const {
----------------
arsenm wrote:

This smells like reinventing BlockFrequencies? 

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


More information about the llvm-commits mailing list