[llvm] 8981298 - Move PowerPC-specific absolute MCSymbolRefExpr::VariantKind to PPCMCExpr

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 4 22:14:09 PST 2025


Author: Fangrui Song
Date: 2025-03-04T22:14:04-08:00
New Revision: 89812985358784b16fb66928ad4da411386f4720

URL: https://github.com/llvm/llvm-project/commit/89812985358784b16fb66928ad4da411386f4720
DIFF: https://github.com/llvm/llvm-project/commit/89812985358784b16fb66928ad4da411386f4720.diff

LOG: Move PowerPC-specific absolute MCSymbolRefExpr::VariantKind to PPCMCExpr

This cleans up @l @ha optimization in PPCAsmParser and is also the first
step toward removing VK_PPC_* from the generic MCSymbolRefExpr::VariantKind.

Basically we ensure that @l @ha family modifiers always lead to
PPCMCExpr and avoid MCSymbolRefExpr::VariantKind. This allows us
to delete a lot of switch statements that involve a long list of VK_PPC_LO/VK_PPC_HI/...

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCExpr.h
    llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
    llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp
    llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp
    llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.cpp
    llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.h
    llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCExpr.h b/llvm/include/llvm/MC/MCExpr.h
index 8bae5349b515e..a7802ba9a7b4d 100644
--- a/llvm/include/llvm/MC/MCExpr.h
+++ b/llvm/include/llvm/MC/MCExpr.h
@@ -252,15 +252,6 @@ class MCSymbolRefExpr : public MCExpr {
     VK_AVR_DIFF32,
     VK_AVR_PM,
 
-    VK_PPC_LO,              // symbol at l
-    VK_PPC_HI,              // symbol at h
-    VK_PPC_HA,              // symbol at ha
-    VK_PPC_HIGH,            // symbol at high
-    VK_PPC_HIGHA,           // symbol at higha
-    VK_PPC_HIGHER,          // symbol at higher
-    VK_PPC_HIGHERA,         // symbol at highera
-    VK_PPC_HIGHEST,         // symbol at highest
-    VK_PPC_HIGHESTA,        // symbol at highesta
     VK_PPC_GOT_LO,          // symbol at got@l
     VK_PPC_GOT_HI,          // symbol at got@h
     VK_PPC_GOT_HA,          // symbol at got@ha

diff  --git a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
index 038bb805b2f30..aeae1619a187f 100644
--- a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
+++ b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
@@ -1384,32 +1384,32 @@ PPCAsmParser::extractModifierFromExpr(const MCExpr *E,
   case MCExpr::SymbolRef: {
     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
 
-    switch (SRE->getKind()) {
-    case MCSymbolRefExpr::VK_PPC_LO:
+    switch ((PPCMCExpr::VariantKind)SRE->getKind()) {
+    case PPCMCExpr::VK_PPC_LO:
       Variant = PPCMCExpr::VK_PPC_LO;
       break;
-    case MCSymbolRefExpr::VK_PPC_HI:
+    case PPCMCExpr::VK_PPC_HI:
       Variant = PPCMCExpr::VK_PPC_HI;
       break;
-    case MCSymbolRefExpr::VK_PPC_HA:
+    case PPCMCExpr::VK_PPC_HA:
       Variant = PPCMCExpr::VK_PPC_HA;
       break;
-    case MCSymbolRefExpr::VK_PPC_HIGH:
+    case PPCMCExpr::VK_PPC_HIGH:
       Variant = PPCMCExpr::VK_PPC_HIGH;
       break;
-    case MCSymbolRefExpr::VK_PPC_HIGHA:
+    case PPCMCExpr::VK_PPC_HIGHA:
       Variant = PPCMCExpr::VK_PPC_HIGHA;
       break;
-    case MCSymbolRefExpr::VK_PPC_HIGHER:
+    case PPCMCExpr::VK_PPC_HIGHER:
       Variant = PPCMCExpr::VK_PPC_HIGHER;
       break;
-    case MCSymbolRefExpr::VK_PPC_HIGHERA:
+    case PPCMCExpr::VK_PPC_HIGHERA:
       Variant = PPCMCExpr::VK_PPC_HIGHERA;
       break;
-    case MCSymbolRefExpr::VK_PPC_HIGHEST:
+    case PPCMCExpr::VK_PPC_HIGHEST:
       Variant = PPCMCExpr::VK_PPC_HIGHEST;
       break;
-    case MCSymbolRefExpr::VK_PPC_HIGHESTA:
+    case PPCMCExpr::VK_PPC_HIGHESTA:
       Variant = PPCMCExpr::VK_PPC_HIGHESTA;
       break;
     default:
@@ -1852,26 +1852,22 @@ const MCExpr *
 PPCAsmParser::applyModifierToExpr(const MCExpr *E,
                                   MCSymbolRefExpr::VariantKind Variant,
                                   MCContext &Ctx) {
-  switch (Variant) {
-  case MCSymbolRefExpr::VK_PPC_LO:
-    return PPCMCExpr::create(PPCMCExpr::VK_PPC_LO, E, Ctx);
-  case MCSymbolRefExpr::VK_PPC_HI:
-    return PPCMCExpr::create(PPCMCExpr::VK_PPC_HI, E, Ctx);
-  case MCSymbolRefExpr::VK_PPC_HA:
-    return PPCMCExpr::create(PPCMCExpr::VK_PPC_HA, E, Ctx);
-  case MCSymbolRefExpr::VK_PPC_HIGH:
-    return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGH, E, Ctx);
-  case MCSymbolRefExpr::VK_PPC_HIGHA:
-    return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHA, E, Ctx);
-  case MCSymbolRefExpr::VK_PPC_HIGHER:
-    return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHER, E, Ctx);
-  case MCSymbolRefExpr::VK_PPC_HIGHERA:
-    return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHERA, E, Ctx);
-  case MCSymbolRefExpr::VK_PPC_HIGHEST:
-    return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHEST, E, Ctx);
-  case MCSymbolRefExpr::VK_PPC_HIGHESTA:
-    return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHESTA, E, Ctx);
-  default:
-    return nullptr;
+  if (isa<MCConstantExpr>(E)) {
+    switch (PPCMCExpr::VariantKind(Variant)) {
+    case PPCMCExpr::VariantKind::VK_PPC_LO:
+    case PPCMCExpr::VariantKind::VK_PPC_HI:
+    case PPCMCExpr::VariantKind::VK_PPC_HA:
+    case PPCMCExpr::VariantKind::VK_PPC_HIGH:
+    case PPCMCExpr::VariantKind::VK_PPC_HIGHA:
+    case PPCMCExpr::VariantKind::VK_PPC_HIGHER:
+    case PPCMCExpr::VariantKind::VK_PPC_HIGHERA:
+    case PPCMCExpr::VariantKind::VK_PPC_HIGHEST:
+    case PPCMCExpr::VariantKind::VK_PPC_HIGHESTA:
+      break;
+    default:
+      return nullptr;
+    }
   }
+
+  return PPCMCExpr::create(PPCMCExpr::VariantKind(Variant), E, Ctx);
 }

