LLVM 19.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
14#include <cassert>
15
16using namespace llvm;
17
18// We rank extensions in the following order:
19// -Single letter extensions in canonical order.
20// -Unknown single letter extensions in alphabetical order.
21// -Multi-letter extensions starting with 'z' sorted by canonical order of
22// the second letter then sorted alphabetically.
23// -Multi-letter extensions starting with 's' in alphabetical order.
24// -(TODO) Multi-letter extensions starting with 'zxm' in alphabetical order.
25// -X extensions in alphabetical order.
26// These flags are used to indicate the category. The first 6 bits store the
27// single letter extension rank for single letter and multi-letter extensions
28// starting with 'z'.
33};
34
35// Get the rank for single-letter extension, lower value meaning higher
36// priority.
37static unsigned singleLetterExtensionRank(char Ext) {
38 assert(Ext >= 'a' && Ext <= 'z');
39 switch (Ext) {
40 case 'i':
41 return 0;
42 case 'e':
43 return 1;
44 }
45
46 size_t Pos = RISCVISAUtils::AllStdExts.find(Ext);
47 if (Pos != StringRef::npos)
48 return Pos + 2; // Skip 'e' and 'i' from above.
49
50 // If we got an unknown extension letter, then give it an alphabetical
51 // order, but after all known standard extensions.
52 return 2 + RISCVISAUtils::AllStdExts.size() + (Ext - 'a');
53}
54
55// Get the rank for multi-letter extension, lower value meaning higher
56// priority/order in canonical order.
57static unsigned getExtensionRank(const std::string &ExtName) {
58 assert(ExtName.size() >= 1);
59 switch (ExtName[0]) {
60 case 's':
61 return RF_S_EXTENSION;
62 case 'z':
63 assert(ExtName.size() >= 2);
64 // `z` extension must be sorted by canonical order of second letter.
65 // e.g. zmx has higher rank than zax.
66 return RF_Z_EXTENSION | singleLetterExtensionRank(ExtName[1]);
67 case 'x':
68 return RF_X_EXTENSION;
69 default:
70 assert(ExtName.size() == 1);
71 return singleLetterExtensionRank(ExtName[0]);
72 }
73}
74
75// Compare function for extension.
76// Only compare the extension name, ignore version comparison.
77bool llvm::RISCVISAUtils::compareExtension(const std::string &LHS,
78 const std::string &RHS) {
79 unsigned LHSRank = getExtensionRank(LHS);
80 unsigned RHSRank = getExtensionRank(RHS);
81
82 // If the ranks differ, pick the lower rank.
83 if (LHSRank != RHSRank)
84 return LHSRank < RHSRank;
85
86 // If the rank is same, it must be sorted by lexicographic order.
87 return LHS < RHS;
88}
RankFlags
@ RF_S_EXTENSION
@ RF_Z_EXTENSION
@ RF_X_EXTENSION
static unsigned getExtensionRank(const std::string &ExtName)
static unsigned singleLetterExtensionRank(char Ext)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:289
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