[llvm] RISCVAsmParser: Don't treat operands with relocation specifier as parse-time constants (PR #133377)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 28 11:39:44 PDT 2025


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/133377

>From 028d669a74d0ddf5baaa9a02647da876643c39be Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 27 Mar 2025 23:30:49 -0700
Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.5-bogner
---
 .../Target/RISCV/AsmParser/RISCVAsmParser.cpp | 247 ++++++------------
 .../Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp |   1 +
 llvm/test/MC/RISCV/insn.s                     |   8 +-
 llvm/test/MC/RISCV/rv32i-aliases-valid.s      |   5 +-
 llvm/test/MC/RISCV/rvd-valid.s                |   8 +-
 llvm/test/MC/RISCV/rvf-valid.s                |   4 +-
 llvm/test/MC/RISCV/rvi-valid.s                |  28 +-
 llvm/test/MC/RISCV/rvzfh-valid.s              |   5 +-
 8 files changed, 120 insertions(+), 186 deletions(-)

diff --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
index 3c225fb38cb19..a6882fb86ecc7 100644
--- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
+++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
@@ -524,15 +524,8 @@ struct RISCVOperand final : public MCParsedAsmOperand {
   bool isGPRAsFPR32() const { return isGPRF32() && Reg.IsGPRAsFPR; }
   bool isGPRPairAsFPR64() const { return isGPRPair() && Reg.IsGPRAsFPR; }
 
-  static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
-                                  RISCVMCExpr::Specifier &VK) {
-    if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
-      VK = RE->getSpecifier();
-      return RE->evaluateAsConstant(Imm);
-    }
-
+  static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm) {
     if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
-      VK = RISCVMCExpr::VK_None;
       Imm = CE->getValue();
       return true;
     }
@@ -547,7 +540,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     bool IsValid;
     if (!IsConstantImm)
       IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK);
@@ -562,7 +555,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     int64_t Imm;
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     // Must be of 'immediate' type but not a constant.
-    if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
+    if (!isImm() || evaluateConstantImm(getImm(), Imm))
       return false;
     return RISCVAsmParser::classifySymbolRef(getImm(), VK) &&
            VK == RISCVMCExpr::VK_None;
@@ -572,7 +565,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     int64_t Imm;
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     // Must be of 'immediate' type but not a constant.
-    if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
+    if (!isImm() || evaluateConstantImm(getImm(), Imm))
       return false;
     return RISCVAsmParser::classifySymbolRef(getImm(), VK) &&
            (VK == RISCVMCExpr::VK_CALL || VK == RISCVMCExpr::VK_CALL_PLT);
@@ -582,7 +575,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     int64_t Imm;
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     // Must be of 'immediate' type but not a constant.
-    if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
+    if (!isImm() || evaluateConstantImm(getImm(), Imm))
       return false;
     return RISCVAsmParser::classifySymbolRef(getImm(), VK) &&
            VK == RISCVMCExpr::VK_CALL;
@@ -592,7 +585,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     int64_t Imm;
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     // Must be of 'immediate' type but not a constant.
-    if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
+    if (!isImm() || evaluateConstantImm(getImm(), Imm))
       return false;
     return RISCVAsmParser::classifySymbolRef(getImm(), VK) &&
            VK == RISCVMCExpr::VK_TPREL_ADD;
@@ -602,7 +595,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     int64_t Imm;
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     // Must be of 'immediate' type but not a constant.
-    if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
+    if (!isImm() || evaluateConstantImm(getImm(), Imm))
       return false;
     return RISCVAsmParser::classifySymbolRef(getImm(), VK) &&
            VK == RISCVMCExpr::VK_TLSDESC_CALL;
