[llvm] [DRAFT][NFC] Make RegState enum class (PR #172441)

Sam Elliott via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 16 09:09:30 PST 2025


================
@@ -138,39 +141,72 @@ class MachineInstrBuilder {
   Register getReg(unsigned Idx) const { return MI->getOperand(Idx).getReg(); }
 
   /// Add a new virtual register operand.
-  const MachineInstrBuilder &addReg(Register RegNo, unsigned flags = 0,
+  const MachineInstrBuilder &addReg(Register RegNo, RegState Flags = RegState::NoFlags,
                                     unsigned SubReg = 0) const {
-    assert((flags & 0x1) == 0 &&
+    assert((Flags & static_cast<RegState>(0x1)) == RegState::NoFlags &&
            "Passing in 'true' to addReg is forbidden! Use enums instead.");
+
+    auto HasFlag = [Flags](RegState Flag) -> bool {
+      return (Flags & Flag) != RegState::NoFlags;
+    };
+
     MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
-                                               flags & RegState::Define,
-                                               flags & RegState::Implicit,
-                                               flags & RegState::Kill,
-                                               flags & RegState::Dead,
-                                               flags & RegState::Undef,
-                                               flags & RegState::EarlyClobber,
-                                               SubReg,
-                                               flags & RegState::Debug,
-                                               flags & RegState::InternalRead,
-                                               flags & RegState::Renamable));
-    return *this;
+                                                  HasFlag(RegState::Define),
+                                                  HasFlag(RegState::Implicit),
+                                                  HasFlag(RegState::Kill),
+                                                  HasFlag(RegState::Dead),
+                                                  HasFlag(RegState::Undef),
+                                                  HasFlag(RegState::EarlyClobber),
+                                                  SubReg,
+                                                  HasFlag(RegState::Debug),
+                                                  HasFlag(RegState::InternalRead),
+                                                  HasFlag(RegState::Renamable)));
+    return *this;
+  }
+
+  /// Add a new virtual register operand with explicit subregister
+  ///
+  /// This works like `addReg`, but the subregister index comes before the flags so
+  /// the flags can be omitted.
+  const MachineInstrBuilder &addSubReg(Register RegNo, unsigned SubReg, RegState Flags = RegState::NoFlags) const {
+    return addReg(RegNo, Flags, SubReg);
   }
 
   /// Add a virtual register definition operand.
-  const MachineInstrBuilder &addDef(Register RegNo, unsigned Flags = 0,
+  const MachineInstrBuilder &addDef(Register RegNo, RegState Flags = RegState::NoFlags,
                                     unsigned SubReg = 0) const {
     return addReg(RegNo, Flags | RegState::Define, SubReg);
   }
 
+  /// Add a virtual register definition with explicit subregister
+
+  ///
+  /// This is like `addDef`, but the subregister index comes before the flags so the
+  /// flags can be omitted.
+  const MachineInstrBuilder &addSubDef(Register RegNo, unsigned SubReg, RegState Flags = RegState::NoFlags) const {
----------------
lenary wrote:

We can push towards that in a follow-up change? I don't think there's a good answer here - `addReg` has two optional arguments, and lots of cases only need one of them, but I'm not sure 

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


More information about the llvm-commits mailing list