Skip to content

[TableGen][GISel] Don't use std::optional with pointers (NFC) #120026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

s-barannikov
Copy link
Contributor

Pointers already have a well-defined null value.

@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2024

@llvm/pr-subscribers-llvm-globalisel

@llvm/pr-subscribers-tablegen

Author: Sergei Barannikov (s-barannikov)

Changes

Pointers already have a well-defined null value.


Full diff: https://github.com/llvm/llvm-project/pull/120026.diff

3 Files Affected:

  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.h (+1-1)
  • (modified) llvm/utils/TableGen/GlobalISelEmitter.cpp (+53-56)
diff --git a/llvm/utils/TableGen/Common/CodeGenTarget.cpp b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
index f01b8a962bfbb1..ffb77d0b6ea27c 100644
--- a/llvm/utils/TableGen/Common/CodeGenTarget.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
@@ -163,7 +163,7 @@ CodeGenRegBank &CodeGenTarget::getRegBank() const {
   return *RegBank;
 }
 
-std::optional<CodeGenRegisterClass *> CodeGenTarget::getSuperRegForSubReg(
+const CodeGenRegisterClass * CodeGenTarget::getSuperRegForSubReg(
     const ValueTypeByHwMode &ValueTy, CodeGenRegBank &RegBank,
     const CodeGenSubRegIndex *SubIdx, bool MustBeAllocatable) const {
   std::vector<CodeGenRegisterClass *> Candidates;
@@ -192,7 +192,7 @@ std::optional<CodeGenRegisterClass *> CodeGenTarget::getSuperRegForSubReg(
 
   // If we didn't find anything, we're done.
   if (Candidates.empty())
-    return std::nullopt;
+    return nullptr;
 
   // Find and return the largest of our candidate classes.
   llvm::stable_sort(Candidates, [&](const CodeGenRegisterClass *A,
diff --git a/llvm/utils/TableGen/Common/CodeGenTarget.h b/llvm/utils/TableGen/Common/CodeGenTarget.h
index c3c7f7e362dbff..682cc4e2bc9c65 100644
--- a/llvm/utils/TableGen/Common/CodeGenTarget.h
+++ b/llvm/utils/TableGen/Common/CodeGenTarget.h
@@ -124,7 +124,7 @@ class CodeGenTarget {
 
   /// Return the largest register class on \p RegBank which supports \p Ty and
   /// covers \p SubIdx if it exists.
-  std::optional<CodeGenRegisterClass *>
+  const CodeGenRegisterClass *
   getSuperRegForSubReg(const ValueTypeByHwMode &Ty, CodeGenRegBank &RegBank,
                        const CodeGenSubRegIndex *SubIdx,
                        bool MustBeAllocatable = false) const;
diff --git a/llvm/utils/TableGen/GlobalISelEmitter.cpp b/llvm/utils/TableGen/GlobalISelEmitter.cpp
index 2259253ab48a00..c34155ce36e022 100644
--- a/llvm/utils/TableGen/GlobalISelEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelEmitter.cpp
@@ -438,26 +438,25 @@ class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
   /// CodeGenRegisterClass will support the CodeGenRegisterClass of
   /// \p SubRegNode, and the subregister index defined by \p SubRegIdxNode.
   /// If no register class is found, return std::nullopt.
-  std::optional<const CodeGenRegisterClass *>
+  const CodeGenRegisterClass *
   inferSuperRegisterClassForNode(const TypeSetByHwMode &Ty,
                                  const TreePatternNode &SuperRegNode,
                                  const TreePatternNode &SubRegIdxNode);
-  std::optional<CodeGenSubRegIndex *>
+  const CodeGenSubRegIndex *
   inferSubRegIndexForNode(const TreePatternNode &SubRegIdxNode);
 
   /// Infer a CodeGenRegisterClass which suppoorts \p Ty and \p SubRegIdxNode.
   /// Return std::nullopt if no such class exists.
-  std::optional<const CodeGenRegisterClass *>
+  const CodeGenRegisterClass *
   inferSuperRegisterClass(const TypeSetByHwMode &Ty,
                           const TreePatternNode &SubRegIdxNode);
 
   /// Return the CodeGenRegisterClass associated with \p Leaf if it has one.
-  std::optional<const CodeGenRegisterClass *>
-  getRegClassFromLeaf(const TreePatternNode &Leaf);
+  const CodeGenRegisterClass *getRegClassFromLeaf(const TreePatternNode &Leaf);
 
   /// Return a CodeGenRegisterClass for \p N if one can be found. Return
   /// std::nullopt otherwise.
-  std::optional<const CodeGenRegisterClass *>
+  const CodeGenRegisterClass *
   inferRegClassFromPattern(const TreePatternNode &N);
 
   Error constrainOperands(action_iterator InsertPt, RuleMatcher &M,
@@ -1733,12 +1732,13 @@ Error GlobalISelEmitter::constrainOperands(action_iterator InsertPt,
     M.insertAction<ConstrainOperandToRegClassAction>(
         InsertPt, InsnID, 0, Target.getRegisterClass(DstIOpRec));
   } else if (DstIName == "EXTRACT_SUBREG") {
-    auto SuperClass = inferRegClassFromPattern(Dst.getChild(0));
+    const CodeGenRegisterClass *SuperClass =
+        inferRegClassFromPattern(Dst.getChild(0));
     if (!SuperClass)
       return failedImport(
           "Cannot infer register class from EXTRACT_SUBREG operand #0");
 
-    auto SubIdx = inferSubRegIndexForNode(Dst.getChild(1));
+    const CodeGenSubRegIndex *SubIdx = inferSubRegIndexForNode(Dst.getChild(1));
     if (!SubIdx)
       return failedImport("EXTRACT_SUBREG child #1 is not a subreg index");
 
@@ -1749,7 +1749,7 @@ Error GlobalISelEmitter::constrainOperands(action_iterator InsertPt,
     // FIXME: This may introduce an extra copy if the chosen class doesn't
     //        actually contain the subregisters.
     const auto SrcRCDstRCPair =
-        (*SuperClass)->getMatchingSubClassWithSubRegs(CGRegs, *SubIdx);
+        SuperClass->getMatchingSubClassWithSubRegs(CGRegs, SubIdx);
     if (!SrcRCDstRCPair) {
       return failedImport("subreg index is incompatible "
                           "with inferred reg class");
@@ -1763,56 +1763,59 @@ Error GlobalISelEmitter::constrainOperands(action_iterator InsertPt,
   } else if (DstIName == "INSERT_SUBREG") {
     // We need to constrain the destination, a super regsister source, and a
     // subregister source.
-    auto SubClass = inferRegClassFromPattern(Dst.getChild(1));
+    const CodeGenRegisterClass *SubClass =
+        inferRegClassFromPattern(Dst.getChild(1));
     if (!SubClass)
       return failedImport(
           "Cannot infer register class from INSERT_SUBREG operand #1");
-    auto SuperClass = inferSuperRegisterClassForNode(
+    const CodeGenRegisterClass *SuperClass = inferSuperRegisterClassForNode(
         Dst.getExtType(0), Dst.getChild(0), Dst.getChild(2));
     if (!SuperClass)
       return failedImport(
           "Cannot infer register class for INSERT_SUBREG operand #0");
     M.insertAction<ConstrainOperandToRegClassAction>(InsertPt, InsnID, 0,
-                                                     **SuperClass);
+                                                     *SuperClass);
     M.insertAction<ConstrainOperandToRegClassAction>(InsertPt, InsnID, 1,
-                                                     **SuperClass);
+                                                     *SuperClass);
     M.insertAction<ConstrainOperandToRegClassAction>(InsertPt, InsnID, 2,
-                                                     **SubClass);
+                                                     *SubClass);
   } else if (DstIName == "SUBREG_TO_REG") {
     // We need to constrain the destination and subregister source.
     // Attempt to infer the subregister source from the first child. If it has
     // an explicitly given register class, we'll use that. Otherwise, we will
     // fail.
-    auto SubClass = inferRegClassFromPattern(Dst.getChild(1));
+    const CodeGenRegisterClass *SubClass =
+        inferRegClassFromPattern(Dst.getChild(1));
     if (!SubClass)
       return failedImport(
           "Cannot infer register class from SUBREG_TO_REG child #1");
     // We don't have a child to look at that might have a super register node.
-    auto SuperClass =
+    const CodeGenRegisterClass *SuperClass =
         inferSuperRegisterClass(Dst.getExtType(0), Dst.getChild(2));
     if (!SuperClass)
       return failedImport(
           "Cannot infer register class for SUBREG_TO_REG operand #0");
     M.insertAction<ConstrainOperandToRegClassAction>(InsertPt, InsnID, 0,
-                                                     **SuperClass);
+                                                     *SuperClass);
     M.insertAction<ConstrainOperandToRegClassAction>(InsertPt, InsnID, 2,
-                                                     **SubClass);
+                                                     *SubClass);
   } else if (DstIName == "REG_SEQUENCE") {
-    auto SuperClass = inferRegClassFromPattern(Dst.getChild(0));
+    const CodeGenRegisterClass *SuperClass =
+        inferRegClassFromPattern(Dst.getChild(0));
 
     M.insertAction<ConstrainOperandToRegClassAction>(InsertPt, InsnID, 0,
-                                                     **SuperClass);
+                                                     *SuperClass);
 
     unsigned Num = Dst.getNumChildren();
     for (unsigned I = 1; I != Num; I += 2) {
       const TreePatternNode &SubRegChild = Dst.getChild(I + 1);
 
-      auto SubIdx = inferSubRegIndexForNode(SubRegChild);
+      const CodeGenSubRegIndex *SubIdx = inferSubRegIndexForNode(SubRegChild);
       if (!SubIdx)
         return failedImport("REG_SEQUENCE child is not a subreg index");
 
       const auto SrcRCDstRCPair =
-          (*SuperClass)->getMatchingSubClassWithSubRegs(CGRegs, *SubIdx);
+          SuperClass->getMatchingSubClassWithSubRegs(CGRegs, SubIdx);
 
       M.insertAction<ConstrainOperandToRegClassAction>(InsertPt, InsnID, I,
                                                        *SrcRCDstRCPair->second);
@@ -1824,19 +1827,16 @@ Error GlobalISelEmitter::constrainOperands(action_iterator InsertPt,
   return Error::success();
 }
 
-std::optional<const CodeGenRegisterClass *>
+const CodeGenRegisterClass *
 GlobalISelEmitter::getRegClassFromLeaf(const TreePatternNode &Leaf) {
   assert(Leaf.isLeaf() && "Expected leaf?");
   const Record *RCRec = getInitValueAsRegClass(Leaf.getLeafValue());
   if (!RCRec)
-    return std::nullopt;
-  const CodeGenRegisterClass *RC = CGRegs.getRegClass(RCRec);
-  if (!RC)
-    return std::nullopt;
-  return RC;
+    return nullptr;
+  return CGRegs.getRegClass(RCRec);
 }
 
-std::optional<const CodeGenRegisterClass *>
+const CodeGenRegisterClass *
 GlobalISelEmitter::inferRegClassFromPattern(const TreePatternNode &N) {
   if (N.isLeaf())
     return getRegClassFromLeaf(N);
@@ -1847,12 +1847,12 @@ GlobalISelEmitter::inferRegClassFromPattern(const TreePatternNode &N) {
   // Only handle things that produce at least one value (if multiple values,
   // just take the first one).
   if (N.getNumTypes() < 1)
-    return std::nullopt;
+    return nullptr;
   const Record *OpRec = N.getOperator();
 
   // We only want instructions.
   if (!OpRec->isSubClassOf("Instruction"))
-    return std::nullopt;
+    return nullptr;
 
   // Don't want to try and infer things when there could potentially be more
   // than one candidate register class.
@@ -1867,7 +1867,7 @@ GlobalISelEmitter::inferRegClassFromPattern(const TreePatternNode &N) {
     // has the desired register class as the first child.
     const TreePatternNode &RCChild = N.getChild(IsRegSequence ? 0 : 1);
     if (!RCChild.isLeaf())
-      return std::nullopt;
+      return nullptr;
     return getRegClassFromLeaf(RCChild);
   }
   if (InstName == "INSERT_SUBREG") {
@@ -1897,34 +1897,29 @@ GlobalISelEmitter::inferRegClassFromPattern(const TreePatternNode &N) {
     return &RC;
   }
 
-  return std::nullopt;
+  return nullptr;
 }
 
-std::optional<const CodeGenRegisterClass *>
+const CodeGenRegisterClass *
 GlobalISelEmitter::inferSuperRegisterClass(
     const TypeSetByHwMode &Ty, const TreePatternNode &SubRegIdxNode) {
   // We need a ValueTypeByHwMode for getSuperRegForSubReg.
   if (!Ty.isValueTypeByHwMode(false))
-    return std::nullopt;
+    return nullptr;
   if (!SubRegIdxNode.isLeaf())
-    return std::nullopt;
+    return nullptr;
   const DefInit *SubRegInit = dyn_cast<DefInit>(SubRegIdxNode.getLeafValue());
   if (!SubRegInit)
-    return std::nullopt;
+    return nullptr;
   const CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef());
 
   // Use the information we found above to find a minimal register class which
   // supports the subregister and type we want.
-  auto RC =
-      Target.getSuperRegForSubReg(Ty.getValueTypeByHwMode(), CGRegs, SubIdx,
-                                  /* MustBeAllocatable */ true);
-  if (!RC)
-    return std::nullopt;
-  return *RC;
+  return Target.getSuperRegForSubReg(Ty.getValueTypeByHwMode(), CGRegs, SubIdx,
+                                     /*MustBeAllocatable=*/true);
 }
 
-std::optional<const CodeGenRegisterClass *>
-GlobalISelEmitter::inferSuperRegisterClassForNode(
+const CodeGenRegisterClass *GlobalISelEmitter::inferSuperRegisterClassForNode(
     const TypeSetByHwMode &Ty, const TreePatternNode &SuperRegNode,
     const TreePatternNode &SubRegIdxNode) {
   // Check if we already have a defined register class for the super register
@@ -1932,20 +1927,20 @@ GlobalISelEmitter::inferSuperRegisterClassForNode(
   // from the subregister index node. We can assume that whoever wrote the
   // pattern in the first place made sure that the super register and
   // subregister are compatible.
-  if (std::optional<const CodeGenRegisterClass *> SuperRegisterClass =
+  if (const CodeGenRegisterClass *SuperRegisterClass =
           inferRegClassFromPattern(SuperRegNode))
-    return *SuperRegisterClass;
+    return SuperRegisterClass;
   return inferSuperRegisterClass(Ty, SubRegIdxNode);
 }
 
-std::optional<CodeGenSubRegIndex *> GlobalISelEmitter::inferSubRegIndexForNode(
+const CodeGenSubRegIndex * GlobalISelEmitter::inferSubRegIndexForNode(
     const TreePatternNode &SubRegIdxNode) {
   if (!SubRegIdxNode.isLeaf())
-    return std::nullopt;
+    return nullptr;
 
   const DefInit *SubRegInit = dyn_cast<DefInit>(SubRegIdxNode.getLeafValue());
   if (!SubRegInit)
-    return std::nullopt;
+    return nullptr;
   return CGRegs.getSubRegIdx(SubRegInit->getDef());
 }
 
@@ -2095,16 +2090,17 @@ Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
       if (MatchedRC.isNull())
         return failedImport("REG_SEQUENCE operand #0 isn't a register class");
     } else if (DstIName == "EXTRACT_SUBREG") {
-      auto InferredClass = inferRegClassFromPattern(Dst.getChild(0));
+      const CodeGenRegisterClass *InferredClass =
+          inferRegClassFromPattern(Dst.getChild(0));
       if (!InferredClass)
         return failedImport(
             "Could not infer class for EXTRACT_SUBREG operand #0");
 
       // We can assume that a subregister is in the same bank as it's super
       // register.
-      MatchedRC = (*InferredClass)->getDef();
+      MatchedRC = InferredClass->getDef();
     } else if (DstIName == "INSERT_SUBREG") {
-      auto MaybeSuperClass =
+      const CodeGenRegisterClass *MaybeSuperClass =
           inferSuperRegisterClassForNode(VTy, Dst.getChild(0), Dst.getChild(2));
       if (!MaybeSuperClass)
         return failedImport(
@@ -2112,13 +2108,14 @@ Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
       // Move to the next pattern here, because the register class we found
       // doesn't necessarily have a record associated with it. So, we can't
       // set DstIOpRec using this.
-      MatchedRC = *MaybeSuperClass;
+      MatchedRC = MaybeSuperClass;
     } else if (DstIName == "SUBREG_TO_REG") {
-      auto MaybeRegClass = inferSuperRegisterClass(VTy, Dst.getChild(2));
+      const CodeGenRegisterClass *MaybeRegClass =
+          inferSuperRegisterClass(VTy, Dst.getChild(2));
       if (!MaybeRegClass)
         return failedImport(
             "Cannot infer register class for SUBREG_TO_REG operand #0");
-      MatchedRC = *MaybeRegClass;
+      MatchedRC = MaybeRegClass;
     } else if (cast<const Record *>(MatchedRC)->isSubClassOf("RegisterOperand"))
       MatchedRC = cast<const Record *>(MatchedRC)->getValueAsDef("RegClass");
     else if (!cast<const Record *>(MatchedRC)->isSubClassOf("RegisterClass"))

Copy link

github-actions bot commented Dec 16, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@s-barannikov s-barannikov force-pushed the tablegen/gisel-optional-nullptr branch from f165172 to 3c7bb6d Compare December 16, 2024 00:26
Pointers already have a well-defined null value.
@s-barannikov s-barannikov force-pushed the tablegen/gisel-optional-nullptr branch from 3c7bb6d to ae600f9 Compare December 16, 2024 00:46
@s-barannikov s-barannikov merged commit 73eecb7 into llvm:main Dec 16, 2024
8 checks passed
@s-barannikov s-barannikov deleted the tablegen/gisel-optional-nullptr branch December 16, 2024 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants