[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / lib / Target / RISCV / MCTargetDesc / RISCVMCExpr.cpp
blobae25ec8181710f431b1140b0abeaefa93c886318
1 //===-- RISCVMCExpr.cpp - RISCV specific MC expression classes ------------===//
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 // This file contains the implementation of the assembly expression modifiers
10 // accepted by the RISCV architecture (e.g. ":lo12:", ":gottprel_g1:", ...).
12 //===----------------------------------------------------------------------===//
14 #include "RISCVMCExpr.h"
15 #include "MCTargetDesc/RISCVAsmBackend.h"
16 #include "RISCV.h"
17 #include "RISCVFixupKinds.h"
18 #include "llvm/BinaryFormat/ELF.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSymbolELF.h"
24 #include "llvm/MC/MCValue.h"
25 #include "llvm/Support/ErrorHandling.h"
27 using namespace llvm;
29 #define DEBUG_TYPE "riscvmcexpr"
31 const RISCVMCExpr *RISCVMCExpr::create(const MCExpr *Expr, VariantKind Kind,
32 MCContext &Ctx) {
33 return new (Ctx) RISCVMCExpr(Expr, Kind);
36 void RISCVMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
37 VariantKind Kind = getKind();
38 bool HasVariant = ((Kind != VK_RISCV_None) && (Kind != VK_RISCV_CALL) &&
39 (Kind != VK_RISCV_CALL_PLT));
41 if (HasVariant)
42 OS << '%' << getVariantKindName(getKind()) << '(';
43 Expr->print(OS, MAI);
44 if (Kind == VK_RISCV_CALL_PLT)
45 OS << "@plt";
46 if (HasVariant)
47 OS << ')';
50 const MCFixup *RISCVMCExpr::getPCRelHiFixup() const {
51 MCValue AUIPCLoc;
52 if (!getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr, nullptr))
53 return nullptr;
55 const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
56 if (!AUIPCSRE)
57 return nullptr;
59 const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
60 const auto *DF = dyn_cast_or_null<MCDataFragment>(AUIPCSymbol->getFragment());
62 if (!DF)
63 return nullptr;
65 uint64_t Offset = AUIPCSymbol->getOffset();
66 if (DF->getContents().size() == Offset) {
67 DF = dyn_cast_or_null<MCDataFragment>(DF->getNextNode());
68 if (!DF)
69 return nullptr;
70 Offset = 0;
73 for (const MCFixup &F : DF->getFixups()) {
74 if (F.getOffset() != Offset)
75 continue;
77 switch ((unsigned)F.getKind()) {
78 default:
79 continue;
80 case RISCV::fixup_riscv_got_hi20:
81 case RISCV::fixup_riscv_tls_got_hi20:
82 case RISCV::fixup_riscv_tls_gd_hi20:
83 case RISCV::fixup_riscv_pcrel_hi20:
84 return &F;
88 return nullptr;
91 bool RISCVMCExpr::evaluatePCRelLo(MCValue &Res, const MCAsmLayout *Layout,
92 const MCFixup *Fixup) const {
93 // VK_RISCV_PCREL_LO has to be handled specially. The MCExpr inside is
94 // actually the location of a auipc instruction with a VK_RISCV_PCREL_HI fixup
95 // pointing to the real target. We need to generate an MCValue in the form of
96 // (<real target> + <offset from this fixup to the auipc fixup>). The Fixup
97 // is pcrel relative to the VK_RISCV_PCREL_LO fixup, so we need to add the
98 // offset to the VK_RISCV_PCREL_HI Fixup from VK_RISCV_PCREL_LO to correct.
100 // Don't try to evaluate if the fixup will be forced as a relocation (e.g.
101 // as linker relaxation is enabled). If we evaluated pcrel_lo in this case,
102 // the modified fixup will be converted into a relocation that no longer
103 // points to the pcrel_hi as the linker requires.
104 auto &RAB =
105 static_cast<RISCVAsmBackend &>(Layout->getAssembler().getBackend());
106 if (RAB.willForceRelocations())
107 return false;
109 MCValue AUIPCLoc;
110 if (!getSubExpr()->evaluateAsValue(AUIPCLoc, *Layout))
111 return false;
113 const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
114 // Don't try to evaluate %pcrel_hi/%pcrel_lo pairs that cross fragment
115 // boundries.
116 if (!AUIPCSRE ||
117 findAssociatedFragment() != AUIPCSRE->findAssociatedFragment())
118 return false;
120 const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
121 if (!AUIPCSymbol)
122 return false;
124 const MCFixup *TargetFixup = getPCRelHiFixup();
125 if (!TargetFixup)
126 return false;
128 if ((unsigned)TargetFixup->getKind() != RISCV::fixup_riscv_pcrel_hi20)
129 return false;
131 MCValue Target;
132 if (!TargetFixup->getValue()->evaluateAsValue(Target, *Layout))
133 return false;
135 if (!Target.getSymA() || !Target.getSymA()->getSymbol().isInSection())
136 return false;
138 if (&Target.getSymA()->getSymbol().getSection() !=
139 findAssociatedFragment()->getParent())
140 return false;
142 uint64_t AUIPCOffset = AUIPCSymbol->getOffset();
144 Res = MCValue::get(Target.getSymA(), nullptr,
145 Target.getConstant() + (Fixup->getOffset() - AUIPCOffset));
146 return true;
149 bool RISCVMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
150 const MCAsmLayout *Layout,
151 const MCFixup *Fixup) const {
152 if (Kind == VK_RISCV_PCREL_LO && evaluatePCRelLo(Res, Layout, Fixup))
153 return true;
155 if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
156 return false;
158 // Some custom fixup types are not valid with symbol difference expressions
159 if (Res.getSymA() && Res.getSymB()) {
160 switch (getKind()) {
161 default:
162 return true;
163 case VK_RISCV_LO:
164 case VK_RISCV_HI:
165 case VK_RISCV_PCREL_LO:
166 case VK_RISCV_PCREL_HI:
167 case VK_RISCV_GOT_HI:
168 case VK_RISCV_TPREL_LO:
169 case VK_RISCV_TPREL_HI:
170 case VK_RISCV_TPREL_ADD:
171 case VK_RISCV_TLS_GOT_HI:
172 case VK_RISCV_TLS_GD_HI:
173 return false;
177 return true;
180 void RISCVMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
181 Streamer.visitUsedExpr(*getSubExpr());
184 RISCVMCExpr::VariantKind RISCVMCExpr::getVariantKindForName(StringRef name) {
185 return StringSwitch<RISCVMCExpr::VariantKind>(name)
186 .Case("lo", VK_RISCV_LO)
187 .Case("hi", VK_RISCV_HI)
188 .Case("pcrel_lo", VK_RISCV_PCREL_LO)
189 .Case("pcrel_hi", VK_RISCV_PCREL_HI)
190 .Case("got_pcrel_hi", VK_RISCV_GOT_HI)
191 .Case("tprel_lo", VK_RISCV_TPREL_LO)
192 .Case("tprel_hi", VK_RISCV_TPREL_HI)
193 .Case("tprel_add", VK_RISCV_TPREL_ADD)
194 .Case("tls_ie_pcrel_hi", VK_RISCV_TLS_GOT_HI)
195 .Case("tls_gd_pcrel_hi", VK_RISCV_TLS_GD_HI)
196 .Default(VK_RISCV_Invalid);
199 StringRef RISCVMCExpr::getVariantKindName(VariantKind Kind) {
200 switch (Kind) {
201 default:
202 llvm_unreachable("Invalid ELF symbol kind");
203 case VK_RISCV_LO:
204 return "lo";
205 case VK_RISCV_HI:
206 return "hi";
207 case VK_RISCV_PCREL_LO:
208 return "pcrel_lo";
209 case VK_RISCV_PCREL_HI:
210 return "pcrel_hi";
211 case VK_RISCV_GOT_HI:
212 return "got_pcrel_hi";
213 case VK_RISCV_TPREL_LO:
214 return "tprel_lo";
215 case VK_RISCV_TPREL_HI:
216 return "tprel_hi";
217 case VK_RISCV_TPREL_ADD:
218 return "tprel_add";
219 case VK_RISCV_TLS_GOT_HI:
220 return "tls_ie_pcrel_hi";
221 case VK_RISCV_TLS_GD_HI:
222 return "tls_gd_pcrel_hi";
226 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
227 switch (Expr->getKind()) {
228 case MCExpr::Target:
229 llvm_unreachable("Can't handle nested target expression");
230 break;
231 case MCExpr::Constant:
232 break;
234 case MCExpr::Binary: {
235 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
236 fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
237 fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
238 break;
241 case MCExpr::SymbolRef: {
242 // We're known to be under a TLS fixup, so any symbol should be
243 // modified. There should be only one.
244 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
245 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
246 break;
249 case MCExpr::Unary:
250 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
251 break;
255 void RISCVMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
256 switch (getKind()) {
257 default:
258 return;
259 case VK_RISCV_TPREL_HI:
260 case VK_RISCV_TLS_GOT_HI:
261 case VK_RISCV_TLS_GD_HI:
262 break;
265 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
268 bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
269 MCValue Value;
271 if (Kind == VK_RISCV_PCREL_HI || Kind == VK_RISCV_PCREL_LO ||
272 Kind == VK_RISCV_GOT_HI || Kind == VK_RISCV_TPREL_HI ||
273 Kind == VK_RISCV_TPREL_LO || Kind == VK_RISCV_TPREL_ADD ||
274 Kind == VK_RISCV_TLS_GOT_HI || Kind == VK_RISCV_TLS_GD_HI ||
275 Kind == VK_RISCV_CALL || Kind == VK_RISCV_CALL_PLT)
276 return false;
278 if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr))
279 return false;
281 if (!Value.isAbsolute())
282 return false;
284 Res = evaluateAsInt64(Value.getConstant());
285 return true;
288 int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value) const {
289 switch (Kind) {
290 default:
291 llvm_unreachable("Invalid kind");
292 case VK_RISCV_LO:
293 return SignExtend64<12>(Value);
294 case VK_RISCV_HI:
295 // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
296 return ((Value + 0x800) >> 12) & 0xfffff;