Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / lib / MC / MCAsmBackend.cpp
blob4b1064a07e83c52823cedb84c823aebd32e07a08
1 //===- MCAsmBackend.cpp - Target MC Assembly Backend ----------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCAsmBackend.h"
10 #include "llvm/MC/MCDXContainerWriter.h"
11 #include "llvm/MC/MCELFObjectWriter.h"
12 #include "llvm/MC/MCFixupKindInfo.h"
13 #include "llvm/MC/MCGOFFObjectWriter.h"
14 #include "llvm/MC/MCMachObjectWriter.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCSPIRVObjectWriter.h"
17 #include "llvm/MC/MCWasmObjectWriter.h"
18 #include "llvm/MC/MCWinCOFFObjectWriter.h"
19 #include "llvm/MC/MCXCOFFObjectWriter.h"
20 #include <cassert>
21 #include <cstddef>
22 #include <cstdint>
24 using namespace llvm;
26 MCAsmBackend::MCAsmBackend(llvm::endianness Endian, unsigned RelaxFixupKind)
27 : Endian(Endian), RelaxFixupKind(RelaxFixupKind) {}
29 MCAsmBackend::~MCAsmBackend() = default;
31 std::unique_ptr<MCObjectWriter>
32 MCAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
33 auto TW = createObjectTargetWriter();
34 switch (TW->getFormat()) {
35 case Triple::ELF:
36 return createELFObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)),
37 OS, Endian == llvm::endianness::little);
38 case Triple::MachO:
39 return createMachObjectWriter(cast<MCMachObjectTargetWriter>(std::move(TW)),
40 OS, Endian == llvm::endianness::little);
41 case Triple::COFF:
42 return createWinCOFFObjectWriter(
43 cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS);
44 case Triple::SPIRV:
45 return createSPIRVObjectWriter(
46 cast<MCSPIRVObjectTargetWriter>(std::move(TW)), OS);
47 case Triple::Wasm:
48 return createWasmObjectWriter(cast<MCWasmObjectTargetWriter>(std::move(TW)),
49 OS);
50 case Triple::GOFF:
51 return createGOFFObjectWriter(cast<MCGOFFObjectTargetWriter>(std::move(TW)),
52 OS);
53 case Triple::XCOFF:
54 return createXCOFFObjectWriter(
55 cast<MCXCOFFObjectTargetWriter>(std::move(TW)), OS);
56 case Triple::DXContainer:
57 return createDXContainerObjectWriter(
58 cast<MCDXContainerTargetWriter>(std::move(TW)), OS);
59 default:
60 llvm_unreachable("unexpected object format");
64 std::unique_ptr<MCObjectWriter>
65 MCAsmBackend::createDwoObjectWriter(raw_pwrite_stream &OS,
66 raw_pwrite_stream &DwoOS) const {
67 auto TW = createObjectTargetWriter();
68 switch (TW->getFormat()) {
69 case Triple::COFF:
70 return createWinCOFFDwoObjectWriter(
71 cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS, DwoOS);
72 case Triple::ELF:
73 return createELFDwoObjectWriter(
74 cast<MCELFObjectTargetWriter>(std::move(TW)), OS, DwoOS,
75 Endian == llvm::endianness::little);
76 case Triple::Wasm:
77 return createWasmDwoObjectWriter(
78 cast<MCWasmObjectTargetWriter>(std::move(TW)), OS, DwoOS);
79 default:
80 report_fatal_error("dwo only supported with COFF, ELF, and Wasm");
84 std::optional<MCFixupKind> MCAsmBackend::getFixupKind(StringRef Name) const {
85 return std::nullopt;
88 const MCFixupKindInfo &MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
89 static const MCFixupKindInfo Builtins[] = {
90 {"FK_NONE", 0, 0, 0},
91 {"FK_Data_1", 0, 8, 0},
92 {"FK_Data_2", 0, 16, 0},
93 {"FK_Data_4", 0, 32, 0},
94 {"FK_Data_8", 0, 64, 0},
95 {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel},
96 {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
97 {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
98 {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel},
99 {"FK_GPRel_1", 0, 8, 0},
100 {"FK_GPRel_2", 0, 16, 0},
101 {"FK_GPRel_4", 0, 32, 0},
102 {"FK_GPRel_8", 0, 64, 0},
103 {"FK_DTPRel_4", 0, 32, 0},
104 {"FK_DTPRel_8", 0, 64, 0},
105 {"FK_TPRel_4", 0, 32, 0},
106 {"FK_TPRel_8", 0, 64, 0},
107 {"FK_SecRel_1", 0, 8, 0},
108 {"FK_SecRel_2", 0, 16, 0},
109 {"FK_SecRel_4", 0, 32, 0},
110 {"FK_SecRel_8", 0, 64, 0},
113 assert((size_t)Kind <= std::size(Builtins) && "Unknown fixup kind");
114 return Builtins[Kind];
117 bool MCAsmBackend::fixupNeedsRelaxationAdvanced(
118 const MCFixup &Fixup, bool Resolved, uint64_t Value,
119 const MCRelaxableFragment *DF, const MCAsmLayout &Layout,
120 const bool WasForced) const {
121 if (!Resolved)
122 return true;
123 return fixupNeedsRelaxation(Fixup, Value, DF, Layout);
126 bool MCAsmBackend::isDarwinCanonicalPersonality(const MCSymbol *Sym) const {
127 // Consider a NULL personality (ie., no personality encoding) to be canonical
128 // because it's always at 0.
129 if (!Sym)
130 return true;
132 if (!Sym->isMachO())
133 llvm_unreachable("Expected MachO symbols only");
135 StringRef name = Sym->getName();
136 // XXX: We intentionally leave out "___gcc_personality_v0" because, despite
137 // being system-defined like these two, it is not very commonly-used.
138 // Reserving an empty slot for it seems silly.
139 return name == "___gxx_personality_v0" || name == "___objc_personality_v0";