@@ -233,6 +233,10 @@ class Pattern {
233
233
234
234
private:
235
235
unsigned Kind;
236
+
237
+ // Note: if this ever changes to a StringRef (e.g. allocated in a pool or
238
+ // something), CombineRuleBuilder::verify() needs to be updated as well.
239
+ // It currently checks that the StringRef in the PatternMap references this.
236
240
std::string Name;
237
241
};
238
242
@@ -454,12 +458,12 @@ struct OperandTableEntry {
454
458
// / - Creation in a `parse` function
455
459
// / - The unique_ptr is stored in a variable, and may be destroyed if the
456
460
// / pattern is found to be semantically invalid.
457
- // / - Ownership transfer into a `PatternStringMap `
461
+ // / - Ownership transfer into a `PatternMap `
458
462
// / - Once a pattern is moved into either the map of Match or Apply
459
463
// / patterns, it is known to be valid and it never moves back.
460
464
class CombineRuleBuilder {
461
465
public:
462
- using PatternStringMap = StringMap< std::unique_ptr<Pattern>>;
466
+ using PatternMap = MapVector<StringRef, std::unique_ptr<Pattern>>;
463
467
464
468
CombineRuleBuilder (const CodeGenTarget &CGT,
465
469
SubtargetFeatureInfoMap &SubtargetFeatures,
@@ -547,8 +551,8 @@ class CombineRuleBuilder {
547
551
548
552
// / These maps have ownership of the actual Pattern objects.
549
553
// / They both map a Pattern's name to the Pattern instance.
550
- PatternStringMap MatchPats;
551
- PatternStringMap ApplyPats;
554
+ PatternMap MatchPats;
555
+ PatternMap ApplyPats;
552
556
553
557
// / Set by findRoots.
554
558
Pattern *MatchRoot = nullptr ;
@@ -614,20 +618,20 @@ void CombineRuleBuilder::print(raw_ostream &OS) const {
614
618
OS << " )\n " ;
615
619
}
616
620
617
- const auto DumpPats = [&](StringRef Name, const PatternStringMap &Pats) {
621
+ const auto DumpPats = [&](StringRef Name, const PatternMap &Pats) {
618
622
OS << " (" << Name << " " ;
619
623
if (Pats.empty ()) {
620
624
OS << " <empty>)\n " ;
621
625
return ;
622
626
}
623
627
624
628
OS << " \n " ;
625
- for (const auto &P : Pats) {
629
+ for (const auto &[Name, Pat] : Pats) {
626
630
OS << " " ;
627
- if (P. getValue () .get () == MatchRoot)
631
+ if (Pat .get () == MatchRoot)
628
632
OS << " <root>" ;
629
- OS << P. getKey () << " :" ;
630
- P. getValue () ->print (OS, /* PrintName=*/ false );
633
+ OS << Name << " :" ;
634
+ Pat ->print (OS, /* PrintName=*/ false );
631
635
OS << " \n " ;
632
636
}
633
637
OS << " )\n " ;
@@ -658,15 +662,27 @@ void CombineRuleBuilder::print(raw_ostream &OS) const {
658
662
}
659
663
660
664
void CombineRuleBuilder::verify () const {
661
- const auto VerifyPats = [&](const PatternStringMap &Pats) {
662
- for (const auto &Entry : Pats) {
663
- if (!Entry. getValue () )
665
+ const auto VerifyPats = [&](const PatternMap &Pats) {
666
+ for (const auto &[Name, Pat] : Pats) {
667
+ if (!Pat )
664
668
PrintFatalError (" null pattern in pattern map!" );
665
669
666
- if (Entry.getKey () != Entry.getValue ()->getName ()) {
667
- Entry.getValue ()->dump ();
668
- PrintFatalError (" Pattern name mismatch! Map name: " + Entry.getKey () +
669
- " , Pat name: " + Entry.getValue ()->getName ());
670
+ if (Name != Pat->getName ()) {
671
+ Pat->dump ();
672
+ PrintFatalError (" Pattern name mismatch! Map name: " + Name +
673
+ " , Pat name: " + Pat->getName ());
674
+ }
675
+
676
+ // As an optimization, the PatternMaps don't re-allocate the PatternName
677
+ // string. They simply reference the std::string inside Pattern. Ensure
678
+ // this is the case to avoid memory issues.
679
+ if (Name.data () != Pat->getName ().data ()) {
680
+ dbgs () << " Map StringRef: '" << Name << " ' @ " << (void *)Name.data ()
681
+ << " \n " ;
682
+ dbgs () << " Pat String: '" << Pat->getName () << " ' @ "
683
+ << (void *)Pat->getName ().data () << " \n " ;
684
+ PrintFatalError (" StringRef stored in the PatternMap is not referencing "
685
+ " the same string as its Pattern!" );
670
686
}
671
687
}
672
688
};
@@ -745,7 +761,7 @@ bool CombineRuleBuilder::findRoots() {
745
761
// Look by pattern name, e.g.
746
762
// (G_FNEG $x, $y):$root
747
763
if (auto It = MatchPats.find (RootName); It != MatchPats.end ()) {
748
- MatchRoot = MatchPats[RootName] .get ();
764
+ MatchRoot = It-> second .get ();
749
765
return true ;
750
766
}
751
767
@@ -769,8 +785,8 @@ bool CombineRuleBuilder::findRoots() {
769
785
770
786
bool CombineRuleBuilder::buildOperandsTable () {
771
787
// Walk each instruction pattern
772
- for (auto &P : MatchPats) {
773
- auto *IP = dyn_cast<InstructionPattern>(P.getValue (). get ());
788
+ for (auto &[_, P] : MatchPats) {
789
+ auto *IP = dyn_cast<InstructionPattern>(P.get ());
774
790
if (!IP)
775
791
continue ;
776
792
for (const auto &Operand : IP->operands ()) {
@@ -790,8 +806,8 @@ bool CombineRuleBuilder::buildOperandsTable() {
790
806
}
791
807
}
792
808
793
- for (auto &P : ApplyPats) {
794
- auto *IP = dyn_cast<InstructionPattern>(P.getValue (). get ());
809
+ for (auto &[_, P] : ApplyPats) {
810
+ auto *IP = dyn_cast<InstructionPattern>(P.get ());
795
811
if (!IP)
796
812
continue ;
797
813
for (const auto &Operand : IP->operands ()) {
@@ -913,7 +929,7 @@ bool CombineRuleBuilder::parseMatch(DagInit &Match) {
913
929
PrintWarning (RuleDef.getLoc (),
914
930
" 'match' C++ code does not seem to return!" );
915
931
}
916
- MatchPats[Name ] = std::move (CXXPat);
932
+ MatchPats[CXXPat-> getName () ] = std::move (CXXPat);
917
933
continue ;
918
934
}
919
935
@@ -940,9 +956,9 @@ bool CombineRuleBuilder::parseApply(DagInit &Apply) {
940
956
}
941
957
942
958
const StringInit *Code = dyn_cast<StringInit>(Apply.getArg (0 ));
943
- const auto PatName = makeAnonPatName (" apply" );
944
- ApplyPats[PatName] =
945
- std::make_unique<CXXPattern>(*Code, PatName, /* IsApply */ true );
959
+ auto Pat = std::make_unique<CXXPattern>(*Code, makeAnonPatName (" apply" ),
960
+ /* IsApply */ true );
961
+ ApplyPats[Pat-> getName ()] = std::move (Pat );
946
962
return true ;
947
963
}
948
964
@@ -1003,20 +1019,19 @@ bool CombineRuleBuilder::emitMatchPattern(CodeExpansions &CE,
1003
1019
return false ;
1004
1020
1005
1021
// Emit remaining patterns
1006
- for (auto &Entry : MatchPats) {
1007
- Pattern *CurPat = Entry.getValue ().get ();
1008
- if (SeenPats.contains (CurPat))
1022
+ for (auto &[_, Pat] : MatchPats) {
1023
+ if (SeenPats.contains (Pat.get ()))
1009
1024
continue ;
1010
1025
1011
- switch (CurPat ->getKind ()) {
1026
+ switch (Pat ->getKind ()) {
1012
1027
case Pattern::K_AnyOpcode:
1013
1028
PrintError (" wip_match_opcode can not be used with instruction patterns!" );
1014
1029
return false ;
1015
1030
case Pattern::K_Inst:
1016
- cast<InstructionPattern>(CurPat )->reportUnreachable (RuleDef.getLoc ());
1031
+ cast<InstructionPattern>(Pat. get () )->reportUnreachable (RuleDef.getLoc ());
1017
1032
return false ;
1018
1033
case Pattern::K_CXX: {
1019
- addCXXPredicate (IM, CE, *cast<CXXPattern>(CurPat ));
1034
+ addCXXPredicate (IM, CE, *cast<CXXPattern>(Pat. get () ));
1020
1035
continue ;
1021
1036
}
1022
1037
default :
@@ -1043,20 +1058,20 @@ bool CombineRuleBuilder::emitMatchPattern(CodeExpansions &CE,
1043
1058
IM.addPredicate <InstructionOpcodeMatcher>(CGI);
1044
1059
1045
1060
// Emit remaining patterns.
1046
- for (auto &Entry : MatchPats) {
1047
- Pattern *CurPat = Entry.getValue ().get ();
1048
- if (CurPat == &AOP)
1061
+ for (auto &[_, Pat] : MatchPats) {
1062
+ if (Pat.get () == &AOP)
1049
1063
continue ;
1050
1064
1051
- switch (CurPat ->getKind ()) {
1065
+ switch (Pat ->getKind ()) {
1052
1066
case Pattern::K_AnyOpcode:
1053
1067
PrintError (" wip_match_opcode can only be present once!" );
1054
1068
return false ;
1055
1069
case Pattern::K_Inst:
1056
- cast<InstructionPattern>(CurPat)->reportUnreachable (RuleDef.getLoc ());
1070
+ cast<InstructionPattern>(Pat.get ())->reportUnreachable (
1071
+ RuleDef.getLoc ());
1057
1072
return false ;
1058
1073
case Pattern::K_CXX: {
1059
- addCXXPredicate (IM, CE, *cast<CXXPattern>(CurPat ));
1074
+ addCXXPredicate (IM, CE, *cast<CXXPattern>(Pat. get () ));
1060
1075
break ;
1061
1076
}
1062
1077
default :
@@ -1072,14 +1087,13 @@ bool CombineRuleBuilder::emitMatchPattern(CodeExpansions &CE,
1072
1087
}
1073
1088
1074
1089
bool CombineRuleBuilder::emitApplyPatterns (CodeExpansions &CE, RuleMatcher &M) {
1075
- for (auto &Entry : ApplyPats) {
1076
- Pattern *CurPat = Entry.getValue ().get ();
1077
- switch (CurPat->getKind ()) {
1090
+ for (auto &[_, Pat] : ApplyPats) {
1091
+ switch (Pat->getKind ()) {
1078
1092
case Pattern::K_AnyOpcode:
1079
1093
case Pattern::K_Inst:
1080
1094
llvm_unreachable (" Unsupported pattern kind in output pattern!" );
1081
1095
case Pattern::K_CXX: {
1082
- CXXPattern *CXXPat = cast<CXXPattern>(CurPat );
1096
+ CXXPattern *CXXPat = cast<CXXPattern>(Pat. get () );
1083
1097
const auto &ExpandedCode = CXXPat->expandCode (CE, RuleDef.getLoc ());
1084
1098
M.addAction <CustomCXXAction>(
1085
1099
ExpandedCode.getEnumNameWithPrefix (CXXApplyPrefix));
0 commit comments