[llvm] [TableGen] Use const getter to implement non-const getter instead of the other way around. NFC (PR #123452)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 17 22:42:03 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-tablegen

Author: Craig Topper (topperc)

<details>
<summary>Changes</summary>

It's better to cast away constness on the reference being returned than to cast away constness on the this pointer.

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


1 Files Affected:

- (modified) llvm/utils/TableGen/Common/CodeGenSchedule.h (+9-8) 


``````````diff
diff --git a/llvm/utils/TableGen/Common/CodeGenSchedule.h b/llvm/utils/TableGen/Common/CodeGenSchedule.h
index d47c03514b155f..3fa7ca1a48cf0d 100644
--- a/llvm/utils/TableGen/Common/CodeGenSchedule.h
+++ b/llvm/utils/TableGen/Common/CodeGenSchedule.h
@@ -495,13 +495,14 @@ class CodeGenSchedModels {
     return ProcModels[I->second];
   }
 
-  CodeGenProcModel &getProcModel(const Record *ModelDef) {
+  const CodeGenProcModel &getProcModel(const Record *ModelDef) const {
     ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
     assert(I != ProcModelMap.end() && "missing machine model");
     return ProcModels[I->second];
   }
-  const CodeGenProcModel &getProcModel(const Record *ModelDef) const {
-    return const_cast<CodeGenSchedModels *>(this)->getProcModel(ModelDef);
+  CodeGenProcModel &getProcModel(const Record *ModelDef) {
+    return const_cast<CodeGenProcModel &>(
+        static_cast<const CodeGenSchedModels &>(*this).getProcModel(ModelDef));
   }
 
   // Iterate over the unique processor models.
@@ -529,14 +530,14 @@ class CodeGenSchedModels {
   const CodeGenSchedRW &getSchedRW(unsigned Idx, bool IsRead) const {
     return IsRead ? getSchedRead(Idx) : getSchedWrite(Idx);
   }
-  CodeGenSchedRW &getSchedRW(const Record *Def) {
+  const CodeGenSchedRW &getSchedRW(const Record *Def) const {
     bool IsRead = Def->isSubClassOf("SchedRead");
     unsigned Idx = getSchedRWIdx(Def, IsRead);
-    return const_cast<CodeGenSchedRW &>(IsRead ? getSchedRead(Idx)
-                                               : getSchedWrite(Idx));
+    return IsRead ? getSchedRead(Idx) : getSchedWrite(Idx);
   }
-  const CodeGenSchedRW &getSchedRW(const Record *Def) const {
-    return const_cast<CodeGenSchedModels &>(*this).getSchedRW(Def);
+  CodeGenSchedRW &getSchedRW(const Record *Def) {
+    return const_cast<CodeGenSchedRW &>(
+        static_cast<const CodeGenSchedModels &>(*this).getSchedRW(Def));
   }
 
   unsigned getSchedRWIdx(const Record *Def, bool IsRead) const;

``````````

</details>


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


More information about the llvm-commits mailing list