Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===-- llvm/Target/TargetLoweringObjectFile.h - Object Info ----*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements classes used to handle lowerings specific to common |
| 10 | // object file formats. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H |
| 15 | #define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H |
| 16 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | #include "llvm/MC/MCObjectFileInfo.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 18 | #include <cstdint> |
| 19 | |
| 20 | namespace llvm { |
| 21 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 22 | class Constant; |
| 23 | class DataLayout; |
| 24 | class Function; |
| 25 | class GlobalObject; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | class GlobalValue; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 27 | class MachineBasicBlock; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 28 | class MachineModuleInfo; |
| 29 | class Mangler; |
| 30 | class MCContext; |
| 31 | class MCExpr; |
| 32 | class MCSection; |
| 33 | class MCSymbol; |
| 34 | class MCSymbolRefExpr; |
| 35 | class MCStreamer; |
| 36 | class MCValue; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 37 | class Module; |
| 38 | class SectionKind; |
| 39 | class StringRef; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 40 | class TargetMachine; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 41 | class DSOLocalEquivalent; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | |
| 43 | class TargetLoweringObjectFile : public MCObjectFileInfo { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 44 | /// Name-mangler for global names. |
| 45 | Mangler *Mang = nullptr; |
| 46 | |
| 47 | protected: |
| 48 | bool SupportIndirectSymViaGOTPCRel = false; |
| 49 | bool SupportGOTPCRelWithOffset = true; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 50 | bool SupportDebugThreadLocalLocation = true; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 51 | bool SupportDSOLocalEquivalentLowering = false; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 52 | |
| 53 | /// PersonalityEncoding, LSDAEncoding, TTypeEncoding - Some encoding values |
| 54 | /// for EH. |
| 55 | unsigned PersonalityEncoding = 0; |
| 56 | unsigned LSDAEncoding = 0; |
| 57 | unsigned TTypeEncoding = 0; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 58 | unsigned CallSiteEncoding = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 59 | |
| 60 | /// This section contains the static constructor pointer list. |
| 61 | MCSection *StaticCtorSection = nullptr; |
| 62 | |
| 63 | /// This section contains the static destructor pointer list. |
| 64 | MCSection *StaticDtorSection = nullptr; |
| 65 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 66 | const TargetMachine *TM = nullptr; |
| 67 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 68 | public: |
| 69 | TargetLoweringObjectFile() = default; |
| 70 | TargetLoweringObjectFile(const TargetLoweringObjectFile &) = delete; |
| 71 | TargetLoweringObjectFile & |
| 72 | operator=(const TargetLoweringObjectFile &) = delete; |
| 73 | virtual ~TargetLoweringObjectFile(); |
| 74 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 75 | Mangler &getMangler() const { return *Mang; } |
| 76 | |
| 77 | /// This method must be called before any actual lowering is done. This |
| 78 | /// specifies the current context for codegen, and gives the lowering |
| 79 | /// implementations a chance to set up their default sections. |
| 80 | virtual void Initialize(MCContext &ctx, const TargetMachine &TM); |
| 81 | |
| 82 | virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, |
| 83 | const MCSymbol *Sym) const; |
| 84 | |
| 85 | /// Emit the module-level metadata that the platform cares about. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 86 | virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 87 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 88 | /// Emit Call Graph Profile metadata. |
| 89 | void emitCGProfileMetadata(MCStreamer &Streamer, Module &M) const; |
| 90 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 91 | /// Get the module-level metadata that the platform cares about. |
| 92 | virtual void getModuleMetadata(Module &M) {} |
| 93 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 94 | /// Given a constant with the SectionKind, return a section that it should be |
| 95 | /// placed in. |
| 96 | virtual MCSection *getSectionForConstant(const DataLayout &DL, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 97 | SectionKind Kind, const Constant *C, |
| 98 | Align &Alignment) const; |
| 99 | |
| 100 | virtual MCSection * |
| 101 | getSectionForMachineBasicBlock(const Function &F, |
| 102 | const MachineBasicBlock &MBB, |
| 103 | const TargetMachine &TM) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 104 | |
| 105 | /// Classify the specified global variable into a set of target independent |
| 106 | /// categories embodied in SectionKind. |
| 107 | static SectionKind getKindForGlobal(const GlobalObject *GO, |
| 108 | const TargetMachine &TM); |
| 109 | |
| 110 | /// This method computes the appropriate section to emit the specified global |
| 111 | /// variable or function definition. This should not be passed external (or |
| 112 | /// available externally) globals. |
| 113 | MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind, |
| 114 | const TargetMachine &TM) const; |
| 115 | |
| 116 | /// This method computes the appropriate section to emit the specified global |
| 117 | /// variable or function definition. This should not be passed external (or |
| 118 | /// available externally) globals. |
| 119 | MCSection *SectionForGlobal(const GlobalObject *GO, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 120 | const TargetMachine &TM) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 121 | |
| 122 | virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName, |
| 123 | const GlobalValue *GV, |
| 124 | const TargetMachine &TM) const; |
| 125 | |
| 126 | virtual MCSection *getSectionForJumpTable(const Function &F, |
| 127 | const TargetMachine &TM) const; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 128 | virtual MCSection *getSectionForLSDA(const Function &F, |
| 129 | const TargetMachine &TM) const { |
| 130 | return LSDASection; |
| 131 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 132 | |
| 133 | virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, |
| 134 | const Function &F) const; |
| 135 | |
| 136 | /// Targets should implement this method to assign a section to globals with |
| 137 | /// an explicit section specfied. The implementation of this method can |
| 138 | /// assume that GO->hasSection() is true. |
| 139 | virtual MCSection * |
| 140 | getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, |
| 141 | const TargetMachine &TM) const = 0; |
| 142 | |
| 143 | /// Return an MCExpr to use for a reference to the specified global variable |
| 144 | /// from exception handling information. |
| 145 | virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV, |
| 146 | unsigned Encoding, |
| 147 | const TargetMachine &TM, |
| 148 | MachineModuleInfo *MMI, |
| 149 | MCStreamer &Streamer) const; |
| 150 | |
| 151 | /// Return the MCSymbol for a private symbol with global value name as its |
| 152 | /// base, with the specified suffix. |
| 153 | MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV, |
| 154 | StringRef Suffix, |
| 155 | const TargetMachine &TM) const; |
| 156 | |
| 157 | // The symbol that gets passed to .cfi_personality. |
| 158 | virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV, |
| 159 | const TargetMachine &TM, |
| 160 | MachineModuleInfo *MMI) const; |
| 161 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 162 | unsigned getPersonalityEncoding() const { return PersonalityEncoding; } |
| 163 | unsigned getLSDAEncoding() const { return LSDAEncoding; } |
| 164 | unsigned getTTypeEncoding() const { return TTypeEncoding; } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 165 | unsigned getCallSiteEncoding() const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 166 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 167 | const MCExpr *getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, |
| 168 | MCStreamer &Streamer) const; |
| 169 | |
| 170 | virtual MCSection *getStaticCtorSection(unsigned Priority, |
| 171 | const MCSymbol *KeySym) const { |
| 172 | return StaticCtorSection; |
| 173 | } |
| 174 | |
| 175 | virtual MCSection *getStaticDtorSection(unsigned Priority, |
| 176 | const MCSymbol *KeySym) const { |
| 177 | return StaticDtorSection; |
| 178 | } |
| 179 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 180 | /// Create a symbol reference to describe the given TLS variable when |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 181 | /// emitting the address in debug info. |
| 182 | virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const; |
| 183 | |
| 184 | virtual const MCExpr *lowerRelativeReference(const GlobalValue *LHS, |
| 185 | const GlobalValue *RHS, |
| 186 | const TargetMachine &TM) const { |
| 187 | return nullptr; |
| 188 | } |
| 189 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 190 | /// Target supports a native lowering of a dso_local_equivalent constant |
| 191 | /// without needing to replace it with equivalent IR. |
| 192 | bool supportDSOLocalEquivalentLowering() const { |
| 193 | return SupportDSOLocalEquivalentLowering; |
| 194 | } |
| 195 | |
| 196 | virtual const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv, |
| 197 | const TargetMachine &TM) const { |
| 198 | return nullptr; |
| 199 | } |
| 200 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 201 | /// Target supports replacing a data "PC"-relative access to a symbol |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 202 | /// through another symbol, by accessing the later via a GOT entry instead? |
| 203 | bool supportIndirectSymViaGOTPCRel() const { |
| 204 | return SupportIndirectSymViaGOTPCRel; |
| 205 | } |
| 206 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 207 | /// Target GOT "PC"-relative relocation supports encoding an additional |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 208 | /// binary expression with an offset? |
| 209 | bool supportGOTPCRelWithOffset() const { |
| 210 | return SupportGOTPCRelWithOffset; |
| 211 | } |
| 212 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 213 | /// Target supports TLS offset relocation in debug section? |
| 214 | bool supportDebugThreadLocalLocation() const { |
| 215 | return SupportDebugThreadLocalLocation; |
| 216 | } |
| 217 | |
| 218 | /// Get the target specific PC relative GOT entry relocation |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 219 | virtual const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV, |
| 220 | const MCSymbol *Sym, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 221 | const MCValue &MV, |
| 222 | int64_t Offset, |
| 223 | MachineModuleInfo *MMI, |
| 224 | MCStreamer &Streamer) const { |
| 225 | return nullptr; |
| 226 | } |
| 227 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 228 | /// If supported, return the section to use for the llvm.commandline |
| 229 | /// metadata. Otherwise, return nullptr. |
| 230 | virtual MCSection *getSectionForCommandLines() const { |
| 231 | return nullptr; |
| 232 | } |
| 233 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 234 | /// On targets that use separate function descriptor symbols, return a section |
| 235 | /// for the descriptor given its symbol. Use only with defined functions. |
| 236 | virtual MCSection * |
| 237 | getSectionForFunctionDescriptor(const Function *F, |
| 238 | const TargetMachine &TM) const { |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
| 242 | /// On targets that support TOC entries, return a section for the entry given |
| 243 | /// the symbol it refers to. |
| 244 | /// TODO: Implement this interface for existing ELF targets. |
| 245 | virtual MCSection *getSectionForTOCEntry(const MCSymbol *S, |
| 246 | const TargetMachine &TM) const { |
| 247 | return nullptr; |
| 248 | } |
| 249 | |
| 250 | /// On targets that associate external references with a section, return such |
| 251 | /// a section for the given external global. |
| 252 | virtual MCSection * |
| 253 | getSectionForExternalReference(const GlobalObject *GO, |
| 254 | const TargetMachine &TM) const { |
| 255 | return nullptr; |
| 256 | } |
| 257 | |
| 258 | /// Targets that have a special convention for their symbols could use |
| 259 | /// this hook to return a specialized symbol. |
| 260 | virtual MCSymbol *getTargetSymbol(const GlobalValue *GV, |
| 261 | const TargetMachine &TM) const { |
| 262 | return nullptr; |
| 263 | } |
| 264 | |
| 265 | /// If supported, return the function entry point symbol. |
| 266 | /// Otherwise, returns nulltpr. |
| 267 | /// Func must be a function or an alias which has a function as base object. |
| 268 | virtual MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func, |
| 269 | const TargetMachine &TM) const { |
| 270 | return nullptr; |
| 271 | } |
| 272 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 273 | protected: |
| 274 | virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO, |
| 275 | SectionKind Kind, |
| 276 | const TargetMachine &TM) const = 0; |
| 277 | }; |
| 278 | |
| 279 | } // end namespace llvm |
| 280 | |
| 281 | #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H |