@@ -1037,7 +1037,83 @@ Init *BinOpInit::getListConcat(TypedInit *LHS, Init *RHS) {
10371037 return BinOpInit::get (BinOpInit::LISTCONCAT, LHS, RHS, LHS->getType ());
10381038}
10391039
1040- Init *BinOpInit::Fold (Record *CurRec) const {
1040+ std::optional<bool > BinOpInit::CompareInit (unsigned Opc, Init *LHS, Init *RHS) const {
1041+ // First see if we have two bit, bits, or int.
1042+ IntInit *LHSi = dyn_cast_or_null<IntInit>(
1043+ LHS->convertInitializerTo (IntRecTy::get (getRecordKeeper ())));
1044+ IntInit *RHSi = dyn_cast_or_null<IntInit>(
1045+ RHS->convertInitializerTo (IntRecTy::get (getRecordKeeper ())));
1046+
1047+ if (LHSi && RHSi) {
1048+ bool Result;
1049+ switch (Opc) {
1050+ case EQ:
1051+ Result = LHSi->getValue () == RHSi->getValue ();
1052+ break ;
1053+ case NE:
1054+ Result = LHSi->getValue () != RHSi->getValue ();
1055+ break ;
1056+ case LE:
1057+ Result = LHSi->getValue () <= RHSi->getValue ();
1058+ break ;
1059+ case LT:
1060+ Result = LHSi->getValue () < RHSi->getValue ();
1061+ break ;
1062+ case GE:
1063+ Result = LHSi->getValue () >= RHSi->getValue ();
1064+ break ;
1065+ case GT:
1066+ Result = LHSi->getValue () > RHSi->getValue ();
1067+ break ;
1068+ default :
1069+ llvm_unreachable (" unhandled comparison" );
1070+ }
1071+ return Result;
1072+ }
1073+
1074+ // Next try strings.
1075+ StringInit *LHSs = dyn_cast<StringInit>(LHS);
1076+ StringInit *RHSs = dyn_cast<StringInit>(RHS);
1077+
1078+ if (LHSs && RHSs) {
1079+ bool Result;
1080+ switch (Opc) {
1081+ case EQ:
1082+ Result = LHSs->getValue () == RHSs->getValue ();
1083+ break ;
1084+ case NE:
1085+ Result = LHSs->getValue () != RHSs->getValue ();
1086+ break ;
1087+ case LE:
1088+ Result = LHSs->getValue () <= RHSs->getValue ();
1089+ break ;
1090+ case LT:
1091+ Result = LHSs->getValue () < RHSs->getValue ();
1092+ break ;
1093+ case GE:
1094+ Result = LHSs->getValue () >= RHSs->getValue ();
1095+ break ;
1096+ case GT:
1097+ Result = LHSs->getValue () > RHSs->getValue ();
1098+ break ;
1099+ default :
1100+ llvm_unreachable (" unhandled comparison" );
1101+ }
1102+ return Result;
1103+ }
1104+
1105+ // Finally, !eq and !ne can be used with records.
1106+ if (Opc == EQ || Opc == NE) {
1107+ DefInit *LHSd = dyn_cast<DefInit>(LHS);
1108+ DefInit *RHSd = dyn_cast<DefInit>(RHS);
1109+ if (LHSd && RHSd)
1110+ return (Opc == EQ) ? LHSd == RHSd : LHSd != RHSd;
1111+ }
1112+
1113+ return std::nullopt ;
1114+ }
1115+
1116+ Init *BinOpInit::Fold (Record *CurRec) const {
10411117 switch (getOpcode ()) {
10421118 case CONCAT: {
10431119 DagInit *LHSs = dyn_cast<DagInit>(LHS);
@@ -1091,6 +1167,28 @@ Init *BinOpInit::Fold(Record *CurRec) const {
10911167 }
10921168 break ;
10931169 }
1170+ case LISTREMOVE: {
1171+ ListInit *LHSs = dyn_cast<ListInit>(LHS);
1172+ ListInit *RHSs = dyn_cast<ListInit>(RHS);
1173+ if (LHSs && RHSs) {
1174+ SmallVector<Init *, 8 > Args;
1175+ for (Init *EltLHS : *LHSs) {
1176+ bool Found = false ;
1177+ for (Init *EltRHS : *RHSs) {
1178+ if (std::optional<bool > Result = CompareInit (EQ, EltLHS, EltRHS)) {
1179+ if (*Result) {
1180+ Found = true ;
1181+ break ;
1182+ }
1183+ }
1184+ }
1185+ if (!Found)
1186+ Args.push_back (EltLHS);
1187+ }
1188+ return ListInit::get (Args, LHSs->getElementType ());
1189+ }
1190+ break ;
1191+ }
10941192 case STRCONCAT: {
10951193 StringInit *LHSs = dyn_cast<StringInit>(LHS);
10961194 StringInit *RHSs = dyn_cast<StringInit>(RHS);
@@ -1118,53 +1216,8 @@ Init *BinOpInit::Fold(Record *CurRec) const {
11181216 case LT:
11191217 case GE:
11201218 case GT: {
1121- // First see if we have two bit, bits, or int.
1122- IntInit *LHSi = dyn_cast_or_null<IntInit>(
1123- LHS->convertInitializerTo (IntRecTy::get (getRecordKeeper ())));
1124- IntInit *RHSi = dyn_cast_or_null<IntInit>(
1125- RHS->convertInitializerTo (IntRecTy::get (getRecordKeeper ())));
1126-
1127- if (LHSi && RHSi) {
1128- bool Result;
1129- switch (getOpcode ()) {
1130- case EQ: Result = LHSi->getValue () == RHSi->getValue (); break ;
1131- case NE: Result = LHSi->getValue () != RHSi->getValue (); break ;
1132- case LE: Result = LHSi->getValue () <= RHSi->getValue (); break ;
1133- case LT: Result = LHSi->getValue () < RHSi->getValue (); break ;
1134- case GE: Result = LHSi->getValue () >= RHSi->getValue (); break ;
1135- case GT: Result = LHSi->getValue () > RHSi->getValue (); break ;
1136- default : llvm_unreachable (" unhandled comparison" );
1137- }
1138- return BitInit::get (getRecordKeeper (), Result);
1139- }
1140-
1141- // Next try strings.
1142- StringInit *LHSs = dyn_cast<StringInit>(LHS);
1143- StringInit *RHSs = dyn_cast<StringInit>(RHS);
1144-
1145- if (LHSs && RHSs) {
1146- bool Result;
1147- switch (getOpcode ()) {
1148- case EQ: Result = LHSs->getValue () == RHSs->getValue (); break ;
1149- case NE: Result = LHSs->getValue () != RHSs->getValue (); break ;
1150- case LE: Result = LHSs->getValue () <= RHSs->getValue (); break ;
1151- case LT: Result = LHSs->getValue () < RHSs->getValue (); break ;
1152- case GE: Result = LHSs->getValue () >= RHSs->getValue (); break ;
1153- case GT: Result = LHSs->getValue () > RHSs->getValue (); break ;
1154- default : llvm_unreachable (" unhandled comparison" );
1155- }
1156- return BitInit::get (getRecordKeeper (), Result);
1157- }
1158-
1159- // Finally, !eq and !ne can be used with records.
1160- if (getOpcode () == EQ || getOpcode () == NE) {
1161- DefInit *LHSd = dyn_cast<DefInit>(LHS);
1162- DefInit *RHSd = dyn_cast<DefInit>(RHS);
1163- if (LHSd && RHSd)
1164- return BitInit::get (getRecordKeeper (),
1165- (getOpcode () == EQ) ? LHSd == RHSd : LHSd != RHSd);
1166- }
1167-
1219+ if (std::optional<bool > Result = CompareInit (getOpcode (), LHS, RHS))
1220+ return BitInit::get (getRecordKeeper (), *Result);
11681221 break ;
11691222 }
11701223 case SETDAGOP: {
@@ -1260,6 +1313,7 @@ std::string BinOpInit::getAsString() const {
12601313 case GT: Result = " !gt" ; break ;
12611314 case LISTCONCAT: Result = " !listconcat" ; break ;
12621315 case LISTSPLAT: Result = " !listsplat" ; break ;
1316+ case LISTREMOVE: Result = " !listremove" ; break ;
12631317 case STRCONCAT: Result = " !strconcat" ; break ;
12641318 case INTERLEAVE: Result = " !interleave" ; break ;
12651319 case SETDAGOP: Result = " !setdagop" ; break ;
0 commit comments