LLVM 20.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'.
36};
37
38// Get the rank for single-letter extension, lower value meaning higher
39// priority.
40static unsigned singleLetterExtensionRank(char Ext) {
41 assert(isLower(Ext));
42 switch (Ext) {
43 case 'i':
44 return 0;
45 case 'e':
46 return 1;
47 }
48
49 size_t Pos = RISCVISAUtils::AllStdExts.find(Ext);
50 if (Pos != StringRef::npos)
51 return Pos + 2; // Skip 'e' and 'i' from above.
52
53 // If we got an unknown extension letter, then give it an alphabetical
54 // order, but after all known standard extensions.
55 return 2 + RISCVISAUtils::AllStdExts.size() + (Ext - 'a');
56}
57
58// Get the rank for multi-letter extension, lower value meaning higher
59// priority/order in canonical order.
60static unsigned getExtensionRank(const std::string &ExtName) {
61 assert(ExtName.size() >= 1);
62 switch (ExtName[0]) {
63 case 's':
64 return RF_S_EXTENSION;
65 case 'z':
66 assert(ExtName.size() >= 2);
67 // `z` extension must be sorted by canonical order of second letter.
68 // e.g. zmx has higher rank than zax.
69 return RF_Z_EXTENSION | singleLetterExtensionRank(ExtName[1]);
70 case 'x':
71 return RF_X_EXTENSION;
72 default:
73 if (ExtName.size() == 1)
74 return singleLetterExtensionRank(ExtName[0]);
76 }
77}
78
79// Compare function for extension.
80// Only compare the extension name, ignore version comparison.
81bool llvm::RISCVISAUtils::compareExtension(const std::string &LHS,
82 const std::string &RHS) {
83 unsigned LHSRank = getExtensionRank(LHS);
84 unsigned RHSRank = getExtensionRank(RHS);
85
86 // If the ranks differ, pick the lower rank.
87 if (LHSRank != RHSRank)
88 return LHSRank < RHSRank;
89
90 // If the rank is same, it must be sorted by lexicographic order.
91 return LHS < RHS;
92}
RankFlags
@ RF_S_EXTENSION
@ RF_Z_EXTENSION
@ RF_UNKNOWN_MULTILETTER_EXTENSION
@ RF_X_EXTENSION
static unsigned getExtensionRank(const std::string &ExtName)
static unsigned singleLetterExtensionRank(char Ext)
static bool isLower(const char C)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some functions that are useful when dealing with strings.
Value * RHS
Value * LHS
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:282
static constexpr size_t npos
Definition: StringRef.h:52
constexpr StringLiteral AllStdExts
Definition: RISCVISAUtils.h:23
bool compareExtension(const std::string &LHS, const std::string &RHS)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18