LLVM 24.0.0git
RISCVISAUtils.cpp
Go to the documentation of this file.
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
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'.
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(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(const std::string &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(ExtName[1]);
76 case 'x':
77 return RF_X_EXTENSION;
78 default:
79 if (ExtName.size() == 1)
80 return singleLetterExtensionRank(ExtName[0]);
82 }
83}
84
85// Compare function for extension.
86// Only compare the extension name, ignore version comparison.
87bool llvm::RISCVISAUtils::compareExtension(const std::string &LHS,
88 const std::string &RHS) {
89 unsigned LHSRank = getExtensionRank(LHS);
90 unsigned RHSRank = getExtensionRank(RHS);
91
92 // If the ranks differ, pick the lower rank.
93 if (LHSRank != RHSRank)
94 return LHSRank < RHSRank;
95
96 // If the rank is same, it must be sorted by lexicographic order.
97 return LHS < RHS;
98}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
RankFlags
@ RF_BASE_I
@ RF_S_EXTENSION
@ RF_Z_EXTENSION
@ RF_UNKNOWN_MULTILETTER_EXTENSION
@ RF_NUM_BASES
@ RF_X_EXTENSION
@ RF_BASE_E
@ RF_BASE_Y
static unsigned getExtensionRank(const std::string &ExtName)
static unsigned singleLetterExtensionRank(char Ext)
This file contains some functions that are useful when dealing with strings.
static constexpr size_t npos
Definition StringRef.h:58
constexpr StringLiteral AllStdExts
LLVM_ABI bool compareExtension(const std::string &LHS, const std::string &RHS)
This is an optimization pass for GlobalISel generic memory operations.
bool isLower(char C)
Checks if character C is a lowercase letter as classified by "C" locale.