@@ -502,6 +502,9 @@ struct MatchableInfo {
502
502
// / matchable came from.
503
503
Record *const TheDef;
504
504
505
+ // ResInstSize - The size of the resulting instruction for this matchable.
506
+ unsigned ResInstSize;
507
+
505
508
// / DefRec - This is the definition that it came from.
506
509
PointerUnion<const CodeGenInstruction *, const CodeGenInstAlias *> DefRec;
507
510
@@ -543,10 +546,12 @@ struct MatchableInfo {
543
546
544
547
MatchableInfo (const CodeGenInstruction &CGI)
545
548
: AsmVariantID(0 ), AsmString(CGI.AsmString), TheDef(CGI.TheDef),
546
- DefRec (&CGI), UseInstAsmMatchConverter(true ) {}
549
+ ResInstSize (TheDef->getValueAsInt (" Size" )), DefRec(&CGI),
550
+ UseInstAsmMatchConverter(true ) {}
547
551
548
552
MatchableInfo (std::unique_ptr<const CodeGenInstAlias> Alias)
549
553
: AsmVariantID(0 ), AsmString(Alias->AsmString), TheDef(Alias->TheDef),
554
+ ResInstSize(Alias->ResultInst->TheDef->getValueAsInt (" Size" )),
550
555
DefRec(Alias.release()), UseInstAsmMatchConverter(TheDef->getValueAsBit (
551
556
" UseInstAsmMatchConverter" )) {}
552
557
@@ -608,12 +613,17 @@ struct MatchableInfo {
608
613
void buildInstructionResultOperands ();
609
614
void buildAliasResultOperands (bool AliasConstraintsAreChecked);
610
615
611
- // / operator< - Compare two matchables.
612
- bool operator < (const MatchableInfo &RHS) const {
616
+ // / shouldBeMatchedBefore - Compare two matchables for ordering .
617
+ bool shouldBeMatchedBefore (const MatchableInfo &RHS, const CodeGenTarget &Target ) const {
613
618
// The primary comparator is the instruction mnemonic.
614
619
if (int Cmp = Mnemonic.compare_insensitive (RHS.Mnemonic ))
615
620
return Cmp == -1 ;
616
621
622
+ // Sort by the resultant instuctions size, eg. for ARM instructions
623
+ // we must choose the smallest matching instruction.
624
+ if (Target.getPreferSmallerInstructions () && ResInstSize != RHS.ResInstSize )
625
+ return ResInstSize < RHS.ResInstSize ;
626
+
617
627
if (AsmOperands.size () != RHS.AsmOperands .size ())
618
628
return AsmOperands.size () < RHS.AsmOperands .size ();
619
629
@@ -652,7 +662,8 @@ struct MatchableInfo {
652
662
// / couldMatchAmbiguouslyWith - Check whether this matchable could
653
663
// / ambiguously match the same set of operands as \p RHS (without being a
654
664
// / strictly superior match).
655
- bool couldMatchAmbiguouslyWith (const MatchableInfo &RHS) const {
665
+ bool couldMatchAmbiguouslyWith (const MatchableInfo &RHS,
666
+ const CodeGenTarget &Target) const {
656
667
// The primary comparator is the instruction mnemonic.
657
668
if (Mnemonic != RHS.Mnemonic )
658
669
return false ;
@@ -661,6 +672,11 @@ struct MatchableInfo {
661
672
if (AsmVariantID != RHS.AsmVariantID )
662
673
return false ;
663
674
675
+ // Sort by the resultant instuctions size, eg. for ARM instructions
676
+ // we must choose the smallest matching instruction.
677
+ if (Target.getPreferSmallerInstructions () && ResInstSize != RHS.ResInstSize )
678
+ return false ;
679
+
664
680
// The number of operands is unambiguous.
665
681
if (AsmOperands.size () != RHS.AsmOperands .size ())
666
682
return false ;
@@ -3224,17 +3240,18 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
3224
3240
// Sort the instruction table using the partial order on classes. We use
3225
3241
// stable_sort to ensure that ambiguous instructions are still
3226
3242
// deterministically ordered.
3227
- llvm::stable_sort (
3228
- Info.Matchables ,
3229
- [](const std::unique_ptr<MatchableInfo> &a,
3230
- const std::unique_ptr<MatchableInfo> &b) { return *a < *b; });
3243
+ llvm::stable_sort (Info.Matchables ,
3244
+ [&Target](const std::unique_ptr<MatchableInfo> &a,
3245
+ const std::unique_ptr<MatchableInfo> &b) {
3246
+ return a->shouldBeMatchedBefore (*b, Target);
3247
+ });
3231
3248
3232
3249
#ifdef EXPENSIVE_CHECKS
3233
3250
// Verify that the table is sorted and operator < works transitively.
3234
3251
for (auto I = Info.Matchables .begin (), E = Info.Matchables .end (); I != E;
3235
3252
++I) {
3236
3253
for (auto J = I; J != E; ++J) {
3237
- assert (!(**J < **I));
3254
+ assert (!((*J)-> shouldBeMatchedBefore ( **I, Target) ));
3238
3255
}
3239
3256
}
3240
3257
#endif
@@ -3253,7 +3270,7 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
3253
3270
const MatchableInfo &A = **I;
3254
3271
const MatchableInfo &B = **J;
3255
3272
3256
- if (A.couldMatchAmbiguouslyWith (B)) {
3273
+ if (A.couldMatchAmbiguouslyWith (B, Target )) {
3257
3274
errs () << " warning: ambiguous matchables:\n " ;
3258
3275
A.dump ();
3259
3276
errs () << " \n is incomparable with:\n " ;
0 commit comments