Skip to content

[LLVM][TableGen] Use const record pointers in TableGen/Common files #109467

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
merged 1 commit into from
Sep 23, 2024

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Sep 20, 2024

Use const record pointers in TableGen/Common files.

This is a part of effort to have better const correctness in TableGen backends:

https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089

@jurahul jurahul marked this pull request as ready for review September 21, 2024 04:44
@llvmbot
Copy link
Member

llvmbot commented Sep 21, 2024

@llvm/pr-subscribers-tablegen

@llvm/pr-subscribers-llvm-selectiondag

Author: Rahul Joshi (jurahul)

Changes

Use const record pointers in TableGen/Common files.

This is a part of effort to have better const correctness in TableGen backends:

https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089


Patch is 66.88 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/109467.diff

12 Files Affected:

  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp (+73-85)
  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.h (+43-41)
  • (modified) llvm/utils/TableGen/Common/CodeGenHwModes.cpp (+5-5)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.cpp (+16-17)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.h (+1-1)
  • (modified) llvm/utils/TableGen/Common/CodeGenRegisters.cpp (+12-7)
  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.cpp (+28-29)
  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.h (+4-4)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp (+2-2)
  • (modified) llvm/utils/TableGen/DAGISelMatcherGen.cpp (+4-4)
  • (modified) llvm/utils/TableGen/FastISelEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/GlobalISelEmitter.cpp (+37-35)
diff --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
index fd80bc681c70d9..e8cf7e3998e125 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
@@ -910,22 +910,16 @@ bool TreePredicateFn::hasPredCode() const {
 std::string TreePredicateFn::getPredCode() const {
   std::string Code;
 
-  if (!isLoad() && !isStore() && !isAtomic()) {
-    Record *MemoryVT = getMemoryVT();
-
-    if (MemoryVT)
-      PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
-                      "MemoryVT requires IsLoad or IsStore");
-  }
+  if (!isLoad() && !isStore() && !isAtomic() && getMemoryVT())
+    PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
+                    "MemoryVT requires IsLoad or IsStore");
 
   if (!isLoad() && !isStore()) {
     if (isUnindexed())
       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
                       "IsUnindexed requires IsLoad or IsStore");
 
-    Record *ScalarMemoryVT = getScalarMemoryVT();
-
-    if (ScalarMemoryVT)
+    if (getScalarMemoryVT())
       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
                       "ScalarMemoryVT requires IsLoad or IsStore");
   }
