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 17 using 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'. 30 enum RankFlags { 31 RF_Z_EXTENSION = 1 << 6, 32 RF_S_EXTENSION = 1 << 7, 33 RF_X_EXTENSION = 1 << 8, 34 }; 35 36 // Get the rank for single-letter extension, lower value meaning higher 37 // priority. 38 static 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. 58 static 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. 78 bool 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 } 90