@@ -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,18 @@ 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,
618
+ const CodeGenTarget &Target) const {
613
619
// The primary comparator is the instruction mnemonic.
614
620
if (int Cmp = Mnemonic.compare_insensitive (RHS.Mnemonic ))
615
621
return Cmp == -1 ;
616
622
623
+ // Sort by the resultant instuctions size, eg. for ARM instructions
624
+ // we must choose the smallest matching instruction.
625
+ if (Target.getPreferSmallerInstructions () && ResInstSize != RHS.ResInstSize )
626
+ return ResInstSize < RHS.ResInstSize ;
627
+
617
628
if (AsmOperands.size () != RHS.AsmOperands .size ())
618
629
return AsmOperands.size () < RHS.AsmOperands .size ();
619
630
@@ -652,7 +663,8 @@ struct MatchableInfo {
652
663
// / couldMatchAmbiguouslyWith - Check whether this matchable could
653
664
// / ambiguously match the same set of operands as \p RHS (without being a
654
665
// / strictly superior match).
655
- bool couldMatchAmbiguouslyWith (const MatchableInfo &RHS) const {
666
+ bool couldMatchAmbiguouslyWith (const MatchableInfo &RHS,
667
+ const CodeGenTarget &Target) const {
656
668
// The primary comparator is the instruction mnemonic.
657
669
if (Mnemonic != RHS.Mnemonic )
658
670
return false ;
@@ -661,6 +673,11 @@ struct MatchableInfo {
661
673
if (AsmVariantID != RHS.AsmVariantID )
662
674
return false ;
663
675
676
+ // Sort by the resultant instuctions size, eg. for ARM instructions
677
+ // we must choose the smallest matching instruction.
678
+ if (Target.getPreferSmallerInstructions () && ResInstSize != RHS.ResInstSize )
679
+ return false ;
680
+
664
681
// The number of operands is unambiguous.
665
682
if (AsmOperands.size () != RHS.AsmOperands .size ())
666
683
return false ;
@@ -3224,17 +3241,18 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
3224
3241
// Sort the instruction table using the partial order on classes. We use
3225
3242
// stable_sort to ensure that ambiguous instructions are still
3226
3243
// 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; });
3244
+ llvm::stable_sort (Info.Matchables ,
3245
+ [&Target](const std::unique_ptr<MatchableInfo> &a,
3246
+ const std::unique_ptr<MatchableInfo> &b) {
3247
+ return a->shouldBeMatchedBefore (*b, Target);
3248
+ });
3231
3249
3232
3250
#ifdef EXPENSIVE_CHECKS
3233
3251
// Verify that the table is sorted and operator < works transitively.
3234
3252
for (auto I = Info.Matchables .begin (), E = Info.Matchables .end (); I != E;
3235
3253
++I) {
3236
3254
for (auto J = I; J != E; ++J) {
3237
- assert (!(**J < **I));
3255
+ assert (!((*J)-> shouldBeMatchedBefore ( **I, Target) ));
3238
3256
}
3239
3257
}
3240
3258
#endif
@@ -3253,7 +3271,7 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
3253
3271
const MatchableInfo &A = **I;
3254
3272
const MatchableInfo &B = **J;
3255
3273
3256
- if (A.couldMatchAmbiguouslyWith (B)) {
3274
+ if (A.couldMatchAmbiguouslyWith (B, Target )) {
3257
3275
errs () << " warning: ambiguous matchables:\n " ;
3258
3276
A.dump ();
3259
3277
errs () << " \n is incomparable with:\n " ;
0 commit comments