[llvm] [NFC][CodeGen] Add helper function to check SubReg validity (PR #181489)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 17 02:55:22 PST 2026


================
@@ -442,6 +442,37 @@ const TargetRegisterClass *TargetRegisterInfo::findCommonRegClass(
   return getCommonSubClass(DefRC, SrcRC);
 }
 
+bool TargetRegisterInfo::isSubRegValidForRegClass(const TargetRegisterClass *RC,
+                                                  unsigned Idx) const {
+  // NoSubRegister is always valid.
+  if (!Idx)
+    return true;
+
+  // `Idx` is valid if the largest subclass of `RC` that supports
+  // sub-register index `Idx` is same as `RC`. That is, every physical
+  // register in `RC` support sub-register index `Idx`.
+  return getSubClassWithSubReg(RC, Idx) == RC;
+}
+
+bool TargetRegisterInfo::isSubRegValidForRegClass(
+    const TargetRegisterClass *RC, const TargetRegisterClass *DRC,
+    unsigned Idx) const {
+  // If no sub-register index is used, return true if `DRC` is a super-class
----------------
jayfoad wrote:

Can this whole function be implemented as:
```
  return getSubRegisterClass(RC, Idx)->hasSuperClassEq(DRC);
```
Maybe that is the point of your [questions on discourse](https://discourse.llvm.org/t/q-about-getsubclasswithsubreg-and-getsubregisterclass/89830)?

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


More information about the llvm-commits mailing list