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