@@ -1016,15 +1010,15 @@ std::string TreePredicateFn::getPredCode() const {
   }
 
   if (isLoad() || isStore() || isAtomic()) {
-    if (ListInit *AddressSpaces = getAddressSpaces()) {
+    if (const ListInit *AddressSpaces = getAddressSpaces()) {
       Code += "unsigned AddrSpace = cast<MemSDNode>(N)->getAddressSpace();\n"
               " if (";
 
       ListSeparator LS(" && ");
-      for (Init *Val : AddressSpaces->getValues()) {
+      for (const Init *Val : AddressSpaces->getValues()) {
         Code += LS;
 
-        IntInit *IntVal = dyn_cast<IntInit>(Val);
+        const IntInit *IntVal = dyn_cast<IntInit>(Val);
         if (!IntVal) {
           PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
                           "AddressSpaces element must be integer");
@@ -1043,9 +1037,7 @@ std::string TreePredicateFn::getPredCode() const {
       Code += "))\nreturn false;\n";
     }
 
-    Record *MemoryVT = getMemoryVT();
-
-    if (MemoryVT)
+    if (const Record *MemoryVT = getMemoryVT())
       Code += ("if (cast<MemSDNode>(N)->getMemoryVT() != MVT::" +
                MemoryVT->getName() + ") return false;\n")
                   .str();
@@ -1129,9 +1121,7 @@ std::string TreePredicateFn::getPredCode() const {
             " if (!cast<StoreSDNode>(N)->isTruncatingStore()) return false;\n";
     }
 
-    Record *ScalarMemoryVT = getScalarMemoryVT();
-
-    if (ScalarMemoryVT)
+    if (const Record *ScalarMemoryVT = getScalarMemoryVT())
       Code += ("if (cast<" + SDNodeName +
                ">(N)->getMemoryVT().getScalarType() != MVT::" +
                ScalarMemoryVT->getName() + ") return false;\n")
@@ -1254,14 +1244,14 @@ bool TreePredicateFn::isAtomicOrderingWeakerThanRelease() const {
   return isPredefinedPredicateEqualTo("IsAtomicOrderingReleaseOrStronger",
                                       false);
 }
-Record *TreePredicateFn::getMemoryVT() const {
+const Record *TreePredicateFn::getMemoryVT() const {
   const Record *R = getOrigPatFragRecord()->getRecord();
   if (R->isValueUnset("MemoryVT"))
     return nullptr;
   return R->getValueAsDef("MemoryVT");
 }
 
-ListInit *TreePredicateFn::getAddressSpaces() const {
+const ListInit *TreePredicateFn::getAddressSpaces() const {
   const Record *R = getOrigPatFragRecord()->getRecord();
   if (R->isValueUnset("AddressSpaces"))
     return nullptr;
@@ -1275,7 +1265,7 @@ int64_t TreePredicateFn::getMinAlignment() const {
   return R->getValueAsInt("MinAlignment");
 }
 
-Record *TreePredicateFn::getScalarMemoryVT() const {
+const Record *TreePredicateFn::getScalarMemoryVT() const {
   const Record *R = getOrigPatFragRecord()->getRecord();
   if (R->isValueUnset("ScalarMemoryVT"))
     return nullptr;
@@ -1419,11 +1409,11 @@ std::string TreePredicateFn::getCodeToRunOnSDNode() const {
 static bool isImmAllOnesAllZerosMatch(const TreePatternNode &P) {
   if (!P.isLeaf())
     return false;
-  DefInit *DI = dyn_cast<DefInit>(P.getLeafValue());
+  const DefInit *DI = dyn_cast<DefInit>(P.getLeafValue());
   if (!DI)
     return false;
 
-  Record *R = DI->getDef();
+  const Record *R = DI->getDef();
   return R->getName() == "immAllOnesV" || R->getName() == "immAllZerosV";
 }
 
@@ -1483,10 +1473,10 @@ int PatternToMatch::getPatternComplexity(const CodeGenDAGPatterns &CGP) const {
 }
 
 void PatternToMatch::getPredicateRecords(
-    SmallVectorImpl<Record *> &PredicateRecs) const {
-  for (Init *I : Predicates->getValues()) {
-    if (DefInit *Pred = dyn_cast<DefInit>(I)) {
-      Record *Def = Pred->getDef();
+    SmallVectorImpl<const Record *> &PredicateRecs) const {
+  for (const Init *I : Predicates->getValues()) {
+    if (const DefInit *Pred = dyn_cast<DefInit>(I)) {
+      const Record *Def = Pred->getDef();
       if (!Def->isSubClassOf("Predicate")) {
 #ifndef NDEBUG
         Def->dump();
@@ -1506,13 +1496,13 @@ void PatternToMatch::getPredicateRecords(
 /// pattern's predicates concatenated with "&&" operators.
 ///
 std::string PatternToMatch::getPredicateCheck() const {
-  SmallVector<Record *, 4> PredicateRecs;
+  SmallVector<const Record *, 4> PredicateRecs;
   getPredicateRecords(PredicateRecs);
 
   SmallString<128> PredicateCheck;
   raw_svector_ostream OS(PredicateCheck);
   ListSeparator LS(" && ");
-  for (Record *Pred : PredicateRecs) {
+  for (const Record *Pred : PredicateRecs) {
     StringRef CondString = Pred->getValueAsString("CondString");
     if (CondString.empty())
       continue;
@@ -1659,7 +1649,7 @@ bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode &N,
       TP.error(N.getOperator()->getName() + " expects a VT operand!");
       return false;
     }
-    DefInit *DI = cast<DefInit>(NodeToApply.getLeafValue());
+    const DefInit *DI = cast<DefInit>(NodeToApply.getLeafValue());
     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
     auto VVT = getValueTypeByHwMode(DI->getDef(), T.getHwModes());
     TypeSetByHwMode TypeListTmp(VVT);
@@ -1731,7 +1721,7 @@ bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo,
 
   // The Operand class specifies a type directly.
   if (Operand->isSubClassOf("Operand")) {
-    Record *R = Operand->getValueAsDef("Type");
+    const Record *R = Operand->getValueAsDef("Type");
     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
     return UpdateNodeType(ResNo, getValueTypeByHwMode(R, T.getHwModes()), TP);
   }
@@ -1802,7 +1792,7 @@ bool TreePatternNode::setDefaultMode(unsigned Mode) {
 SDNodeInfo::SDNodeInfo(const Record *R, const CodeGenHwModes &CGH) : Def(R) {
   EnumName = R->getValueAsString("Opcode");
   SDClassName = R->getValueAsString("SDClass");
-  Record *TypeProfile = R->getValueAsDef("TypeProfile");
+  const Record *TypeProfile = R->getValueAsDef("TypeProfile");
   NumResults = TypeProfile->getValueAsInt("NumResults");
   NumOperands = TypeProfile->getValueAsInt("NumOperands");
 
@@ -1810,9 +1800,7 @@ SDNodeInfo::SDNodeInfo(const Record *R, const CodeGenHwModes &CGH) : Def(R) {
   Properties = parseSDPatternOperatorProperties(R);
 
   // Parse the type constraints.
-  std::vector<Record *> ConstraintList =
-      TypeProfile->getValueAsListOfDefs("Constraints");
-  for (Record *R : ConstraintList)
+  for (const Record *R : TypeProfile->getValueAsListOfDefs("Constraints"))
     TypeConstraints.emplace_back(R, CGH);
 }
 
@@ -1872,13 +1860,13 @@ static unsigned GetNumNodeResults(const Record *Operator,
       return NumResults;
     }
 
-    ListInit *LI = Operator->getValueAsListInit("Fragments");
+    const ListInit *LI = Operator->getValueAsListInit("Fragments");
     assert(LI && "Invalid Fragment");
     unsigned NumResults = 0;
-    for (Init *I : LI->getValues()) {
-      Record *Op = nullptr;
-      if (DagInit *Dag = dyn_cast<DagInit>(I))
-        if (DefInit *DI = dyn_cast<DefInit>(Dag->getOperator()))
+    for (const Init *I : LI->getValues()) {
+      const Record *Op = nullptr;
+      if (const DagInit *Dag = dyn_cast<DagInit>(I))
+        if (const DefInit *DI = dyn_cast<DefInit>(Dag->getOperator()))
           Op = DI->getDef();
       assert(Op && "Invalid Fragment");
       NumResults = std::max(NumResults, GetNumNodeResults(Op, CDP));
@@ -1986,8 +1974,8 @@ bool TreePatternNode::isIsomorphicTo(const TreePatternNode &N,
     return false;
 
   if (isLeaf()) {
-    if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
-      if (DefInit *NDI = dyn_cast<DefInit>(N.getLeafValue())) {
+    if (const DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
+      if (const DefInit *NDI = dyn_cast<DefInit>(N.getLeafValue())) {
         return ((DI->getDef() == NDI->getDef()) &&
                 (!DepVars.contains(getName()) || getName() == N.getName()));
       }
@@ -2044,7 +2032,7 @@ void TreePatternNode::SubstituteFormalArguments(
   for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
     TreePatternNode &Child = getChild(i);
     if (Child.isLeaf()) {
-      Init *Val = Child.getLeafValue();
+      const Init *Val = Child.getLeafValue();
       // Note that, when substituting into an output pattern, Val might be an
       // UnsetInit.
       if (isa<UnsetInit>(Val) ||
@@ -2217,7 +2205,7 @@ void TreePatternNode::InlinePatternFragments(
 /// When Unnamed is false, return the type of a named DAG operand such as the
 /// GPR:$src operand above.
 ///
-static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
+static TypeSetByHwMode getImplicitType(const Record *R, unsigned ResNo,
                                        bool NotRegisters, bool Unnamed,
                                        TreePattern &TP) {
   CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
@@ -2227,7 +2215,7 @@ static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
     assert(ResNo == 0 && "Regoperand ref only has one result!");
     if (NotRegisters)
       return TypeSetByHwMode(); // Unknown.
-    Record *RegClass = R->getValueAsDef("RegClass");
+    const Record *RegClass = R->getValueAsDef("RegClass");
     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
     return TypeSetByHwMode(T.getRegisterClass(RegClass).getValueTypes());
   }
@@ -2316,7 +2304,7 @@ static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
 
   if (R->isSubClassOf("Operand")) {
     const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
-    Record *T = R->getValueAsDef("Type");
+    const Record *T = R->getValueAsDef("Type");
     return TypeSetByHwMode(getValueTypeByHwMode(T, CGH));
   }
 
@@ -2343,7 +2331,7 @@ const ComplexPattern *
 TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const {
   const Record *Rec;
   if (isLeaf()) {
-    DefInit *DI = dyn_cast<DefInit>(getLeafValue());
+    const DefInit *DI = dyn_cast<DefInit>(getLeafValue());
     if (!DI)
       return nullptr;
     Rec = DI->getDef();
@@ -2362,9 +2350,9 @@ unsigned TreePatternNode::getNumMIResults(const CodeGenDAGPatterns &CGP) const {
 
   // If MIOperandInfo is specified, that gives the count.
   if (isLeaf()) {
-    DefInit *DI = dyn_cast<DefInit>(getLeafValue());
+    const DefInit *DI = dyn_cast<DefInit>(getLeafValue());
     if (DI && DI->getDef()->isSubClassOf("Operand")) {
-      DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo");
+      const DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo");
       if (MIOps->getNumArgs())
         return MIOps->getNumArgs();
     }
@@ -2423,7 +2411,7 @@ static bool isOperandClass(const TreePatternNode &N, StringRef Class) {
   if (!N.isLeaf())
     return N.getOperator()->isSubClassOf(Class);
 
-  DefInit *DI = dyn_cast<DefInit>(N.getLeafValue());
+  const DefInit *DI = dyn_cast<DefInit>(N.getLeafValue());
   if (DI && DI->getDef()->isSubClassOf(Class))
     return true;
 
@@ -2451,7 +2439,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
 
   CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
   if (isLeaf()) {
-    if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
+    if (const DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
       // If it's a regclass or something else known, include the type.
       bool MadeChange = false;
       for (unsigned i = 0, e = Types.size(); i != e; ++i)
@@ -2461,7 +2449,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
       return MadeChange;
     }
 
-    if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) {
+    if (const IntInit *II = dyn_cast<IntInit>(getLeafValue())) {
       assert(Types.size() == 1 && "Invalid IntInit");
 
       // Int inits are always integers. :)
@@ -2658,7 +2646,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
 
           if (Child->getNumMIResults(CDP) < NumArgs) {
             // Match first sub-operand against the child we already have.
-            Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef();
+            const Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef();
             MadeChange |= Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP);
 
             // And the remaining sub-operands against subsequent children.
@@ -2794,8 +2782,8 @@ bool TreePatternNode::canPatternMatch(std::string &Reason,
 // TreePattern implementation
 //
 
-TreePattern::TreePattern(const Record *TheRec, ListInit *RawPat, bool isInput,
-                         CodeGenDAGPatterns &cdp)
+TreePattern::TreePattern(const Record *TheRec, const ListInit *RawPat,
+                         bool isInput, CodeGenDAGPatterns &cdp)
     : TheRecord(TheRec), CDP(cdp), isInputPattern(isInput), HasError(false),
       Infer(*this) {
   for (Init *I : RawPat->getValues())
@@ -2840,8 +2828,10 @@ void TreePattern::ComputeNamedNodes(TreePatternNode &N) {
 TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit,
                                                  StringRef OpName) {
   RecordKeeper &RK = TheInit->getRecordKeeper();
+  // Here, we are creating new records (BitsInit->InitInit), so const_cast
+  // TheInit back to non-const pointer.
   if (DefInit *DI = dyn_cast<DefInit>(TheInit)) {
-    Record *R = DI->getDef();
+    const Record *R = DI->getDef();
 
     // Direct reference to a leaf DagNode or PatFrag?  Turn it into a
     // TreePatternNode of its own.  For example:
@@ -2901,7 +2891,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit,
     error("Pattern has unexpected operator type!");
     return nullptr;
   }
-  Record *Operator = OpDef->getDef();
+  const Record *Operator = OpDef->getDef();
 
   if (Operator->isSubClassOf("ValueType")) {
     // If the operator is a ValueType, then this must be "type cast" of a leaf
@@ -2996,7 +2986,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit,
       // Check that the ComplexPattern uses are consistent: "(MY_PAT $a, $b)"
       // and "(MY_PAT $b, $a)" should not be allowed in the same pattern;
       // neither should "(MY_PAT_1 $a, $b)" and "(MY_PAT_2 $a, $b)".
-      auto OperandId = std::pair(Operator, i);
+      auto OperandId = std::make_pair(Operator, i);
       auto PrevOp = ComplexPatternOperands.find(Child->getName());
       if (PrevOp != ComplexPatternOperands.end()) {
         if (PrevOp->getValue() != OperandId)
@@ -3094,7 +3084,7 @@ bool TreePattern::InferAllTypes(
           // us to match things like:
           //  def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>;
           if (Node == Trees[0].get() && Node->isLeaf()) {
-            DefInit *DI = dyn_cast<DefInit>(Node->getLeafValue());
+            const DefInit *DI = dyn_cast<DefInit>(Node->getLeafValue());
             if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
                        DI->getDef()->isSubClassOf("RegisterOperand")))
               continue;
@@ -3188,11 +3178,10 @@ CodeGenDAGPatterns::CodeGenDAGPatterns(const RecordKeeper &R,
   VerifyInstructionFlags();
 }
 
-Record *CodeGenDAGPatterns::getSDNodeNamed(StringRef Name) const {
-  Record *N = Records.getDef(Name);
+const Record *CodeGenDAGPatterns::getSDNodeNamed(StringRef Name) const {
+  const Record *N = Records.getDef(Name);
   if (!N || !N->isSubClassOf("SDNode"))
     PrintFatalError("Error getting SDNode '" + Name + "'!");
-
   return N;
 }
 
@@ -3286,7 +3275,7 @@ void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
 
     // If there is a node transformation corresponding to this, keep track of
     // it.
-    Record *Transform = Frag->getValueAsDef("OperandTransform");
+    const Record *Transform = Frag->getValueAsDef("OperandTransform");
     if (!getSDNodeTransform(Transform).second.empty()) // not noop xform?
       for (const auto &T : P->getTrees())
         T->setTransformFn(Transform);
@@ -3369,7 +3358,7 @@ static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
   // No name -> not interesting.
   if (Pat->getName().empty()) {
     if (Pat->isLeaf()) {
-      DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
+      const DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
       if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
                  DI->getDef()->isSubClassOf("RegisterOperand")))
         I.error("Input " + DI->getDef()->getName() + " must be named!");
@@ -3379,7 +3368,7 @@ static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
 
   const Record *Rec;
   if (Pat->isLeaf()) {
-    DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
+    const DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
     if (!DI)
       I.error("Input $" + Pat->getName() + " must be an identifier!");
     Rec = DI->getDef();
@@ -3423,8 +3412,7 @@ void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
     std::map<std::string, TreePatternNodePtr> &InstInputs,
     MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
         &InstResults,
-    std::vector<Record *> &InstImpResults) {
-
+    std::vector<const Record *> &InstImpResults) {
   // The instruction pattern still has unresolved fragments.  For *named*
   // nodes we must resolve those here.  This may not result in multiple
   // alternatives.
@@ -3448,7 +3436,7 @@ void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
       if (!Dest.isLeaf())
         I.error("implicitly defined value should be a register!");
 
-      DefInit *Val = dyn_cast<DefInit>(Dest.getLeafValue());
+      const DefInit *Val = dyn_cast<DefInit>(Dest.getLeafValue());
       if (!Val || !Val->getDef()->isSubClassOf("Register"))
         I.error("implicitly defined value should be a register!");
       if (Val)
@@ -3496,7 +3484,7 @@ void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
     if (!Dest->isLeaf())
       I.error("set destination should be a register!");
 
-    DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
+    const DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
     if (!Val) {
       I.error("set destination should be a register!");
       continue;
@@ -3571,8 +3559,8 @@ class InstAnalyzer {
 public:
   void AnalyzeNode(const TreePatternNode &N) {
     if (N.isLeaf()) {
-      if (DefInit *DI = dyn_cast<DefInit>(N.getLeafValue())) {
-        Record *LeafRec = DI->getDef();
+      if (const DefInit *DI = dyn_cast<DefInit>(N.getLeafValue())) {
+        const Record *LeafRec = DI->getDef();
         // Handle ComplexPattern leaves.
         if (LeafRec->isSubClassOf("ComplexPattern")) {
           const ComplexPattern &CP = CDP.getComplexPattern(LeafRec);
@@ -3729,7 +3717,8 @@ static void getInstructionsInTree(TreePatternNode &Tree,
 
 /// Check the class of a pattern leaf node against the instruction operand it
 /// represents.
-static bool checkOperandClass(CGIOperandList::OperandInfo &OI, Record *Leaf) {
+static bool checkOperandClass(CGIOperandList::OperandInfo &OI,
+                              const Record *Leaf) {
   if (OI.Rec == Leaf)
     return true;
 
@@ -3746,7 +3735,7 @@ static bool checkOperandClass(CGIOperandList::OperandInfo &OI, Record *Leaf) {
 }
 
 void CodeGenDAGPatterns::parseInstructionPattern(CodeGenInstruction &CGI,
-                                                 ListInit *Pat,
+                                                 const ListInit *Pat,
                   ...
[truncated]

@llvmbot
Copy link
Member

llvmbot commented Sep 21, 2024

@llvm/pr-subscribers-llvm-globalisel

Author: Rahul Joshi (jurahul)

Changes

Use const record pointers in TableGen/Common files.

This is a part of effort to have better const correctness in TableGen backends:

https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089


Patch is 66.88 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/109467.diff

12 Files Affected:

  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp (+73-85)
  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.h (+43-41)
  • (modified) llvm/utils/TableGen/Common/CodeGenHwModes.cpp (+5-5)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.cpp (+16-17)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.h (+1-1)
  • (modified) llvm/utils/TableGen/Common/CodeGenRegisters.cpp (+12-7)
  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.cpp (+28-29)
  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.h (+4-4)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp (+2-2)
  • (modified) llvm/utils/TableGen/DAGISelMatcherGen.cpp (+4-4)
  • (modified) llvm/utils/TableGen/FastISelEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/GlobalISelEmitter.cpp (+37-35)
diff --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
index fd80bc681c70d9..e8cf7e3998e125 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
@@ -910,22 +910,16 @@ bool TreePredicateFn::hasPredCode() const {
 std::string TreePredicateFn::getPredCode() const {
   std::string Code;
 
-  if (!isLoad() && !isStore() && !isAtomic()) {
-    Record *MemoryVT = getMemoryVT();
-
-    if (MemoryVT)
-      PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
-                      "MemoryVT requires IsLoad or IsStore");
-  }
+  if (!isLoad() && !isStore() && !isAtomic() && getMemoryVT())
+    PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
+                    "MemoryVT requires IsLoad or IsStore");
 
   if (!isLoad() && !isStore()) {
     if (isUnindexed())
       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
                       "IsUnindexed requires IsLoad or IsStore");
 
-    Record *ScalarMemoryVT = getScalarMemoryVT();
-
-    if (ScalarMemoryVT)
+    if (getScalarMemoryVT())
       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
                       "ScalarMemoryVT requires IsLoad or IsStore");
   }
@@ -1016,15 +1010,15 @@ std::string TreePredicateFn::getPredCode() const {
   }
 
   if (isLoad() || isStore() || isAtomic()) {
-    if (ListInit *AddressSpaces = getAddressSpaces()) {
+    if (const ListInit *AddressSpaces = getAddressSpaces()) {
       Code += "unsigned AddrSpace = cast<MemSDNode>(N)->getAddressSpace();\n"
               " if (";
 
       ListSeparator LS(" && ");
-      for (Init *Val : AddressSpaces->getValues()) {
+      for (const Init *Val : AddressSpaces->getValues()) {
         Code += LS;
 
-        IntInit *IntVal = dyn_cast<IntInit>(Val);
+        const IntInit *IntVal = dyn_cast<IntInit>(Val);
         if (!IntVal) {
           PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
                           "AddressSpaces element must be integer");
@@ -1043,9 +1037,7 @@ std::string TreePredicateFn::getPredCode() const {
       Code += "))\nreturn false;\n";
     }
 
-    Record *MemoryVT = getMemoryVT();
-
-    if (MemoryVT)
+    if (const Record *MemoryVT = getMemoryVT())
       Code += ("if (cast<MemSDNode>(N)->getMemoryVT() != MVT::" +
                MemoryVT->getName() + ") return false;\n")
                   .str();
@@ -1129,9 +1121,7 @@ std::string TreePredicateFn::getPredCode() const {
             " if (!cast<StoreSDNode>(N)->isTruncatingStore()) return false;\n";
     }
 
-    Record *ScalarMemoryVT = getScalarMemoryVT();
-
-    if (ScalarMemoryVT)
+    if (const Record *ScalarMemoryVT = getScalarMemoryVT())
       Code += ("if (cast<" + SDNodeName +
                ">(N)->getMemoryVT().getScalarType() != MVT::" +
                ScalarMemoryVT->getName() + ") return false;\n")
@@ -1254,14 +1244,14 @@ bool TreePredicateFn::isAtomicOrderingWeakerThanRelease() const {
   return isPredefinedPredicateEqualTo("IsAtomicOrderingReleaseOrStronger",
                                       false);
 }
-Record *TreePredicateFn::getMemoryVT() const {
+const Record *TreePredicateFn::getMemoryVT() const {
   const Record *R = getOrigPatFragRecord()->getRecord();
   if (R->isValueUnset("MemoryVT"))
     return nullptr;
   return R->getValueAsDef("MemoryVT");
 }
 
-ListInit *TreePredicateFn::getAddressSpaces() const {
+const ListInit *TreePredicateFn::getAddressSpaces() const {
   const Record *R = getOrigPatFragRecord()->getRecord();
   if (R->isValueUnset("AddressSpaces"))
     return nullptr;
@@ -1275,7 +1265,7 @@ int64_t TreePredicateFn::getMinAlignment() const {
   return R->getValueAsInt("MinAlignment");
 }
 
-Record *TreePredicateFn::getScalarMemoryVT() const {
+const Record *TreePredicateFn::getScalarMemoryVT() const {
   const Record *R = getOrigPatFragRecord()->getRecord();
   if (R->isValueUnset("ScalarMemoryVT"))
     return nullptr;
@@ -1419,11 +1409,11 @@ std::string TreePredicateFn::getCodeToRunOnSDNode() const {
 static bool isImmAllOnesAllZerosMatch(const TreePatternNode &P) {
   if (!P.isLeaf())
     return false;
-  DefInit *DI = dyn_cast<DefInit>(P.getLeafValue());
+  const DefInit *DI = dyn_cast<DefInit>(P.getLeafValue());
   if (!DI)
     return false;
 
-  Record *R = DI->getDef();
+  const Record *R = DI->getDef();
   return R->getName() == "immAllOnesV" || R->getName() == "immAllZerosV";
 }
 
@@ -1483,10 +1473,10 @@ int PatternToMatch::getPatternComplexity(const CodeGenDAGPatterns &CGP) const {
 }
 
 void PatternToMatch::getPredicateRecords(
-    SmallVectorImpl<Record *> &PredicateRecs) const {
-  for (Init *I : Predicates->getValues()) {
-    if (DefInit *Pred = dyn_cast<DefInit>(I)) {
-      Record *Def = Pred->getDef();
+    SmallVectorImpl<const Record *> &PredicateRecs) const {
+  for (const Init *I : Predicates->getValues()) {
+    if (const DefInit *Pred = dyn_cast<DefInit>(I)) {
+      const Record *Def = Pred->getDef();
       if (!Def->isSubClassOf("Predicate")) {
 #ifndef NDEBUG
         Def->dump();
@@ -1506,13 +1496,13 @@ void PatternToMatch::getPredicateRecords(
 /// pattern's predicates concatenated with "&&" operators.
 ///
 std::string PatternToMatch::getPredicateCheck() const {
-  SmallVector<Record *, 4> PredicateRecs;
+  SmallVector<const Record *, 4> PredicateRecs;
   getPredicateRecords(PredicateRecs);
 
   SmallString<128> PredicateCheck;
   raw_svector_ostream OS(PredicateCheck);
   ListSeparator LS(" && ");
-  for (Record *Pred : PredicateRecs) {
+  for (const Record *Pred : PredicateRecs) {
     StringRef CondString = Pred->getValueAsString("CondString");
     if (CondString.empty())
       continue;
@@ -1659,7 +1649,7 @@ bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode &N,
       TP.error(N.getOperator()->getName() + " expects a VT operand!");
       return false;
     }
-    DefInit *DI = cast<DefInit>(NodeToApply.getLeafValue());
+    const DefInit *DI = cast<DefInit>(NodeToApply.getLeafValue());
     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
     auto VVT = getValueTypeByHwMode(DI->getDef(), T.getHwModes());
     TypeSetByHwMode TypeListTmp(VVT);
@@ -1731,7 +1721,7 @@ bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo,
 
   // The Operand class specifies a type directly.
   if (Operand->isSubClassOf("Operand")) {
-    Record *R = Operand->getValueAsDef("Type");
+    const Record *R = Operand->getValueAsDef("Type");
     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
     return UpdateNodeType(ResNo, getValueTypeByHwMode(R, T.getHwModes()), TP);
   }
@@ -1802,7 +1792,7 @@ bool TreePatternNode::setDefaultMode(unsigned Mode) {
 SDNodeInfo::SDNodeInfo(const Record *R, const CodeGenHwModes &CGH) : Def(R) {
   EnumName = R->getValueAsString("Opcode");
   SDClassName = R->getValueAsString("SDClass");
-  Record *TypeProfile = R->getValueAsDef("TypeProfile");
+  const Record *TypeProfile = R->getValueAsDef("TypeProfile");
   NumResults = TypeProfile->getValueAsInt("NumResults");
   NumOperands = TypeProfile->getValueAsInt("NumOperands");
 
@@ -1810,9 +1800,7 @@ SDNodeInfo::SDNodeInfo(const Record *R, const CodeGenHwModes &CGH) : Def(R) {
   Properties = parseSDPatternOperatorProperties(R);
 
   // Parse the type constraints.
-  std::vector<Record *> ConstraintList =
-      TypeProfile->getValueAsListOfDefs("Constraints");
-  for (Record *R : ConstraintList)
+  for (const Record *R : TypeProfile->getValueAsListOfDefs("Constraints"))
     TypeConstraints.emplace_back(R, CGH);
 }
 
@@ -1872,13 +1860,13 @@ static unsigned GetNumNodeResults(const Record *Operator,
       return NumResults;
     }
 
-    ListInit *LI = Operator->getValueAsListInit("Fragments");
+    const ListInit *LI = Operator->getValueAsListInit("Fragments");
     assert(LI && "Invalid Fragment");
     unsigned NumResults = 0;
-    for (Init *I : LI->getValues()) {
-      Record *Op = nullptr;
-      if (DagInit *Dag = dyn_cast<DagInit>(I))
-        if (DefInit *DI = dyn_cast<DefInit>(Dag->getOperator()))
+    for (const Init *I : LI->getValues()) {
+      const Record *Op = nullptr;
+      if (const DagInit *Dag = dyn_cast<DagInit>(I))
+        if (const DefInit *DI = dyn_cast<DefInit>(Dag->getOperator()))
           Op = DI->getDef();
       assert(Op && "Invalid Fragment");
       NumResults = std::max(NumResults, GetNumNodeResults(Op, CDP));
@@ -1986,8 +1974,8 @@ bool TreePatternNode::isIsomorphicTo(const TreePatternNode &N,
     return false;
 
   if (isLeaf()) {
-    if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
-      if (DefInit *NDI = dyn_cast<DefInit>(N.getLeafValue())) {
+    if (const DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
+      if (const DefInit *NDI = dyn_cast<DefInit>(N.getLeafValue())) {
         return ((DI->getDef() == NDI->getDef()) &&
                 (!DepVars.contains(getName()) || getName() == N.getName()));
       }
@@ -2044,7 +2032,7 @@ void TreePatternNode::SubstituteFormalArguments(
   for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
     TreePatternNode &Child = getChild(i);
     if (Child.isLeaf()) {
-      Init *Val = Child.getLeafValue();
+      const Init *Val = Child.getLeafValue();
       // Note that, when substituting into an output pattern, Val might be an
       // UnsetInit.
       if (isa<UnsetInit>(Val) ||
@@ -2217,7 +2205,7 @@ void TreePatternNode::InlinePatternFragments(
 /// When Unnamed is false, return the type of a named DAG operand such as the
 /// GPR:$src operand above.
 ///
-static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
+static TypeSetByHwMode getImplicitType(const Record *R, unsigned ResNo,
                                        bool NotRegisters, bool Unnamed,
                                        TreePattern &TP) {
   CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
@@ -2227,7 +2215,7 @@ static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
     assert(ResNo == 0 && "Regoperand ref only has one result!");
     if (NotRegisters)
       return TypeSetByHwMode(); // Unknown.
-    Record *RegClass = R->getValueAsDef("RegClass");
+    const Record *RegClass = R->getValueAsDef("RegClass");
     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
     return TypeSetByHwMode(T.getRegisterClass(RegClass).getValueTypes());
   }
@@ -2316,7 +2304,7 @@ static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
 
   if (R->isSubClassOf("Operand")) {
     const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
-    Record *T = R->getValueAsDef("Type");
+    const Record *T = R->getValueAsDef("Type");
     return TypeSetByHwMode(getValueTypeByHwMode(T, CGH));
   }
 
@@ -2343,7 +2331,7 @@ const ComplexPattern *
 TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const {
   const Record *Rec;
   if (isLeaf()) {
-    DefInit *DI = dyn_cast<DefInit>(getLeafValue());
+    const DefInit *DI = dyn_cast<DefInit>(getLeafValue());
     if (!DI)
       return nullptr;
     Rec = DI->getDef();
@@ -2362,9 +2350,9 @@ unsigned TreePatternNode::getNumMIResults(const CodeGenDAGPatterns &CGP) const {
 
   // If MIOperandInfo is specified, that gives the count.
   if (isLeaf()) {
-    DefInit *DI = dyn_cast<DefInit>(getLeafValue());
+    const DefInit *DI = dyn_cast<DefInit>(getLeafValue());
     if (DI && DI->getDef()->isSubClassOf("Operand")) {
-      DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo");
+      const DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo");
       if (MIOps->getNumArgs())
         return MIOps->getNumArgs();
     }
@@ -2423,7 +2411,7 @@ static bool isOperandClass(const TreePatternNode &N, StringRef Class) {
   if (!N.isLeaf())
     return N.getOperator()->isSubClassOf(Class);
 
-  DefInit *DI = dyn_cast<DefInit>(N.getLeafValue());
+  const DefInit *DI = dyn_cast<DefInit>(N.getLeafValue());
   if (DI && DI->getDef()->isSubClassOf(Class))
     return true;
 
@@ -2451,7 +2439,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
 
   CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
   if (isLeaf()) {
-    if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
+    if (const DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
       // If it's a regclass or something else known, include the type.
       bool MadeChange = false;
       for (unsigned i = 0, e = Types.size(); i != e; ++i)
@@ -2461,7 +2449,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
       return MadeChange;
     }
 
-    if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) {
+    if (const IntInit *II = dyn_cast<IntInit>(getLeafValue())) {
       assert(Types.size() == 1 && "Invalid IntInit");
 
       // Int inits are always integers. :)
@@ -2658,7 +2646,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
 
           if (Child->getNumMIResults(CDP) < NumArgs) {
             // Match first sub-operand against the child we already have.
-            Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef();
+            const Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef();
             MadeChange |= Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP);
 
             // And the remaining sub-operands against subsequent children.
@@ -2794,8 +2782,8 @@ bool TreePatternNode::canPatternMatch(std::string &Reason,
 // TreePattern implementation
 //
 
-TreePattern::TreePattern(const Record *TheRec, ListInit *RawPat, bool isInput,
-                         CodeGenDAGPatterns &cdp)
+TreePattern::TreePattern(const Record *TheRec, const ListInit *RawPat,
+                         bool isInput, CodeGenDAGPatterns &cdp)
     : TheRecord(TheRec), CDP(cdp), isInputPattern(isInput), HasError(false),
       Infer(*this) {
   for (Init *I : RawPat->getValues())
@@ -2840,8 +2828,10 @@ void TreePattern::ComputeNamedNodes(TreePatternNode &N) {
 TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit,
                                                  StringRef OpName) {
   RecordKeeper &RK = TheInit->getRecordKeeper();
+  // Here, we are creating new records (BitsInit->InitInit), so const_cast
+  // TheInit back to non-const pointer.
   if (DefInit *DI = dyn_cast<DefInit>(TheInit)) {
-    Record *R = DI->getDef();
+    const Record *R = DI->getDef();
 
     // Direct reference to a leaf DagNode or PatFrag?  Turn it into a
     // TreePatternNode of its own.  For example:
@@ -2901,7 +2891,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit,
     error("Pattern has unexpected operator type!");
     return nullptr;
   }
-  Record *Operator = OpDef->getDef();
+  const Record *Operator = OpDef->getDef();
 
   if (Operator->isSubClassOf("ValueType")) {
     // If the operator is a ValueType, then this must be "type cast" of a leaf
@@ -2996,7 +2986,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit,
       // Check that the ComplexPattern uses are consistent: "(MY_PAT $a, $b)"
       // and "(MY_PAT $b, $a)" should not be allowed in the same pattern;
       // neither should "(MY_PAT_1 $a, $b)" and "(MY_PAT_2 $a, $b)".
-      auto OperandId = std::pair(Operator, i);
+      auto OperandId = std::make_pair(Operator, i);
       auto PrevOp = ComplexPatternOperands.find(Child->getName());
       if (PrevOp != ComplexPatternOperands.end()) {
         if (PrevOp->getValue() != OperandId)
@@ -3094,7 +3084,7 @@ bool TreePattern::InferAllTypes(
           // us to match things like:
           //  def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>;
           if (Node == Trees[0].get() && Node->isLeaf()) {
-            DefInit *DI = dyn_cast<DefInit>(Node->getLeafValue());
+            const DefInit *DI = dyn_cast<DefInit>(Node->getLeafValue());
             if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
                        DI->getDef()->isSubClassOf("RegisterOperand")))
               continue;
@@ -3188,11 +3178,10 @@ CodeGenDAGPatterns::CodeGenDAGPatterns(const RecordKeeper &R,
   VerifyInstructionFlags();
 }
 
-Record *CodeGenDAGPatterns::getSDNodeNamed(StringRef Name) const {
-  Record *N = Records.getDef(Name);
+const Record *CodeGenDAGPatterns::getSDNodeNamed(StringRef Name) const {
+  const Record *N = Records.getDef(Name);
   if (!N || !N->isSubClassOf("SDNode"))
     PrintFatalError("Error getting SDNode '" + Name + "'!");
-
   return N;
 }
 
@@ -3286,7 +3275,7 @@ void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
 
     // If there is a node transformation corresponding to this, keep track of
     // it.
-    Record *Transform = Frag->getValueAsDef("OperandTransform");
+    const Record *Transform = Frag->getValueAsDef("OperandTransform");
     if (!getSDNodeTransform(Transform).second.empty()) // not noop xform?
       for (const auto &T : P->getTrees())
         T->setTransformFn(Transform);
@@ -3369,7 +3358,7 @@ static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
   // No name -> not interesting.
   if (Pat->getName().empty()) {
     if (Pat->isLeaf()) {
-      DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
+      const DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
       if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
                  DI->getDef()->isSubClassOf("RegisterOperand")))
         I.error("Input " + DI->getDef()->getName() + " must be named!");
@@ -3379,7 +3368,7 @@ static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
 
   const Record *Rec;
   if (Pat->isLeaf()) {
-    DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
+    const DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
     if (!DI)
       I.error("Input $" + Pat->getName() + " must be an identifier!");
     Rec = DI->getDef();
@@ -3423,8 +3412,7 @@ void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
     std::map<std::string, TreePatternNodePtr> &InstInputs,
     MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
         &InstResults,
-    std::vector<Record *> &InstImpResults) {
-
+    std::vector<const Record *> &InstImpResults) {
   // The instruction pattern still has unresolved fragments.  For *named*
   // nodes we must resolve those here.  This may not result in multiple
   // alternatives.
@@ -3448,7 +3436,7 @@ void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
       if (!Dest.isLeaf())
         I.error("implicitly defined value should be a register!");
 
-      DefInit *Val = dyn_cast<DefInit>(Dest.getLeafValue());
+      const DefInit *Val = dyn_cast<DefInit>(Dest.getLeafValue());
       if (!Val || !Val->getDef()->isSubClassOf("Register"))
         I.error("implicitly defined value should be a register!");
       if (Val)
@@ -3496,7 +3484,7 @@ void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
     if (!Dest->isLeaf())
       I.error("set destination should be a register!");
 
-    DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
+    const DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
     if (!Val) {
       I.error("set destination should be a register!");
       continue;
@@ -3571,8 +3559,8 @@ class InstAnalyzer {
 public:
   void AnalyzeNode(const TreePatternNode &N) {
     if (N.isLeaf()) {
-      if (DefInit *DI = dyn_cast<DefInit>(N.getLeafValue())) {
-        Record *LeafRec = DI->getDef();
+      if (const DefInit *DI = dyn_cast<DefInit>(N.getLeafValue())) {
+        const Record *LeafRec = DI->getDef();
         // Handle ComplexPattern leaves.
         if (LeafRec->isSubClassOf("ComplexPattern")) {
           const ComplexPattern &CP = CDP.getComplexPattern(LeafRec);
@@ -3729,7 +3717,8 @@ static void getInstructionsInTree(TreePatternNode &Tree,
 
 /// Check the class of a pattern leaf node against the instruction operand it
 /// represents.
-static bool checkOperandClass(CGIOperandList::OperandInfo &OI, Record *Leaf) {
+static bool checkOperandClass(CGIOperandList::OperandInfo &OI,
+                              const Record *Leaf) {
   if (OI.Rec == Leaf)
     return true;
 
@@ -3746,7 +3735,7 @@ static bool checkOperandClass(CGIOperandList::OperandInfo &OI, Record *Leaf) {
 }
 
 void CodeGenDAGPatterns::parseInstructionPattern(CodeGenInstruction &CGI,
-                                                 ListInit *Pat,
+                                                 const ListInit *Pat,
                   ...
[truncated]

@jurahul jurahul merged commit 3138eb5 into llvm:main Sep 23, 2024
14 checks passed
@jurahul jurahul deleted the const_record_tg_common branch September 23, 2024 20:07
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