LLVM 22.0.0git
MCLinkerOptimizationHint.h
Go to the documentation of this file.
1//===- MCLinkerOptimizationHint.h - LOH interface ---------------*- C++ -*-===//
2//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares some helpers classes to handle Linker Optimization Hint
11// (LOH).
12//
13// FIXME: LOH interface supports only MachO format at the moment.
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
17#define LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
18
20#include "llvm/ADT/StringRef.h"
23#include <cassert>
24#include <cstdint>
25
26namespace llvm {
27
28class MachObjectWriter;
29class MCAssembler;
30class MCSymbol;
31class raw_ostream;
32
33/// Linker Optimization Hint Type.
35 MCLOH_AdrpAdrp = 0x1u, ///< Adrp xY, _v1@PAGE -> Adrp xY, _v2@PAGE.
36 MCLOH_AdrpLdr = 0x2u, ///< Adrp _v@PAGE -> Ldr _v@PAGEOFF.
37 MCLOH_AdrpAddLdr = 0x3u, ///< Adrp _v@PAGE -> Add _v@PAGEOFF -> Ldr.
38 MCLOH_AdrpLdrGotLdr = 0x4u, ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Ldr.
39 MCLOH_AdrpAddStr = 0x5u, ///< Adrp _v@PAGE -> Add _v@PAGEOFF -> Str.
40 MCLOH_AdrpLdrGotStr = 0x6u, ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Str.
41 MCLOH_AdrpAdd = 0x7u, ///< Adrp _v@PAGE -> Add _v@PAGEOFF.
42 MCLOH_AdrpLdrGot = 0x8u ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF.
43};
44
46 return StringRef(".loh");
47}
48
49static inline bool isValidMCLOHType(unsigned Kind) {
50 return Kind >= MCLOH_AdrpAdrp && Kind <= MCLOH_AdrpLdrGot;
51}
52
53static inline int MCLOHNameToId(StringRef Name) {
54#define MCLOHCaseNameToId(Name) .Case(#Name, MCLOH_ ## Name)
56 MCLOHCaseNameToId(AdrpAdrp)
57 MCLOHCaseNameToId(AdrpLdr)
58 MCLOHCaseNameToId(AdrpAddLdr)
59 MCLOHCaseNameToId(AdrpLdrGotLdr)
60 MCLOHCaseNameToId(AdrpAddStr)
61 MCLOHCaseNameToId(AdrpLdrGotStr)
62 MCLOHCaseNameToId(AdrpAdd)
63 MCLOHCaseNameToId(AdrpLdrGot)
64 .Default(-1);
65#undef MCLOHCaseNameToId
66}
67
68static inline StringRef MCLOHIdToName(MCLOHType Kind) {
69#define MCLOHCaseIdToName(Name) case MCLOH_ ## Name: return StringRef(#Name);
70 switch (Kind) {
71 MCLOHCaseIdToName(AdrpAdrp);
72 MCLOHCaseIdToName(AdrpLdr);
73 MCLOHCaseIdToName(AdrpAddLdr);
74 MCLOHCaseIdToName(AdrpLdrGotLdr);
75 MCLOHCaseIdToName(AdrpAddStr);
76 MCLOHCaseIdToName(AdrpLdrGotStr);
77 MCLOHCaseIdToName(AdrpAdd);
78 MCLOHCaseIdToName(AdrpLdrGot);
79 }
80 return StringRef();
81#undef MCLOHCaseIdToName
82}
83
84static inline int MCLOHIdToNbArgs(MCLOHType Kind) {
85 switch (Kind) {
86 // LOH with two arguments
87 case MCLOH_AdrpAdrp:
88 case MCLOH_AdrpLdr:
89 case MCLOH_AdrpAdd:
91 return 2;
92 // LOH with three arguments
97 return 3;
98 }
99 return -1;
100}
101
102/// Store Linker Optimization Hint information (LOH).
104 MCLOHType Kind;
105
106 /// Arguments of this directive. Order matters.
108
109 /// Emit this directive in \p OutStream using the information available
110 /// in the given \p ObjWriter and \p Layout to get the address of the
111 /// arguments within the object file.
112 void emit_impl(raw_ostream &OutStream,
113 const MachObjectWriter &ObjWriter) const;
114
115public:
117
119 : Kind(Kind), Args(Args.begin(), Args.end()) {
120 assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
121 }
122
123 MCLOHType getKind() const { return Kind; }
124
125 const LOHArgs &getArgs() const { return Args; }
126
127 /// Emit this directive as:
128 /// <kind, numArgs, addr1, ..., addrN>
129 LLVM_ABI void emit(const MCAssembler &Asm, MachObjectWriter &ObjWriter) const;
130
131 /// Get the size in bytes of this directive if emitted in \p ObjWriter with
132 /// the given \p Layout.
134 const MachObjectWriter &ObjWriter) const;
135};
136
138 /// Keep track of the emit size of all the LOHs.
139 mutable uint64_t EmitSize = 0;
140
141 /// Keep track of all LOH directives.
143
144public:
146
147 MCLOHContainer() = default;
148
149 /// Const accessor to the directives.
151 return Directives;
152 }
153
154 /// Add the directive of the given kind \p Kind with the given arguments
155 /// \p Args to the container.
157 Directives.push_back(MCLOHDirective(Kind, Args));
158 }
159
160 /// Get the size of the directives if emitted.
162 const MachObjectWriter &ObjWriter) const {
163 if (!EmitSize) {
164 for (const MCLOHDirective &D : Directives)
165 EmitSize += D.getEmitSize(Asm, ObjWriter);
166 }
167 return EmitSize;
168 }
169
170 /// Emit all Linker Optimization Hint in one big table.
171 /// Each line of the table is emitted by LOHDirective::emit.
172 void emit(const MCAssembler &Asm, MachObjectWriter &ObjWriter) const {
173 for (const MCLOHDirective &D : Directives)
174 D.emit(Asm, ObjWriter);
175 }
176
177 void reset() {
178 Directives.clear();
179 EmitSize = 0;
180 }
181};
182
183// Add types for specialized template using MCSymbol.
186
187} // end namespace llvm
188
189#endif // LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_ABI
Definition: Compiler.h:213
std::string Name
#define MCLOHCaseIdToName(Name)
#define MCLOHCaseNameToId(Name)
This file defines the SmallVector class.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
void emit(const MCAssembler &Asm, MachObjectWriter &ObjWriter) const
Emit all Linker Optimization Hint in one big table.
SmallVectorImpl< MCLOHDirective > LOHDirectives
MCLOHContainer()=default
const LOHDirectives & getDirectives() const
Const accessor to the directives.
uint64_t getEmitSize(const MCAssembler &Asm, const MachObjectWriter &ObjWriter) const
Get the size of the directives if emitted.
void addDirective(MCLOHType Kind, const MCLOHDirective::LOHArgs &Args)
Add the directive of the given kind Kind with the given arguments Args to the container.
Store Linker Optimization Hint information (LOH).
LLVM_ABI uint64_t getEmitSize(const MCAssembler &Asm, const MachObjectWriter &ObjWriter) const
Get the size in bytes of this directive if emitted in ObjWriter with the given Layout.
const LOHArgs & getArgs() const
LLVM_ABI void emit(const MCAssembler &Asm, MachObjectWriter &ObjWriter) const
Emit this directive as: <kind, numArgs, addr1, ..., addrN>
MCLOHDirective(MCLOHType Kind, const LOHArgs &Args)
SmallVectorImpl< MCSymbol * > LOHArgs
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
static int MCLOHNameToId(StringRef Name)
static StringRef MCLOHDirectiveName()
static bool isValidMCLOHType(unsigned Kind)
static int MCLOHIdToNbArgs(MCLOHType Kind)
MCLOHType
Linker Optimization Hint Type.
@ MCLOH_AdrpAddLdr
Adrp _v@PAGE -> Add _v@PAGEOFF -> Ldr.
@ MCLOH_AdrpLdrGotStr
Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Str.
@ MCLOH_AdrpLdrGotLdr
Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Ldr.
@ MCLOH_AdrpLdrGot
Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF.
@ MCLOH_AdrpLdr
Adrp _v@PAGE -> Ldr _v@PAGEOFF.
@ MCLOH_AdrpAdd
Adrp _v@PAGE -> Add _v@PAGEOFF.
@ MCLOH_AdrpAddStr
Adrp _v@PAGE -> Add _v@PAGEOFF -> Str.
@ MCLOH_AdrpAdrp
Adrp xY, _v1@PAGE -> Adrp xY, _v2@PAGE.
static StringRef MCLOHIdToName(MCLOHType Kind)