[llvm] 4d5a963 - [VE] Rename VariantKind to Specifier and clean up code

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 20 21:04:16 PDT 2025


Author: Fangrui Song
Date: 2025-03-20T21:04:11-07:00
New Revision: 4d5a963eaf6ad209487a321dee7f0cd2a0f98477

URL: https://github.com/llvm/llvm-project/commit/4d5a963eaf6ad209487a321dee7f0cd2a0f98477
DIFF: https://github.com/llvm/llvm-project/commit/4d5a963eaf6ad209487a321dee7f0cd2a0f98477.diff

LOG: [VE] Rename VariantKind to Specifier and clean up code

Added: 
    

Modified: 
    llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
    llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp
    llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp
    llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.h
    llvm/lib/Target/VE/VEAsmPrinter.cpp
    llvm/lib/Target/VE/VEMCInstLower.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp b/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
index 174eb86fe6a56..2b7e1072e7394 100644
--- a/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
+++ b/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
@@ -73,9 +73,7 @@ class VEAsmParser : public MCTargetAsmParser {
   ParseStatus parseVEAsmOperand(std::unique_ptr<VEOperand> &Operand);
 
   // Helper function to parse expression with a symbol.
-  const MCExpr *extractModifierFromExpr(const MCExpr *E,
-                                        VEMCExpr::VariantKind &Variant);
-  const MCExpr *fixupVariantKind(const MCExpr *E);
+  const MCExpr *extractSpecifier(const MCExpr *E, VEMCExpr::Specifier &Variant);
   bool parseExpression(const MCExpr *&EVal);
 
   // Split the mnemonic stripping conditional code and quantifiers
@@ -1035,15 +1033,14 @@ bool VEAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
   return (parseMany(parseOne));
 }
 
-/// Extract \code @lo32/@hi32/etc \endcode modifier from expression.
+/// Extract \code @lo32/@hi32/etc \endcode specifier from expression.
 /// Recursively scan the expression and check for VK_HI32/LO32/etc
 /// symbol variants.  If all symbols with modifier use the same
-/// variant, return the corresponding VEMCExpr::VariantKind,
+/// variant, return the corresponding VEMCExpr::Specifier,
 /// and a modified expression using the default symbol variant.
 /// Otherwise, return NULL.