@@ -612,11 +605,10 @@ struct RISCVOperand final : public MCParsedAsmOperand {
 
   bool isVTypeImm(unsigned N) const {
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isUIntN(N, Imm) && VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isUIntN(N, Imm);
   }
 
   // If the last operand of the vsetvli/vsetvli instruction is a constant
@@ -659,7 +651,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     if (VK == RISCVMCExpr::VK_LO || VK == RISCVMCExpr::VK_PCREL_LO ||
         VK == RISCVMCExpr::VK_TLSDESC_LOAD_LO ||
         VK == RISCVMCExpr::VK_TLSDESC_ADD_LO)
@@ -676,12 +668,11 @@ struct RISCVOperand final : public MCParsedAsmOperand {
 
   bool isImmXLenLI_Restricted() const {
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     // 'la imm' supports constant immediates only.
-    return IsConstantImm && (VK == RISCVMCExpr::VK_None) &&
+    return IsConstantImm &&
            (isRV64Imm() || (isInt<32>(Imm) || isUInt<32>(Imm)));
   }
 
@@ -690,7 +681,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    if (!evaluateConstantImm(getImm(), Imm, VK) || VK != RISCVMCExpr::VK_None)
+    if (!evaluateConstantImm(getImm(), Imm) || VK != RISCVMCExpr::VK_None)
       return false;
     return (isRV64Imm() && isUInt<6>(Imm)) || isUInt<5>(Imm);
   }
@@ -700,7 +691,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    if (!evaluateConstantImm(getImm(), Imm, VK) || VK != RISCVMCExpr::VK_None)
+    if (!evaluateConstantImm(getImm(), Imm) || VK != RISCVMCExpr::VK_None)
       return false;
     if (Imm == 0)
       return false;
@@ -712,18 +703,17 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    if (!evaluateConstantImm(getImm(), Imm, VK) || VK != RISCVMCExpr::VK_None)
+    if (!evaluateConstantImm(getImm(), Imm) || VK != RISCVMCExpr::VK_None)
       return false;
     return (isRV64Imm() && isUInt<5>(Imm)) || isUInt<4>(Imm);
   }
 
   template <unsigned N> bool IsUImm() const {
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isUInt<N>(Imm) && VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isUInt<N>(Imm);
   }
 
   bool isUImm1() const { return IsUImm<1>(); }
@@ -746,144 +736,116 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isUInt<5>(Imm) && (Imm != 0);
   }
 
   bool isUImm5GT3() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isUInt<5>(Imm) && (Imm > 3) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isUInt<5>(Imm) && (Imm > 3);
   }
 
   bool isUImm5Plus1() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && ((isUInt<5>(Imm) && (Imm != 0)) || (Imm == 32)) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && ((isUInt<5>(Imm) && (Imm != 0)) || (Imm == 32));
   }
 
   bool isUImm5GE6Plus1() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && ((isUInt<5>(Imm) && (Imm >= 6)) || (Imm == 32)) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && ((isUInt<5>(Imm) && (Imm >= 6)) || (Imm == 32));
   }
 
   bool isUImm5Slist() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm &&
            ((Imm == 0) || (Imm == 1) || (Imm == 2) || (Imm == 4) ||
-            (Imm == 8) || (Imm == 16) || (Imm == 15) || (Imm == 31)) &&
-           VK == RISCVMCExpr::VK_None;
+            (Imm == 8) || (Imm == 16) || (Imm == 15) || (Imm == 31));
   }
 
   bool isUImm8GE32() const {
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isUInt<8>(Imm) && Imm >= 32 &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isUInt<8>(Imm) && Imm >= 32;
   }
 
   bool isRnumArg() const {
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && Imm >= INT64_C(0) && Imm <= INT64_C(10) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && Imm >= INT64_C(0) && Imm <= INT64_C(10);
   }
 
   bool isRnumArg_0_7() const {
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && Imm >= INT64_C(0) && Imm <= INT64_C(7) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && Imm >= INT64_C(0) && Imm <= INT64_C(7);
   }
 
   bool isRnumArg_1_10() const {
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && Imm >= INT64_C(1) && Imm <= INT64_C(10) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && Imm >= INT64_C(1) && Imm <= INT64_C(10);
   }
 
   bool isRnumArg_2_14() const {
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && Imm >= INT64_C(2) && Imm <= INT64_C(14) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && Imm >= INT64_C(2) && Imm <= INT64_C(14);
   }
 
   bool isSImm5() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isInt<5>(fixImmediateForRV32(Imm, isRV64Imm())) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isInt<5>(fixImmediateForRV32(Imm, isRV64Imm()));
   }
 
   bool isSImm5NonZero() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm && Imm != 0 &&