diff  --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp
index 395d43b960e76..14e7be021e24d 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp
@@ -9,6 +9,7 @@
 #include "MCTargetDesc/PPCFixupKinds.h"
 #include "MCTargetDesc/PPCMCExpr.h"
 #include "MCTargetDesc/PPCMCTargetDesc.h"
+#include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCELFObjectWriter.h"
 #include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCObjectWriter.h"
@@ -43,30 +44,7 @@ static MCSymbolRefExpr::VariantKind getAccessVariant(const MCValue &Target,
 
   if (Expr->getKind() != MCExpr::Target)
     return Target.getAccessVariant();
-
-  switch (cast<PPCMCExpr>(Expr)->getKind()) {
-  case PPCMCExpr::VK_PPC_None:
-    return MCSymbolRefExpr::VK_None;
-  case PPCMCExpr::VK_PPC_LO:
-    return MCSymbolRefExpr::VK_PPC_LO;
-  case PPCMCExpr::VK_PPC_HI:
-    return MCSymbolRefExpr::VK_PPC_HI;
-  case PPCMCExpr::VK_PPC_HA:
-    return MCSymbolRefExpr::VK_PPC_HA;
-  case PPCMCExpr::VK_PPC_HIGH:
-    return MCSymbolRefExpr::VK_PPC_HIGH;
-  case PPCMCExpr::VK_PPC_HIGHA:
-    return MCSymbolRefExpr::VK_PPC_HIGHA;
-  case PPCMCExpr::VK_PPC_HIGHERA:
-    return MCSymbolRefExpr::VK_PPC_HIGHERA;
-  case PPCMCExpr::VK_PPC_HIGHER:
-    return MCSymbolRefExpr::VK_PPC_HIGHER;
-  case PPCMCExpr::VK_PPC_HIGHEST:
-    return MCSymbolRefExpr::VK_PPC_HIGHEST;
-  case PPCMCExpr::VK_PPC_HIGHESTA:
-    return MCSymbolRefExpr::VK_PPC_HIGHESTA;
-  }
-  llvm_unreachable("unknown PPCMCExpr kind");
+  return MCSymbolRefExpr::VariantKind(cast<PPCMCExpr>(Expr)->getKind());
 }
 
 unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
