Skip to content

Commit f39696e

Browse files
committed
Sparc: Remove fixup kinds and specifiers for H44/M44/L44
The simm13 format OR instruction should use one single fixup kind, while it currently uses a lot more, including %m44/%l44. This change refactors R_SPARC_H44/R_SPARC_M44/R_SPARC_M44 handling to remove fixup kinds and specifiers. We utilize the [0, FirstLiteralRelocationKind) MCFixupKind range to encode raw relocation types that may be resolved (see the MCAssembler.cpp change). The `evaluateAsRelocatableImpl` implementation resembles PPCMCExpr::evaluateAsRelocatableImpl.
1 parent 935e5e1 commit f39696e

8 files changed

+50
-61
lines changed

llvm/lib/MC/MCAssembler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ bool MCAssembler::evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
203203
if (IsResolved) {
204204
auto TargetVal = Target;
205205
TargetVal.Cst = Value;
206-
if (mc::isRelocation(Fixup.getKind()) ||
206+
if (mc::isRelocRelocation(Fixup.getKind()) ||
207207
getBackend().shouldForceRelocation(*this, Fixup, TargetVal, STI))
208208
IsResolved = false;
209209
}

llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ using namespace llvm;
2424
static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
2525
switch (Kind) {
2626
default:
27-
llvm_unreachable("Unknown fixup kind!");
27+
assert(uint16_t(Kind) < FirstTargetFixupKind && "Unknown fixup kind!");
28+
return Value;
2829
case FK_Data_1:
2930
case FK_Data_2:
3031
case FK_Data_4:
@@ -67,15 +68,6 @@ static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
6768
case Sparc::fixup_sparc_lo10:
6869
return Value & 0x3ff;
6970

70-
case Sparc::fixup_sparc_h44:
71-
return (Value >> 22) & 0x3fffff;
72-
73-
case Sparc::fixup_sparc_m44:
74-
return (Value >> 12) & 0x3ff;
75-
76-
case Sparc::fixup_sparc_l44:
77-
return Value & 0xfff;
78-
7971
case Sparc::fixup_sparc_hh:
8072
return (Value >> 42) & 0x3fffff;
8173

@@ -141,9 +133,6 @@ namespace {
141133
{ "fixup_sparc_13", 19, 13, 0 },
142134
{ "fixup_sparc_hi22", 10, 22, 0 },
143135
{ "fixup_sparc_lo10", 22, 10, 0 },
144-
{ "fixup_sparc_h44", 10, 22, 0 },
145-
{ "fixup_sparc_m44", 22, 10, 0 },
146-
{ "fixup_sparc_l44", 20, 12, 0 },
147136
{ "fixup_sparc_hh", 10, 22, 0 },
148137
{ "fixup_sparc_hm", 22, 10, 0 },
149138
{ "fixup_sparc_lm", 10, 22, 0 },
@@ -162,9 +151,6 @@ namespace {
162151
{ "fixup_sparc_13", 0, 13, 0 },
163152
{ "fixup_sparc_hi22", 0, 22, 0 },
164153
{ "fixup_sparc_lo10", 0, 10, 0 },
165-
{ "fixup_sparc_h44", 0, 22, 0 },
166-
{ "fixup_sparc_m44", 0, 10, 0 },
167-
{ "fixup_sparc_l44", 0, 12, 0 },
168154
{ "fixup_sparc_hh", 0, 22, 0 },
169155
{ "fixup_sparc_hm", 0, 10, 0 },
170156
{ "fixup_sparc_lm", 0, 22, 0 },
@@ -230,8 +216,7 @@ namespace {
230216
const MCValue &Target, MutableArrayRef<char> Data,
231217
uint64_t Value, bool IsResolved,
232218
const MCSubtargetInfo *STI) const override {
233-
234-
if (mc::isRelocation(Fixup.getKind()))
219+
if (mc::isRelocRelocation(Fixup.getKind()))
235220
return;
236221
Value = adjustFixupValue(Fixup.getKind(), Value);
237222
if (!Value) return; // Doesn't change encoding.

llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx,
122122

123123
case Sparc::fixup_sparc_hi22: return ELF::R_SPARC_HI22;
124124
case Sparc::fixup_sparc_lo10: return ELF::R_SPARC_LO10;
125-
case Sparc::fixup_sparc_h44: return ELF::R_SPARC_H44;
126-
case Sparc::fixup_sparc_m44: return ELF::R_SPARC_M44;
127-
case Sparc::fixup_sparc_l44: return ELF::R_SPARC_L44;
128125
case Sparc::fixup_sparc_hh: return ELF::R_SPARC_HH22;
129126
case Sparc::fixup_sparc_hm: return ELF::R_SPARC_HM10;
130127
case Sparc::fixup_sparc_lm: return ELF::R_SPARC_LM22;

llvm/lib/Target/Sparc/MCTargetDesc/SparcFixupKinds.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ namespace llvm {
3939
/// fixup_sparc_lo10 - 10-bit fixup corresponding to %lo(foo)
4040
fixup_sparc_lo10,
4141

42-
/// fixup_sparc_h44 - 22-bit fixup corresponding to %h44(foo)
43-
fixup_sparc_h44,
44-
45-
/// fixup_sparc_m44 - 10-bit fixup corresponding to %m44(foo)
46-
fixup_sparc_m44,
47-
48-
/// fixup_sparc_l44 - 12-bit fixup corresponding to %l44(foo)
49-
fixup_sparc_l44,
50-
5142
/// fixup_sparc_hh - 22-bit fixup corresponding to %hh(foo)
5243
fixup_sparc_hh,
5344

llvm/lib/Target/Sparc/MCTargetDesc/SparcMCExpr.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ void SparcMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
4040

4141
StringRef SparcMCExpr::getSpecifierName(SparcMCExpr::Specifier S) {
4242
// clang-format off
43-
switch (S) {
43+
switch (uint16_t(S)) {
4444
case VK_None: return {};
4545
case VK_LO: return "lo";
4646
case VK_HI: return "hi";
47-
case VK_H44: return "h44";
48-
case VK_M44: return "m44";
49-
case VK_L44: return "l44";
47+
case ELF::R_SPARC_H44: return "h44";
48+
case ELF::R_SPARC_M44: return "m44";
49+
case ELF::R_SPARC_L44: return "l44";
5050
case VK_HH: return "hh";
5151
case VK_HM: return "hm";
5252
case VK_LM: return "lm";
@@ -89,9 +89,9 @@ SparcMCExpr::Specifier SparcMCExpr::parseSpecifier(StringRef name) {
8989
return StringSwitch<SparcMCExpr::Specifier>(name)
9090
.Case("lo", VK_LO)
9191
.Case("hi", VK_HI)
92-
.Case("h44", VK_H44)
93-
.Case("m44", VK_M44)
94-
.Case("l44", VK_L44)
92+
.Case("h44", (SparcMCExpr::Specifier)ELF::R_SPARC_H44)
93+
.Case("m44", (SparcMCExpr::Specifier)ELF::R_SPARC_M44)
94+
.Case("l44", (SparcMCExpr::Specifier)ELF::R_SPARC_L44)
9595
.Case("hh", VK_HH)
9696
.Case("uhi", VK_HH) // Nonstandard GNU extension
9797
.Case("hm", VK_HM)
@@ -132,12 +132,11 @@ SparcMCExpr::Specifier SparcMCExpr::parseSpecifier(StringRef name) {
132132
uint16_t SparcMCExpr::getFixupKind() const {
133133
// clang-format off
134134
switch (specifier) {
135-
default: llvm_unreachable("Unhandled SparcMCExpr::Specifier");
135+
default:
136+
assert(uint16_t(specifier) < FirstTargetFixupKind);
137+
return specifier;
136138
case VK_LO: return Sparc::fixup_sparc_lo10;
137139
case VK_HI: return Sparc::fixup_sparc_hi22;
138-
case VK_H44: return Sparc::fixup_sparc_h44;
139-
case VK_M44: return Sparc::fixup_sparc_m44;
140-
case VK_L44: return Sparc::fixup_sparc_l44;
141140
case VK_HH: return Sparc::fixup_sparc_hh;
142141
case VK_HM: return Sparc::fixup_sparc_hm;
143142
case VK_LM: return Sparc::fixup_sparc_lm;
@@ -177,6 +176,27 @@ bool SparcMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
177176
const MCAssembler *Asm) const {
178177
if (!getSubExpr()->evaluateAsRelocatable(Res, Asm))
179178
return false;
179+
180+
if (Res.isAbsolute()) {
181+
std::optional<int64_t> V;
182+
auto C = (uint64_t)Res.getConstant();
183+
switch (uint16_t(specifier)) {
184+
case ELF::R_SPARC_H44:
185+
V = (C >> 22) & 0x3fffff;
186+
break;
187+
case ELF::R_SPARC_M44:
188+
V = (C >> 12) & 0x3ff;
189+
break;
190+
case ELF::R_SPARC_L44:
191+
V = C & 0xfff;
192+
break;
193+
}
194+
if (V) {
195+
Res = MCValue::get(*V);
196+
return true;
197+
}
198+
}
199+
180200
Res.setSpecifier(specifier);
181201
return true;
182202
}

llvm/lib/Target/Sparc/MCTargetDesc/SparcMCExpr.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ class SparcMCExpr : public MCTargetExpr {
2424
public:
2525
enum Specifier {
2626
VK_None,
27-
VK_LO,
27+
VK_LO = 200, // larger than any relocation type
2828
VK_HI,
29-
VK_H44,
30-
VK_M44,
31-
VK_L44,
3229
VK_HH,
3330
VK_HM,
3431
VK_LM,

llvm/lib/Target/Sparc/SparcAsmPrinter.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "SparcInstrInfo.h"
2020
#include "SparcTargetMachine.h"
2121
#include "TargetInfo/SparcTargetInfo.h"
22+
#include "llvm/BinaryFormat/ELF.h"
2223
#include "llvm/CodeGen/AsmPrinter.h"
2324
#include "llvm/CodeGen/MachineInstr.h"
2425
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
@@ -75,11 +76,11 @@ class SparcAsmPrinter : public AsmPrinter {
7576
};
7677
} // end of anonymous namespace
7778

78-
static MCOperand createSparcMCOperand(SparcMCExpr::Specifier Kind,
79-
MCSymbol *Sym, MCContext &OutContext) {
80-
const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Sym,
81-
OutContext);
82-
const SparcMCExpr *expr = SparcMCExpr::create(Kind, MCSym, OutContext);
79+
static MCOperand createSparcMCOperand(uint16_t Kind, MCSymbol *Sym,
80+
MCContext &OutContext) {
81+
const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Sym, OutContext);
82+
const SparcMCExpr *expr =
83+
SparcMCExpr::create(SparcMCExpr::Specifier(Kind), MCSym, OutContext);
8384
return MCOperand::createExpr(expr);
8485
}
8586
static MCOperand createPCXCallOP(MCSymbol *Label,
@@ -163,11 +164,9 @@ static void EmitSHL(MCStreamer &OutStreamer,
163164
EmitBinary(OutStreamer, SP::SLLri, RS1, Imm, RD, STI);
164165
}
165166

166-
static void EmitHiLo(MCStreamer &OutStreamer, MCSymbol *GOTSym,
167-
SparcMCExpr::Specifier HiKind,
168-
SparcMCExpr::Specifier LoKind, MCOperand &RD,
169-
MCContext &OutContext, const MCSubtargetInfo &STI) {
170-
167+
static void EmitHiLo(MCStreamer &OutStreamer, MCSymbol *GOTSym, uint16_t HiKind,
168+
uint16_t LoKind, MCOperand &RD, MCContext &OutContext,
169+
const MCSubtargetInfo &STI) {
171170
MCOperand hi = createSparcMCOperand(HiKind, GOTSym, OutContext);
172171
MCOperand lo = createSparcMCOperand(LoKind, GOTSym, OutContext);
173172
EmitSETHI(OutStreamer, hi, RD, STI);
@@ -197,13 +196,13 @@ void SparcAsmPrinter::LowerGETPCXAndEmitMCInsts(const MachineInstr *MI,
197196
MCRegOP, OutContext, STI);
198197
break;
199198
case CodeModel::Medium: {
200-
EmitHiLo(*OutStreamer, GOTLabel, SparcMCExpr::VK_H44, SparcMCExpr::VK_M44,
199+
EmitHiLo(*OutStreamer, GOTLabel, ELF::R_SPARC_H44, ELF::R_SPARC_M44,
201200
MCRegOP, OutContext, STI);
202201
MCOperand imm = MCOperand::createExpr(MCConstantExpr::create(12,
203202
OutContext));
204203
EmitSHL(*OutStreamer, MCRegOP, imm, MCRegOP, STI);
205204
MCOperand lo =
206-
createSparcMCOperand(SparcMCExpr::VK_L44, GOTLabel, OutContext);
205+
createSparcMCOperand(ELF::R_SPARC_L44, GOTLabel, OutContext);
207206
EmitOR(*OutStreamer, MCRegOP, lo, MCRegOP, STI);
208207
break;
209208
}

llvm/lib/Target/Sparc/SparcISelLowering.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "SparcTargetObjectFile.h"
2121
#include "llvm/ADT/StringExtras.h"
2222
#include "llvm/ADT/StringSwitch.h"
23+
#include "llvm/BinaryFormat/ELF.h"
2324
#include "llvm/CodeGen/CallingConvLower.h"
2425
#include "llvm/CodeGen/MachineFrameInfo.h"
2526
#include "llvm/CodeGen/MachineFunction.h"
@@ -2204,10 +2205,9 @@ SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
22042205
return makeHiLoPair(Op, SparcMCExpr::VK_HI, SparcMCExpr::VK_LO, DAG);
22052206
case CodeModel::Medium: {
22062207
// abs44.
2207-
SDValue H44 =
2208-
makeHiLoPair(Op, SparcMCExpr::VK_H44, SparcMCExpr::VK_M44, DAG);
2208+
SDValue H44 = makeHiLoPair(Op, ELF::R_SPARC_H44, ELF::R_SPARC_M44, DAG);
22092209
H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, DL, MVT::i32));
2210-
SDValue L44 = withTargetFlags(Op, SparcMCExpr::VK_L44, DAG);
2210+
SDValue L44 = withTargetFlags(Op, ELF::R_SPARC_L44, DAG);
22112211
L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
22122212
return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
22132213
}

0 commit comments

Comments
 (0)