-           isInt<5>(fixImmediateForRV32(Imm, isRV64Imm())) &&
-           VK == RISCVMCExpr::VK_None;
+           isInt<5>(fixImmediateForRV32(Imm, isRV64Imm()));
   }
 
   bool isSImm6() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isInt<6>(fixImmediateForRV32(Imm, isRV64Imm())) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isInt<6>(fixImmediateForRV32(Imm, isRV64Imm()));
   }
 
   bool isSImm6NonZero() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm && Imm != 0 &&
-           isInt<6>(fixImmediateForRV32(Imm, isRV64Imm())) &&
-           VK == RISCVMCExpr::VK_None;
+           isInt<6>(fixImmediateForRV32(Imm, isRV64Imm()));
   }
 
   bool isCLUIImm() const {
@@ -891,7 +853,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
       return false;
     int64_t Imm;
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm && (Imm != 0) &&
            (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
            VK == RISCVMCExpr::VK_None;
@@ -901,70 +863,56 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isShiftedUInt<1, 1>(Imm) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isShiftedUInt<1, 1>(Imm);
   }
 
   bool isUImm5Lsb0() const {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isShiftedUInt<4, 1>(Imm) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isShiftedUInt<4, 1>(Imm);
   }
 
   bool isUImm6Lsb0() const {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isShiftedUInt<5, 1>(Imm) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isShiftedUInt<5, 1>(Imm);
   }
 
   bool isUImm7Lsb00() const {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isShiftedUInt<5, 2>(Imm);
   }
 
   bool isUImm7Lsb000() const {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isShiftedUInt<4, 3>(Imm) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isShiftedUInt<4, 3>(Imm);
   }
 
   bool isUImm8Lsb00() const {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isShiftedUInt<6, 2>(Imm);
   }
 
   bool isUImm8Lsb000() const {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isShiftedUInt<5, 3>(Imm);
   }
 
   bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
@@ -973,20 +921,16 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isShiftedUInt<6, 3>(Imm);
   }
 
   bool isUImm10Lsb00NonZero() const {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0);
   }
 
   // If this a RV32 and the immediate is a uimm32, sign extend it to 32 bits.
@@ -1000,11 +944,9 @@ struct RISCVOperand final : public MCParsedAsmOperand {
   bool isSImm11() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isInt<11>(fixImmediateForRV32(Imm, isRV64Imm())) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isInt<11>(fixImmediateForRV32(Imm, isRV64Imm()));
   }
 
   bool isSImm12() const {
@@ -1013,7 +955,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     bool IsValid;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     if (!IsConstantImm)
       IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK);
     else
@@ -1031,12 +973,10 @@ struct RISCVOperand final : public MCParsedAsmOperand {
   bool isSImm12Lsb00000() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm &&
-           isShiftedInt<7, 5>(fixImmediateForRV32(Imm, isRV64Imm())) &&
-           VK == RISCVMCExpr::VK_None;
+           isShiftedInt<7, 5>(fixImmediateForRV32(Imm, isRV64Imm()));
   }
 
   bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
@@ -1045,32 +985,26 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm && (Imm != 0) &&
-           isShiftedInt<6, 4>(fixImmediateForRV32(Imm, isRV64Imm())) &&
-           VK == RISCVMCExpr::VK_None;
+           isShiftedInt<6, 4>(fixImmediateForRV32(Imm, isRV64Imm()));
   }
 
   bool isSImm16NonZero() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm && Imm != 0 &&
