1//===-- RISCVISAUtils.cpp - RISC-V ISA Utilities --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Utilities shared by TableGen and RISCVISAInfo.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/RISCVISAUtils.h"
14#include "llvm/ADT/StringExtras.h"
15#include <cassert>
16
17using namespace llvm;
18
19// We rank extensions in the following order:
20// -Single letter extensions in canonical order.
21// -Unknown single letter extensions in alphabetical order.
22// -Multi-letter extensions starting with 'z' sorted by canonical order of
23// the second letter then sorted alphabetically.
24// -Multi-letter extensions starting with 's' in alphabetical order.
25// -(TODO) Multi-letter extensions starting with 'zxm' in alphabetical order.
26// -X extensions in alphabetical order.
27// -Unknown multi-letter extensions in alphabetical order.
28// These flags are used to indicate the category. The first 6 bits store the
29// single letter extension rank for single letter and multi-letter extensions
30// starting with 'z'.
31enum RankFlags {
32 RF_BASE_I = 0,
33 RF_BASE_E = 1,
34 RF_BASE_Y = 2,
35 RF_NUM_BASES = 3,
36 RF_Z_EXTENSION = 1 << 6,
37 RF_S_EXTENSION = 2 << 6,
38 RF_X_EXTENSION = 3 << 6,
39 RF_UNKNOWN_MULTILETTER_EXTENSION = 4 << 6,
40};
41
42// Get the rank for single-letter extension, lower value meaning higher
43// priority.
44static unsigned singleLetterExtensionRank(char Ext) {
45 assert(isLower(Ext));
46 switch (Ext) {
47 case 'i':
48 return RF_BASE_I;
49 case 'e':
50 return RF_BASE_E;
51 case 'y':
52 return RF_BASE_Y;
53 }
54
55 size_t Pos = RISCVISAUtils::AllStdExts.find(C: Ext);
56 if (Pos != StringRef::npos)
57 return Pos + RF_NUM_BASES; // Skip 'e', 'i', and 'y' from above.
58
59 // If we got an unknown extension letter, then give it an alphabetical
60 // order, but after all known standard extensions.
61 return RF_NUM_BASES + RISCVISAUtils::AllStdExts.size() + (Ext - 'a');
62}
63
64// Get the rank for multi-letter extension, lower value meaning higher
65// priority/order in canonical order.
66static unsigned getExtensionRank(StringRef ExtName) {
67 assert(ExtName.size() >= 1);
68 switch (ExtName[0]) {
69 case 's':
70 return RF_S_EXTENSION;
71 case 'z':
72 assert(ExtName.size() >= 2);
73 // `z` extension must be sorted by canonical order of second letter.
74 // e.g. zmx has higher rank than zax.
75 return RF_Z_EXTENSION | singleLetterExtensionRank(Ext: ExtName[1]);
76 case 'x':
77 return RF_X_EXTENSION;
78 default:
79 if (ExtName.size() == 1)
80 return singleLetterExtensionRank(Ext: ExtName[0]);
81 return RF_UNKNOWN_MULTILETTER_EXTENSION;
82 }
83}
84
85// Compare function for extension.
86// Only compare the extension name, ignore version comparison.
87bool llvm::RISCVISAUtils::compareExtension(StringRef LHS, StringRef RHS) {
88 unsigned LHSRank = getExtensionRank(ExtName: LHS);
89 unsigned RHSRank = getExtensionRank(ExtName: RHS);
90
91 // If the ranks differ, pick the lower rank.
92 if (LHSRank != RHSRank)
93 return LHSRank < RHSRank;
94
95 // If the rank is same, it must be sorted by lexicographic order.
96 return LHS < RHS;
97}
98