@@ -75,6 +53,7 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
   MCFixupKind Kind = Fixup.getKind();
   if (Kind >= FirstLiteralRelocationKind)
     return Kind - FirstLiteralRelocationKind;
+  auto RefKind = static_cast<PPCMCExpr::VariantKind>(Target.getRefKind());
   MCSymbolRefExpr::VariantKind Modifier = getAccessVariant(Target, Fixup);
 
   // determine the type of the relocation
@@ -107,20 +86,18 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       Type = ELF::R_PPC_REL14;
       break;
     case PPC::fixup_ppc_half16:
-      switch (Modifier) {
-      default: llvm_unreachable("Unsupported Modifier");
-      case MCSymbolRefExpr::VK_None:
-        Type = ELF::R_PPC_REL16;
-        break;
-      case MCSymbolRefExpr::VK_PPC_LO:
-        Type = ELF::R_PPC_REL16_LO;
-        break;
-      case MCSymbolRefExpr::VK_PPC_HI:
-        Type = ELF::R_PPC_REL16_HI;
-        break;
-      case MCSymbolRefExpr::VK_PPC_HA:
-        Type = ELF::R_PPC_REL16_HA;
-        break;
+      switch (RefKind) {
+      default:
+        Ctx.reportError(Fixup.getLoc(), "invalid VariantKind");
+        return ELF::R_PPC_NONE;
+      case PPCMCExpr::VK_PPC_None:
+        return ELF::R_PPC_REL16;
+      case PPCMCExpr::VK_PPC_LO:
+        return ELF::R_PPC_REL16_LO;
+      case PPCMCExpr::VK_PPC_HI:
+        return ELF::R_PPC_REL16_HI;
+      case PPCMCExpr::VK_PPC_HA:
+        return ELF::R_PPC_REL16_HA;
       }
       break;
     case PPC::fixup_ppc_half16ds:
@@ -168,38 +145,33 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
       break;
     case PPC::fixup_ppc_half16:
+      switch (RefKind) {
+      case PPCMCExpr::VK_PPC_None:
+        break;
+      case PPCMCExpr::VK_PPC_LO:
+        return ELF::R_PPC_ADDR16_LO;
+      case PPCMCExpr::VK_PPC_HI:
+        return ELF::R_PPC_ADDR16_HI;
+      case PPCMCExpr::VK_PPC_HA:
+        return ELF::R_PPC_ADDR16_HA;
+      case PPCMCExpr::VK_PPC_HIGH:
+        return ELF::R_PPC64_ADDR16_HIGH;
+      case PPCMCExpr::VK_PPC_HIGHA:
+        return ELF::R_PPC64_ADDR16_HIGHA;
+      case PPCMCExpr::VK_PPC_HIGHER:
+        return ELF::R_PPC64_ADDR16_HIGHER;
+      case PPCMCExpr::VK_PPC_HIGHERA:
+        return ELF::R_PPC64_ADDR16_HIGHERA;
+      case PPCMCExpr::VK_PPC_HIGHEST:
+        return ELF::R_PPC64_ADDR16_HIGHEST;
+      case PPCMCExpr::VK_PPC_HIGHESTA:
+        return ELF::R_PPC64_ADDR16_HIGHESTA;
+      }
       switch (Modifier) {
       default: llvm_unreachable("Unsupported Modifier");
       case MCSymbolRefExpr::VK_None:
         Type = ELF::R_PPC_ADDR16;
         break;
-      case MCSymbolRefExpr::VK_PPC_LO:
-        Type = ELF::R_PPC_ADDR16_LO;
-        break;
-      case MCSymbolRefExpr::VK_PPC_HI:
-        Type = ELF::R_PPC_ADDR16_HI;
-        break;
-      case MCSymbolRefExpr::VK_PPC_HA:
-        Type = ELF::R_PPC_ADDR16_HA;
-        break;
-      case MCSymbolRefExpr::VK_PPC_HIGH:
-        Type = ELF::R_PPC64_ADDR16_HIGH;
-        break;
-      case MCSymbolRefExpr::VK_PPC_HIGHA:
-        Type = ELF::R_PPC64_ADDR16_HIGHA;
-        break;
-      case MCSymbolRefExpr::VK_PPC_HIGHER:
-        Type = ELF::R_PPC64_ADDR16_HIGHER;
-        break;
-      case MCSymbolRefExpr::VK_PPC_HIGHERA:
-        Type = ELF::R_PPC64_ADDR16_HIGHERA;
-        break;
-      case MCSymbolRefExpr::VK_PPC_HIGHEST:
-        Type = ELF::R_PPC64_ADDR16_HIGHEST;
-        break;
-      case MCSymbolRefExpr::VK_PPC_HIGHESTA:
-        Type = ELF::R_PPC64_ADDR16_HIGHESTA;
-        break;
       case MCSymbolRefExpr::VK_GOT:
         Type = ELF::R_PPC_GOT16;
         break;
@@ -350,14 +322,20 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
       break;
     case PPC::fixup_ppc_half16ds:
     case PPC::fixup_ppc_half16dq:
+      switch (RefKind) {
+      default:
+        Ctx.reportError(Fixup.getLoc(), "invalid VariantKind");
+        return ELF::R_PPC64_NONE;
+      case PPCMCExpr::VK_PPC_None:
+        break;
+      case PPCMCExpr::VK_PPC_LO:
+        return ELF::R_PPC64_ADDR16_LO_DS;
+      }
       switch (Modifier) {
       default: llvm_unreachable("Unsupported Modifier");
       case MCSymbolRefExpr::VK_None:
         Type = ELF::R_PPC64_ADDR16_DS;
         break;
-      case MCSymbolRefExpr::VK_PPC_LO:
-        Type = ELF::R_PPC64_ADDR16_LO_DS;
-        break;
       case MCSymbolRefExpr::VK_GOT:
         Type = ELF::R_PPC64_GOT16_DS;
         break;

diff  --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp
index fb5b665882a2f..71af7342f1a77 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "PPCMCAsmInfo.h"
+#include "PPCMCExpr.h"
 #include "llvm/MC/MCExpr.h"
 #include "llvm/TargetParser/Triple.h"
 
@@ -26,15 +27,15 @@ const MCAsmInfo::VariantKindDesc variantKindDescs[] = {
     {MCSymbolRefExpr::VK_TLSGD, "tlsgd"},
     {MCSymbolRefExpr::VK_TLSLD, "tlsld"},
     {MCSymbolRefExpr::VK_TPREL, "TPREL"},
-    {MCSymbolRefExpr::VK_PPC_LO, "l"},
-    {MCSymbolRefExpr::VK_PPC_HI, "h"},
-    {MCSymbolRefExpr::VK_PPC_HA, "ha"},
-    {MCSymbolRefExpr::VK_PPC_HIGH, "high"},
-    {MCSymbolRefExpr::VK_PPC_HIGHA, "higha"},
-    {MCSymbolRefExpr::VK_PPC_HIGHER, "higher"},
-    {MCSymbolRefExpr::VK_PPC_HIGHERA, "highera"},
-    {MCSymbolRefExpr::VK_PPC_HIGHEST, "highest"},
-    {MCSymbolRefExpr::VK_PPC_HIGHESTA, "highesta"},
+    {PPCMCExpr::VK_PPC_LO, "l"},
+    {PPCMCExpr::VK_PPC_HI, "h"},
+    {PPCMCExpr::VK_PPC_HA, "ha"},
+    {PPCMCExpr::VK_PPC_HIGH, "high"},
+    {PPCMCExpr::VK_PPC_HIGHA, "higha"},
+    {PPCMCExpr::VK_PPC_HIGHER, "higher"},
+    {PPCMCExpr::VK_PPC_HIGHERA, "highera"},
+    {PPCMCExpr::VK_PPC_HIGHEST, "highest"},
+    {PPCMCExpr::VK_PPC_HIGHESTA, "highesta"},
     {MCSymbolRefExpr::VK_PPC_GOT_LO, "got at l"},
     {MCSymbolRefExpr::VK_PPC_GOT_HI, "got at h"},
     {MCSymbolRefExpr::VK_PPC_GOT_HA, "got at ha"},

diff  --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.cpp
index cc1d98105b0cb..bfd0c69e28d56 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.cpp
@@ -67,13 +67,14 @@ PPCMCExpr::evaluateAsConstant(int64_t &Res) const {
 
   if (!Value.isAbsolute())
     return false;
-
-  Res = evaluateAsInt64(Value.getConstant());
+  auto Tmp = evaluateAsInt64(Value.getConstant());
+  if (!Tmp)
+    return false;
+  Res = *Tmp;
   return true;
 }
 
-int64_t
-PPCMCExpr::evaluateAsInt64(int64_t Value) const {
+std::optional<int64_t> PPCMCExpr::evaluateAsInt64(int64_t Value) const {
   switch (Kind) {
     case VK_PPC_LO:
       return Value & 0xffff;
@@ -93,21 +94,19 @@ PPCMCExpr::evaluateAsInt64(int64_t Value) const {
       return (Value >> 48) & 0xffff;
     case VK_PPC_HIGHESTA:
       return ((Value + 0x8000) >> 48) & 0xffff;
-    case VK_PPC_None:
-      break;
-  }
-  llvm_unreachable("Invalid kind!");
+    default:
+      return {};
+    }
 }
 
 bool PPCMCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
                                           const MCFixup *Fixup) const {
-  MCValue Value;
-
-  if (!getSubExpr()->evaluateAsRelocatable(Value, Asm, Fixup))
+  if (!getSubExpr()->evaluateAsRelocatable(Res, Asm, Fixup))
     return false;
 
-  if (Value.isAbsolute()) {
-    int64_t Result = evaluateAsInt64(Value.getConstant());
+  std::optional<int64_t> MaybeInt = evaluateAsInt64(Res.getConstant());
+  if (Res.isAbsolute() && MaybeInt) {
+    int64_t Result = *MaybeInt;
     bool IsHalf16 = Fixup && Fixup->getTargetKind() == PPC::fixup_ppc_half16;
     bool IsHalf16DS =
         Fixup && Fixup->getTargetKind() == PPC::fixup_ppc_half16ds;
@@ -122,47 +121,8 @@ bool PPCMCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
 
     Res = MCValue::get(Result);
   } else {
-    if (!Asm || !Asm->hasLayout())
-      return false;
-
-    MCContext &Context = Asm->getContext();
-    const MCSymbolRefExpr *Sym = Value.getSymA();
-    MCSymbolRefExpr::VariantKind Modifier = Sym->getKind();
-    if (Modifier != MCSymbolRefExpr::VK_None)
-      return false;
-    switch (Kind) {
-      default:
-        llvm_unreachable("Invalid kind!");
-      case VK_PPC_LO:
-        Modifier = MCSymbolRefExpr::VK_PPC_LO;
-        break;
-      case VK_PPC_HI:
-        Modifier = MCSymbolRefExpr::VK_PPC_HI;
-        break;
-      case VK_PPC_HA:
-        Modifier = MCSymbolRefExpr::VK_PPC_HA;
-        break;
-      case VK_PPC_HIGH:
-        Modifier = MCSymbolRefExpr::VK_PPC_HIGH;
-        break;
-      case VK_PPC_HIGHA:
-        Modifier = MCSymbolRefExpr::VK_PPC_HIGHA;
-        break;
-      case VK_PPC_HIGHERA:
-        Modifier = MCSymbolRefExpr::VK_PPC_HIGHERA;
-        break;
-      case VK_PPC_HIGHER:
-        Modifier = MCSymbolRefExpr::VK_PPC_HIGHER;
-        break;
-      case VK_PPC_HIGHEST:
-        Modifier = MCSymbolRefExpr::VK_PPC_HIGHEST;
-        break;
-      case VK_PPC_HIGHESTA:
-        Modifier = MCSymbolRefExpr::VK_PPC_HIGHESTA;
-        break;
-    }
-    Sym = MCSymbolRefExpr::create(&Sym->getSymbol(), Modifier, Context);
-    Res = MCValue::get(Sym, Value.getSymB(), Value.getConstant());
+    Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(),
+                       getKind());
   }
 
   return true;

diff  --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.h b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.h
index 59ca51459a40c..79dad4156d4b3 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.h
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.h
@@ -11,6 +11,7 @@
 
 #include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCValue.h"
+#include <optional>
 
 namespace llvm {
 
@@ -18,7 +19,9 @@ class PPCMCExpr : public MCTargetExpr {
 public:
   enum VariantKind {
     VK_PPC_None,
-    VK_PPC_LO,
+    // We currently use both MCSymbolRefExpr::VariantKind and
+    // PPCMCExpr::VariantKind. Start at a larger number to avoid conflicts.
+    VK_PPC_LO = 200,
     VK_PPC_HI,
     VK_PPC_HA,
     VK_PPC_HIGH,
@@ -33,7 +36,7 @@ class PPCMCExpr : public MCTargetExpr {
   const VariantKind Kind;
   const MCExpr *Expr;
 
-  int64_t evaluateAsInt64(int64_t Value) const;
+  std::optional<int64_t> evaluateAsInt64(int64_t Value) const;
 
   explicit PPCMCExpr(VariantKind Kind, const MCExpr *Expr)
       : Kind(Kind), Expr(Expr) {}

diff  --git a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
index 80c30bafaf65f..0e406f7ba93b6 100644
--- a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
+++ b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
@@ -1408,10 +1408,16 @@ void PPCAsmPrinter::emitInstruction(const MachineInstr *MI) {
   case PPC::PPC32GOT: {
     MCSymbol *GOTSymbol =
         OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
-    const MCExpr *SymGotTlsL = MCSymbolRefExpr::create(
-        GOTSymbol, MCSymbolRefExpr::VK_PPC_LO, OutContext);
-    const MCExpr *SymGotTlsHA = MCSymbolRefExpr::create(
-        GOTSymbol, MCSymbolRefExpr::VK_PPC_HA, OutContext);
+    const MCExpr *SymGotTlsL =
+        PPCMCExpr::create(PPCMCExpr::VK_PPC_LO,
+                          MCSymbolRefExpr::create(
+                              GOTSymbol, MCSymbolRefExpr::VK_None, OutContext),
+                          OutContext);
+    const MCExpr *SymGotTlsHA =
+        PPCMCExpr::create(PPCMCExpr::VK_PPC_HA,
+                          MCSymbolRefExpr::create(
+                              GOTSymbol, MCSymbolRefExpr::VK_None, OutContext),
+                          OutContext);
     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI)
                                  .addReg(MI->getOperand(0).getReg())
                                  .addExpr(SymGotTlsL));


        


More information about the llvm-commits mailing list