-           isInt<16>(fixImmediateForRV32(Imm, isRV64Imm())) &&
-           VK == RISCVMCExpr::VK_None;
+           isInt<16>(fixImmediateForRV32(Imm, isRV64Imm()));
   }
 
   bool isUImm16NonZero() const {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && isUInt<16>(Imm) && (Imm != 0) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isUInt<16>(Imm) && (Imm != 0);
   }
 
   bool isUImm20LUI() const {
@@ -1079,7 +1013,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     bool IsValid;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     if (!IsConstantImm) {
       IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK);
       return IsValid &&
@@ -1097,7 +1031,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     bool IsValid;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     if (!IsConstantImm) {
       IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK);
       return IsValid &&
@@ -1120,20 +1054,16 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     if (!isImm())
       return false;
     int64_t Imm;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm && (Imm == 0) && VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && Imm == 0;
   }
 
   bool isSImm5Plus1() const {
     if (!isImm())
       return false;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
-    return IsConstantImm &&
-           isInt<5>(fixImmediateForRV32(Imm, isRV64Imm()) - 1) &&
-           VK == RISCVMCExpr::VK_None;
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
+    return IsConstantImm && isInt<5>(fixImmediateForRV32(Imm, isRV64Imm()) - 1);
   }
 
   bool isSImm20() const {
@@ -1141,7 +1071,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
       return false;
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm && (VK == RISCVMCExpr::VK_None) &&
            isInt<20>(fixImmediateForRV32(Imm, isRV64Imm()));
   }
@@ -1151,7 +1081,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
       return false;
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm && (VK == RISCVMCExpr::VK_None) &&
            isInt<26>(fixImmediateForRV32(Imm, isRV64Imm()));
   }
@@ -1161,7 +1091,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     if (!isImm())
       return false;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm && isInt<32>(fixImmediateForRV32(Imm, isRV64Imm())) &&
            VK == RISCVMCExpr::VK_None;
   }
@@ -1171,7 +1101,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
       return false;
     RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
     int64_t Imm;
-    bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
+    bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
     return IsConstantImm &&
            isShiftedInt<31, 1>(fixImmediateForRV32(Imm, isRV64Imm())) &&
            VK == RISCVMCExpr::VK_None;