-const MCExpr *
-VEAsmParser::extractModifierFromExpr(const MCExpr *E,
-                                     VEMCExpr::VariantKind &Variant) {
+const MCExpr *VEAsmParser::extractSpecifier(const MCExpr *E,
+                                            VEMCExpr::Specifier &Variant) {
   MCContext &Context = getParser().getContext();
   Variant = VEMCExpr::VK_None;
 
@@ -1055,7 +1052,7 @@ VEAsmParser::extractModifierFromExpr(const MCExpr *E,
   case MCExpr::SymbolRef: {
     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
 
-    switch (getVariantKind(SRE)) {
+    switch (getSpecifier(SRE)) {
     case VEMCExpr::VK_None:
       // Use VK_REFLONG to a symbol without modifiers.
       Variant = VEMCExpr::VK_REFLONG;
@@ -1111,7 +1108,7 @@ VEAsmParser::extractModifierFromExpr(const MCExpr *E,
 
   case MCExpr::Unary: {
     const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
-    const MCExpr *Sub = extractModifierFromExpr(UE->getSubExpr(), Variant);
+    const MCExpr *Sub = extractSpecifier(UE->getSubExpr(), Variant);
     if (!Sub)
       return nullptr;
     return MCUnaryExpr::create(UE->getOpcode(), Sub, Context);
@@ -1119,9 +1116,9 @@ VEAsmParser::extractModifierFromExpr(const MCExpr *E,
 
   case MCExpr::Binary: {
     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
-    VEMCExpr::VariantKind LHSVariant, RHSVariant;
-    const MCExpr *LHS = extractModifierFromExpr(BE->getLHS(), LHSVariant);
-    const MCExpr *RHS = extractModifierFromExpr(BE->getRHS(), RHSVariant);
+    VEMCExpr::Specifier LHSVariant, RHSVariant;
+    const MCExpr *LHS = extractSpecifier(BE->getLHS(), LHSVariant);
+    const MCExpr *RHS = extractSpecifier(BE->getRHS(), RHSVariant);
 
     if (!LHS && !RHS)
       return nullptr;
@@ -1147,49 +1144,18 @@ VEAsmParser::extractModifierFromExpr(const MCExpr *E,
   llvm_unreachable("Invalid expression kind!");
 }
 
-const MCExpr *VEAsmParser::fixupVariantKind(const MCExpr *E) {
-  MCContext &Context = getParser().getContext();
-
-  switch (E->getKind()) {
-  case MCExpr::Target:
-  case MCExpr::Constant:
-  case MCExpr::SymbolRef:
-    return E;
-
-  case MCExpr::Unary: {
-    const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
-    const MCExpr *Sub = fixupVariantKind(UE->getSubExpr());
-    if (Sub == UE->getSubExpr())
-      return E;
-    return MCUnaryExpr::create(UE->getOpcode(), Sub, Context);
-  }
-
-  case MCExpr::Binary: {
-    const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
-    const MCExpr *LHS = fixupVariantKind(BE->getLHS());
-    const MCExpr *RHS = fixupVariantKind(BE->getRHS());
-    if (LHS == BE->getLHS() && RHS == BE->getRHS())
-      return E;
-    return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, Context);
-  }
-  }
-
-  llvm_unreachable("Invalid expression kind!");
-}
-
-/// ParseExpression.  This 
diff ers from the default "parseExpression" in that
-/// it handles modifiers.
+/// This 
diff ers from the default "parseExpression" in that it handles
+/// relocation specifiers.
 bool VEAsmParser::parseExpression(const MCExpr *&EVal) {
   // Handle \code symbol @lo32/@hi32/etc \endcode.
   if (getParser().parseExpression(EVal))
     return true;
 
   // Convert MCSymbolRefExpr with VK_* to MCExpr with VK_*.
-  EVal = fixupVariantKind(EVal);
-  VEMCExpr::VariantKind Variant;
-  const MCExpr *E = extractModifierFromExpr(EVal, Variant);
+  VEMCExpr::Specifier Specifier;
+  const MCExpr *E = extractSpecifier(EVal, Specifier);
   if (E)
-    EVal = VEMCExpr::create(Variant, E, getParser().getContext());
+    EVal = VEMCExpr::create(Specifier, E, getParser().getContext());
 
   return false;
 }

diff  --git a/llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp
index 24149de7c33fd..f49985cad7aac 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp
@@ -40,7 +40,7 @@ unsigned VEELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
                                          const MCFixup &Fixup,
                                          bool IsPCRel) const {
   if (const VEMCExpr *SExpr = dyn_cast<VEMCExpr>(Fixup.getValue())) {
-    if (SExpr->getKind() == VEMCExpr::VK_PC_LO32)
+    if (SExpr->getSpecifier() == VEMCExpr::VK_PC_LO32)
       return ELF::R_VE_PC_LO32;
   }
 

diff  --git a/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp
index f91cbdff4ecbf..429e3a9f0e83f 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp
@@ -25,73 +25,23 @@ using namespace llvm;
 
 #define DEBUG_TYPE "vemcexpr"
 
-const VEMCExpr *VEMCExpr::create(VariantKind Kind, const MCExpr *Expr,
+const VEMCExpr *VEMCExpr::create(Specifier Kind, const MCExpr *Expr,
                                  MCContext &Ctx) {
   return new (Ctx) VEMCExpr(Kind, Expr);
 }
 
 void VEMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
 
-  bool closeParen = printVariantKind(OS, Kind);
-
   const MCExpr *Expr = getSubExpr();
   Expr->print(OS, MAI);
-
-  if (closeParen)
-    OS << ')';
   if (Kind != VK_None && Kind != VK_REFLONG)
     OS << '@' << MAI->getVariantKindName(Kind);
 }
 
-bool VEMCExpr::printVariantKind(raw_ostream &OS, VariantKind Kind) {
-  switch (Kind) {
-  case VK_None:
-  case VK_REFLONG:
-    return false;
-
-  case VK_HI32:
-  case VK_LO32:
-  case VK_PC_HI32:
-  case VK_PC_LO32:
-  case VK_GOT_HI32:
-  case VK_GOT_LO32:
-  case VK_GOTOFF_HI32:
-  case VK_GOTOFF_LO32:
-  case VK_PLT_HI32:
-  case VK_PLT_LO32:
-  case VK_TLS_GD_HI32:
-  case VK_TLS_GD_LO32:
-  case VK_TPOFF_HI32:
-  case VK_TPOFF_LO32:
-    // Use suffix for these variant kinds
-    return false;
-  }
-  return true;
-}
-
-VEMCExpr::VariantKind VEMCExpr::parseVariantKind(StringRef name) {
-  return StringSwitch<VEMCExpr::VariantKind>(name)
-      .Case("hi", VK_HI32)
-      .Case("lo", VK_LO32)
-      .Case("pc_hi", VK_PC_HI32)
-      .Case("pc_lo", VK_PC_LO32)
-      .Case("got_hi", VK_GOT_HI32)
-      .Case("got_lo", VK_GOT_LO32)
-      .Case("gotoff_hi", VK_GOTOFF_HI32)
-      .Case("gotoff_lo", VK_GOTOFF_LO32)
-      .Case("plt_hi", VK_PLT_HI32)
-      .Case("plt_lo", VK_PLT_LO32)
-      .Case("tls_gd_hi", VK_TLS_GD_HI32)
-      .Case("tls_gd_lo", VK_TLS_GD_LO32)
-      .Case("tpoff_hi", VK_TPOFF_HI32)
-      .Case("tpoff_lo", VK_TPOFF_LO32)
-      .Default(VK_None);
-}
-
-VE::Fixups VEMCExpr::getFixupKind(VEMCExpr::VariantKind Kind) {
-  switch (Kind) {
+VE::Fixups VEMCExpr::getFixupKind(VEMCExpr::Specifier S) {
+  switch (S) {
   default:
-    llvm_unreachable("Unhandled VEMCExpr::VariantKind");
+    llvm_unreachable("Unhandled VEMCExpr::Specifier");
   case VK_REFLONG:
     return VE::fixup_ve_reflong;
   case VK_HI32:
@@ -130,8 +80,8 @@ bool VEMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
   if (!getSubExpr()->evaluateAsRelocatable(Res, Asm))
     return false;
 
-  Res =
-      MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
+  Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(),
+                     getSpecifier());
 
   return true;
 }
@@ -171,7 +121,7 @@ void VEMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
 }
 
 void VEMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
-  switch (getKind()) {
+  switch (getSpecifier()) {
   default:
     return;
   case VK_TLS_GD_HI32:

diff  --git a/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.h b/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.h
index 07586778fc362..4826cea8b190b 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.h
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.h
@@ -22,12 +22,10 @@ namespace llvm {
 class StringRef;
 class VEMCExpr : public MCTargetExpr {
 public:
-  enum VariantKind {
+  enum Specifier {
     VK_None,
 
-    // While not strictly necessary, start at a larger number to avoid confusion
-    // with MCSymbolRefExpr::VariantKind.
-    VK_REFLONG = 100,
+    VK_REFLONG = MCSymbolRefExpr::FirstTargetSpecifier,
     VK_HI32,        // @hi
     VK_LO32,        // @lo
     VK_PC_HI32,     // @pc_hi
@@ -45,24 +43,24 @@ class VEMCExpr : public MCTargetExpr {
   };
 
 private:
-  const VariantKind Kind;
+  const Specifier Kind;
   const MCExpr *Expr;
 
-  explicit VEMCExpr(VariantKind Kind, const MCExpr *Expr)
+  explicit VEMCExpr(Specifier Kind, const MCExpr *Expr)
       : Kind(Kind), Expr(Expr) {}
 
 public:
   /// @name Construction
   /// @{
 
-  static const VEMCExpr *create(VariantKind Kind, const MCExpr *Expr,
+  static const VEMCExpr *create(Specifier Kind, const MCExpr *Expr,
                                 MCContext &Ctx);
   /// @}
   /// @name Accessors
   /// @{
 
   /// getOpcode - Get the kind of this expression.
-  VariantKind getKind() const { return Kind; }
+  Specifier getSpecifier() const { return Kind; }
 
   /// getSubExpr - Get the child of this expression.
   const MCExpr *getSubExpr() const { return Expr; }
@@ -85,13 +83,11 @@ class VEMCExpr : public MCTargetExpr {
     return E->getKind() == MCExpr::Target;
   }
 
-  static VariantKind parseVariantKind(StringRef name);
-  static bool printVariantKind(raw_ostream &OS, VariantKind Kind);
-  static VE::Fixups getFixupKind(VariantKind Kind);
+  static VE::Fixups getFixupKind(Specifier S);
 };
 
-static inline VEMCExpr::VariantKind getVariantKind(const MCSymbolRefExpr *SRE) {
-  return VEMCExpr::VariantKind(SRE->getKind());
+static inline VEMCExpr::Specifier getSpecifier(const MCSymbolRefExpr *SRE) {
+  return VEMCExpr::Specifier(SRE->getKind());
 }
 
 } // namespace llvm

diff  --git a/llvm/lib/Target/VE/VEAsmPrinter.cpp b/llvm/lib/Target/VE/VEAsmPrinter.cpp
index 60164cdd16cf6..79d5840d11363 100644
--- a/llvm/lib/Target/VE/VEAsmPrinter.cpp
+++ b/llvm/lib/Target/VE/VEAsmPrinter.cpp
@@ -65,14 +65,14 @@ class VEAsmPrinter : public AsmPrinter {
 };
 } // end of anonymous namespace
 
-static MCOperand createVEMCOperand(VEMCExpr::VariantKind Kind, MCSymbol *Sym,
+static MCOperand createVEMCOperand(VEMCExpr::Specifier Kind, MCSymbol *Sym,
                                    MCContext &OutContext) {
   const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Sym, OutContext);
   const VEMCExpr *expr = VEMCExpr::create(Kind, MCSym, OutContext);
   return MCOperand::createExpr(expr);
 }
 
-static MCOperand createGOTRelExprOp(VEMCExpr::VariantKind Kind,
+static MCOperand createGOTRelExprOp(VEMCExpr::Specifier Kind,
                                     MCSymbol *GOTLabel, MCContext &OutContext) {
   const MCSymbolRefExpr *GOT = MCSymbolRefExpr::create(GOTLabel, OutContext);
   const VEMCExpr *expr = VEMCExpr::create(Kind, GOT, OutContext);
@@ -164,7 +164,7 @@ static void emitANDrm(MCStreamer &OutStreamer, MCOperand &RS1, MCOperand &Imm,
 }
 
 static void emitHiLo(MCStreamer &OutStreamer, MCSymbol *GOTSym,
-                     VEMCExpr::VariantKind HiKind, VEMCExpr::VariantKind LoKind,
+                     VEMCExpr::Specifier HiKind, VEMCExpr::Specifier LoKind,
                      MCOperand &RD, MCContext &OutContext,
                      const MCSubtargetInfo &STI) {
 

diff  --git a/llvm/lib/Target/VE/VEMCInstLower.cpp b/llvm/lib/Target/VE/VEMCInstLower.cpp
index 57195f238cf6e..bed71df3921cf 100644
--- a/llvm/lib/Target/VE/VEMCInstLower.cpp
+++ b/llvm/lib/Target/VE/VEMCInstLower.cpp
@@ -28,7 +28,7 @@ using namespace llvm;
 static MCOperand LowerSymbolOperand(const MachineInstr *MI,
                                     const MachineOperand &MO,
                                     const MCSymbol *Symbol, AsmPrinter &AP) {
-  VEMCExpr::VariantKind Kind = (VEMCExpr::VariantKind)MO.getTargetFlags();
+  VEMCExpr::Specifier Kind = (VEMCExpr::Specifier)MO.getTargetFlags();
 
   const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, AP.OutContext);
   // Add offset iff MO is not jump table info or machine basic block.


        


More information about the llvm-commits mailing list