@@ -1385,8 +1315,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
   static void addExpr(MCInst &Inst, const MCExpr *Expr, bool IsRV64Imm) {
     assert(Expr && "Expr shouldn't be null!");
     int64_t Imm = 0;
-    RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-    bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
+    bool IsConstant = evaluateConstantImm(Expr, Imm);
 
     if (IsConstant)
       Inst.addOperand(
@@ -1435,9 +1364,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
     assert(N == 1 && "Invalid number of operands!");
     int64_t Imm = 0;
     if (Kind == KindTy::Immediate) {
-      RISCVMCExpr::Specifier VK = RISCVMCExpr::VK_None;
-      [[maybe_unused]] bool IsConstantImm =
-          evaluateConstantImm(getImm(), Imm, VK);
+      [[maybe_unused]] bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
       assert(IsConstantImm && "Invalid VTypeI Operand!");
     } else {
       Imm = getVType();
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
index b73496beba7e0..060f2fca84dd1 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
@@ -169,6 +169,7 @@ bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
   MCValue Value;
   if (specifier != VK_LO && specifier != VK_HI)
     return false;
+  return false;
 
   if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr))
     return false;
diff --git a/llvm/test/MC/RISCV/insn.s b/llvm/test/MC/RISCV/insn.s
index b1ef2511b96c6..7edde9ef451f3 100644
--- a/llvm/test/MC/RISCV/insn.s
+++ b/llvm/test/MC/RISCV/insn.s
@@ -146,12 +146,12 @@ target:
 # CHECK-OBJ: fmadd.s fa0, fa1, fa2, fa3, rne
 .insn r4 MADD,  0,  0, fa0, fa1, fa2, fa3
 
-# CHECK-ASM: .insn i 3, 5, t1, -2048(t2)
-# CHECK-ASM: encoding: [0x03,0xd3,0x03,0x80]
+# CHECK-ASM: .insn i 3, 5, t1, %lo(2048)(t2)
+# CHECK-ASM: encoding: [0x03,0xd3,0bAAAA0011,A]
 # CHECK-OBJ: lhu t1, -0x800(t2)
 .insn i 0x3, 0x5, x6, %lo(2048)(x7)
-# CHECK-ASM: .insn i 3, 5, t1, -2048(t2)
-# CHECK-ASM: encoding: [0x03,0xd3,0x03,0x80]
+# CHECK-ASM: .insn i 3, 5, t1, %lo(2048)(t2)
+# CHECK-ASM: encoding: [0x03,0xd3,0bAAAA0011,A]
 # CHECK-OBJ: lhu t1, -0x800(t2)
 .insn i LOAD, 0x5, x6, %lo(2048)(x7)
 
diff --git a/llvm/test/MC/RISCV/rv32i-aliases-valid.s b/llvm/test/MC/RISCV/rv32i-aliases-valid.s
index 20deda4ec5eaf..4ebbac4aab983 100644
--- a/llvm/test/MC/RISCV/rv32i-aliases-valid.s
+++ b/llvm/test/MC/RISCV/rv32i-aliases-valid.s
@@ -83,8 +83,9 @@ li x12, 0x80000000
 # CHECK-ALIAS: li a2, -1
 li x12, 0xFFFFFFFF
 
-# CHECK-INST: addi a0, zero, 1110
-# CHECK-ALIAS: li a0, 1110
+# CHECK-ASM-NOALIAS: addi a0, zero, %lo(1193046)
+# CHECK-OBJ-NOALIAS: addi a0, zero, 1110
+# CHECK-ASM: addi a0, zero, %lo(1193046)
 li a0, %lo(0x123456)
 
 # CHECK-OBJ-NOALIAS: addi a0, zero, 0
diff --git a/llvm/test/MC/RISCV/rvd-valid.s b/llvm/test/MC/RISCV/rvd-valid.s
index 5135562352a0f..81abedd74c286 100644
--- a/llvm/test/MC/RISCV/rvd-valid.s
+++ b/llvm/test/MC/RISCV/rvd-valid.s
@@ -23,8 +23,8 @@ fld f1, +4(ra)
 # CHECK-ASM-AND-OBJ: fld ft2, -2048(a3)
 # CHECK-ASM: encoding: [0x07,0xb1,0x06,0x80]
 fld f2, -2048(x13)
-# CHECK-ASM-AND-OBJ: fld ft3, -2048(s1)
-# CHECK-ASM: encoding: [0x87,0xb1,0x04,0x80]
+# CHECK-ASM: fld ft3, %lo(2048)(s1)  # encoding: [0x87,0xb1,0bAAAA0100,A]
+# CHECK-OBJ: fld ft3, -2048(s1)
 fld f3, %lo(2048)(s1)
 # CHECK-ASM-AND-OBJ: fld ft4, 2047(s2)
 # CHECK-ASM: encoding: [0x07,0x32,0xf9,0x7f]
@@ -39,8 +39,8 @@ fsd f6, 2047(s4)
 # CHECK-ASM-AND-OBJ: fsd ft7, -2048(s5)
 # CHECK-ASM: encoding: [0x27,0xb0,0x7a,0x80]
 fsd f7, -2048(s5)
-# CHECK-ASM-AND-OBJ: fsd fs0, -2048(s6)
-# CHECK-ASM: encoding: [0x27,0x30,0x8b,0x80]
+# CHECK-ASM: fsd fs0, %lo(2048)(s6)  # encoding: [0x27'A',0x30'A',0x8b'A',A]
+# CHECK-OBJ: fsd fs0, -2048(s6)
 fsd f8, %lo(2048)(s6)
 # CHECK-ASM-AND-OBJ: fsd fs1, 999(s7)
 # CHECK-ASM: encoding: [0xa7,0xb3,0x9b,0x3e]
diff --git a/llvm/test/MC/RISCV/rvf-valid.s b/llvm/test/MC/RISCV/rvf-valid.s
index 49b67a0e9db94..29969118ffd04 100644
--- a/llvm/test/MC/RISCV/rvf-valid.s
+++ b/llvm/test/MC/RISCV/rvf-valid.s
@@ -18,8 +18,8 @@ flw f1, +4(ra)
 # CHECK-ASM-AND-OBJ: flw ft2, -2048(a3)
 # CHECK-ASM: encoding: [0x07,0xa1,0x06,0x80]
 flw f2, -2048(x13)
-# CHECK-ASM-AND-OBJ: flw ft3, -2048(s1)
-# CHECK-ASM: encoding: [0x87,0xa1,0x04,0x80]
+# CHECK-ASM: flw ft0, %lo(2048)(s6)  # encoding: [0x27'A',0x20'A',0x8b'A',A]
+# CHECK-OBJ: flw ft3, -2048(s1)
 flw f3, %lo(2048)(s1)
 # CHECK-ASM-AND-OBJ: flw ft4, 2047(s2)
 # CHECK-ASM: encoding: [0x07,0x22,0xf9,0x7f]
diff --git a/llvm/test/MC/RISCV/rvi-valid.s b/llvm/test/MC/RISCV/rvi-valid.s
index 25b72d479bc6c..f91f363703269 100644
--- a/llvm/test/MC/RISCV/rvi-valid.s
+++ b/llvm/test/MC/RISCV/rvi-valid.s
@@ -20,14 +20,15 @@ lui a0, 2
 # CHECK-ASM-AND-OBJ: lui s11, 552960
 # CHECK-ASM: encoding: [0xb7,0x0d,0x00,0x87]
 lui s11, (0x87000000>>12)
-# CHECK-ASM-AND-OBJ: lui a0, 0
-# CHECK-ASM: encoding: [0x37,0x05,0x00,0x00]
+# CHECK-ASM: lui a0, %hi(2)  # encoding: [0x37,0bAAAA0101,A,A]
+# CHECK-OBJ: lui a0, 0
 lui a0, %hi(2)
 # CHECK-ASM-AND-OBJ: lui s11, 552960
 # CHECK-ASM: encoding: [0xb7,0x0d,0x00,0x87]
 lui s11, (0x87000000>>12)
-# CHECK-ASM-AND-OBJ: lui s11, 552960
-# CHECK-ASM: encoding: [0xb7,0x0d,0x00,0x87]
+# CHECK-ASM: lui s11, %hi(2264924160)
+# CHECK-OBJ: lui s11, 552960
+# CHECK-ASM: encoding: [0xb7,0bAAAA1101,A,A]
 lui s11, %hi(0x87000000)
 # CHECK-ASM-AND-OBJ: lui t0, 1048575
 # CHECK-ASM: encoding: [0xb7,0xf2,0xff,0xff]
@@ -104,8 +105,9 @@ jal zero, .
 # CHECK-ASM-AND-OBJ: jalr a0, -2048(a1)
 # CHECK-ASM: encoding: [0x67,0x85,0x05,0x80]
 jalr a0, -2048(a1)
-# CHECK-ASM-AND-OBJ: jalr a0, -2048(a1)
-# CHECK-ASM: encoding: [0x67,0x85,0x05,0x80]
+# CHECK-ASM: jalr a0, %lo(2048)(a1)
+# CHECK-ASM: encoding: [0x67,0x85,0bAAAA0101,A]
+# CHECK-OBJ: jalr a0, -2048(a1)
 jalr a0, %lo(2048)(a1)
 # CHECK-ASM-AND-OBJ: jalr t2, 2047(t1)
 # CHECK-ASM: encoding: [0xe7,0x03,0xf3,0x7f]
@@ -162,8 +164,9 @@ lh t1, ~2047(zero)
 # CHECK-ASM-AND-OBJ: lh t1, 0(zero)
 # CHECK-ASM: encoding: [0x03,0x13,0x00,0x00]
 lh t1, !1(zero)
-# CHECK-ASM-AND-OBJ: lh t1, -2048(zero)
-# CHECK-ASM: encoding: [0x03,0x13,0x00,0x80]
+# CHECK-ASM: lh t1, %lo(2048)(zero)
+# CHECK-ASM: encoding: [0x03,0x13,0bAAAA0000,A]
+# CHECK-OBJ: lh t1, -2048(zero)
 lh t1, %lo(2048)(zero)
 # CHECK-ASM-AND-OBJ: lh sp, 2047(a0)
 # CHECK-ASM: encoding: [0x03,0x11,0xf5,0x7f]
@@ -203,8 +206,9 @@ sh t3, ~2047(t5)
 # CHECK-ASM-AND-OBJ: sh t3, 0(t5)
 # CHECK-ASM: encoding: [0x23,0x10,0xcf,0x01]
 sh t3, !1(t5)
-# CHECK-ASM-AND-OBJ: sh t3, -2048(t5)
-# CHECK-ASM: encoding: [0x23,0x10,0xcf,0x81]
+# CHECK-ASM: sh t3, %lo(2048)(t5)
+# CHECK-ASM: encoding: [0x23'A',0x10'A',0xcf'A',0x01'A']
+# CHECK-OBJ: sh t3, -2048(t5)
 sh t3, %lo(2048)(t5)
 # CHECK-ASM-AND-OBJ: sw ra, 999(zero)
 # CHECK-ASM: encoding: [0xa3,0x23,0x10,0x3e]
@@ -254,8 +258,8 @@ ori a0, a1, ~2047
 # CHECK-ASM-AND-OBJ: ori a0, a1, 0
 # CHECK-ASM: encoding: [0x13,0xe5,0x05,0x00]
 ori a0, a1, !1
-# CHECK-ASM-AND-OBJ: ori a0, a1, -2048
-# CHECK-ASM: encoding: [0x13,0xe5,0x05,0x80]
+# CHECK-ASM: ori a0, a1, %lo(2048)  # encoding: [0x13,0xe5,0bAAAA0101,A]
+# CHECK-OBJ: ori a0, a1, -2048
 ori a0, a1, %lo(2048)
 # CHECK-ASM-AND-OBJ: andi ra, sp, 2047
 # CHECK-ASM: encoding: [0x93,0x70,0xf1,0x7f]
diff --git a/llvm/test/MC/RISCV/rvzfh-valid.s b/llvm/test/MC/RISCV/rvzfh-valid.s
index ccc6d10188de2..c39eb2dc426c8 100644
--- a/llvm/test/MC/RISCV/rvzfh-valid.s
+++ b/llvm/test/MC/RISCV/rvzfh-valid.s
@@ -18,8 +18,9 @@ flh f1, +4(ra)
 # CHECK-ASM-AND-OBJ: flh ft2, -2048(a3)
 # CHECK-ASM: encoding: [0x07,0x91,0x06,0x80]
 flh f2, -2048(x13)
-# CHECK-ASM-AND-OBJ: flh ft3, -2048(s1)
-# CHECK-ASM: encoding: [0x87,0x91,0x04,0x80]
+# CHECK-ASM: flh ft3, %lo(2048)(s1)
+# CHECCK-ASM: encoding: [0x87,0x91,0xAAAA0100,A]
+# CHECK-OBJ: flh ft3, -2048(s1)
 flh f3, %lo(2048)(s1)
 # CHECK-ASM-AND-OBJ: flh ft4, 2047(s2)
 # CHECK-ASM: encoding: [0x07,0x12,0xf9,0x7f]

>From 037e7fd97cccc3941be14f9f8c79fd51af57b9dc Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 27 Mar 2025 23:33:36 -0700
Subject: [PATCH 2/2] .

Created using spr 1.3.5-bogner
---
 .../Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp | 28 -------------------
 1 file changed, 28 deletions(-)

diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
index 060f2fca84dd1..15a199a755ad9 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
@@ -164,31 +164,3 @@ StringRef RISCVMCExpr::getSpecifierName(Specifier S) {
   }
   llvm_unreachable("Invalid ELF symbol kind");
 }
-
-bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
-  MCValue Value;
-  if (specifier != VK_LO && specifier != VK_HI)
-    return false;
-  return false;
-
-  if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr))
-    return false;
-
-  if (!Value.isAbsolute())
-    return false;
-
-  Res = evaluateAsInt64(Value.getConstant());
-  return true;
-}
-
-int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value) const {
-  switch (specifier) {
-  default:
-    llvm_unreachable("Invalid kind");
-  case VK_LO:
-    return SignExtend64<12>(Value);
-  case VK_HI:
-    // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
-    return ((Value + 0x800) >> 12) & 0xfffff;
-  }
-}



More information about the llvm-commits mailing list