LLVM  14.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 
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/StringSwitch.h"
23 #include <cassert>
24 #include <cstdint>
25 
26 namespace llvm {
27 
28 class MachObjectWriter;
29 class MCAsmLayout;
30 class MCSymbol;
31 
32 /// Linker Optimization Hint Type.
33 enum MCLOHType {
34  MCLOH_AdrpAdrp = 0x1u, ///< Adrp xY, _v1@PAGE -> Adrp xY, _v2@PAGE.
35  MCLOH_AdrpLdr = 0x2u, ///< Adrp _v@PAGE -> Ldr _v@PAGEOFF.
36  MCLOH_AdrpAddLdr = 0x3u, ///< Adrp _v@PAGE -> Add _v@PAGEOFF -> Ldr.
37  MCLOH_AdrpLdrGotLdr = 0x4u, ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Ldr.
38  MCLOH_AdrpAddStr = 0x5u, ///< Adrp _v@PAGE -> Add _v@PAGEOFF -> Str.
39  MCLOH_AdrpLdrGotStr = 0x6u, ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Str.
40  MCLOH_AdrpAdd = 0x7u, ///< Adrp _v@PAGE -> Add _v@PAGEOFF.
41  MCLOH_AdrpLdrGot = 0x8u ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF.
42 };
43 
44 static inline StringRef MCLOHDirectiveName() {
45  return StringRef(".loh");
46 }
47 
48 static inline bool isValidMCLOHType(unsigned Kind) {
49  return Kind >= MCLOH_AdrpAdrp && Kind <= MCLOH_AdrpLdrGot;
50 }
51 
52 static inline int MCLOHNameToId(StringRef Name) {
53 #define MCLOHCaseNameToId(Name) .Case(#Name, MCLOH_ ## Name)
54  return StringSwitch<int>(Name)
55  MCLOHCaseNameToId(AdrpAdrp)
56  MCLOHCaseNameToId(AdrpLdr)
57  MCLOHCaseNameToId(AdrpAddLdr)
58  MCLOHCaseNameToId(AdrpLdrGotLdr)
59  MCLOHCaseNameToId(AdrpAddStr)
60  MCLOHCaseNameToId(AdrpLdrGotStr)
61  MCLOHCaseNameToId(AdrpAdd)
62  MCLOHCaseNameToId(AdrpLdrGot)
63  .Default(-1);
64 #undef MCLOHCaseNameToId
65 }
66 
68 #define MCLOHCaseIdToName(Name) case MCLOH_ ## Name: return StringRef(#Name);
69  switch (Kind) {
70  MCLOHCaseIdToName(AdrpAdrp);
71  MCLOHCaseIdToName(AdrpLdr);
72  MCLOHCaseIdToName(AdrpAddLdr);
73  MCLOHCaseIdToName(AdrpLdrGotLdr);
74  MCLOHCaseIdToName(AdrpAddStr);
75  MCLOHCaseIdToName(AdrpLdrGotStr);
76  MCLOHCaseIdToName(AdrpAdd);
77  MCLOHCaseIdToName(AdrpLdrGot);
78  }
79  return StringRef();
80 #undef MCLOHCaseIdToName
81 }
82 
83 static inline int MCLOHIdToNbArgs(MCLOHType Kind) {
84  switch (Kind) {
85  // LOH with two arguments
86  case MCLOH_AdrpAdrp:
87  case MCLOH_AdrpLdr:
88  case MCLOH_AdrpAdd:
89  case MCLOH_AdrpLdrGot:
90  return 2;
91  // LOH with three arguments
92  case MCLOH_AdrpAddLdr:
94  case MCLOH_AdrpAddStr:
96  return 3;
97  }
98  return -1;
99 }
100 
101 /// Store Linker Optimization Hint information (LOH).
103  MCLOHType Kind;
104 
105  /// Arguments of this directive. Order matters.
107 
108  /// Emit this directive in \p OutStream using the information available
109  /// in the given \p ObjWriter and \p Layout to get the address of the
110  /// arguments within the object file.
111  void emit_impl(raw_ostream &OutStream, const MachObjectWriter &ObjWriter,
112  const MCAsmLayout &Layout) const;
113 
114 public:
116 
117  MCLOHDirective(MCLOHType Kind, const LOHArgs &Args)
118  : Kind(Kind), Args(Args.begin(), Args.end()) {
119  assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
120  }
121 
122  MCLOHType getKind() const { return Kind; }
123 
124  const LOHArgs &getArgs() const { return Args; }
125 
126  /// Emit this directive as:
127  /// <kind, numArgs, addr1, ..., addrN>
128  void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const;
129 
130  /// Get the size in bytes of this directive if emitted in \p ObjWriter with
131  /// the given \p Layout.
132  uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
133  const MCAsmLayout &Layout) const;
134 };
135 
137  /// Keep track of the emit size of all the LOHs.
138  mutable uint64_t EmitSize = 0;
139 
140  /// Keep track of all LOH directives.
142 
143 public:
145 
146  MCLOHContainer() = default;
147 
148  /// Const accessor to the directives.
149  const LOHDirectives &getDirectives() const {
150  return Directives;
151  }
152 
153  /// Add the directive of the given kind \p Kind with the given arguments
154  /// \p Args to the container.
156  Directives.push_back(MCLOHDirective(Kind, Args));
157  }
158 
159  /// Get the size of the directives if emitted.
161  const MCAsmLayout &Layout) const {
162  if (!EmitSize) {
163  for (const MCLOHDirective &D : Directives)
164  EmitSize += D.getEmitSize(ObjWriter, Layout);
165  }
166  return EmitSize;
167  }
168 
169  /// Emit all Linker Optimization Hint in one big table.
170  /// Each line of the table is emitted by LOHDirective::emit.
171  void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const {
172  for (const MCLOHDirective &D : Directives)
173  D.emit(ObjWriter, Layout);
174  }
175 
176  void reset() {
177  Directives.clear();
178  EmitSize = 0;
179  }
180 };
181 
182 // Add types for specialized template using MCSymbol.
185 
186 } // end namespace llvm
187 
188 #endif // LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MCLOHDirective::getArgs
const LOHArgs & getArgs() const
Definition: MCLinkerOptimizationHint.h:124
llvm::MCLOHType
MCLOHType
Linker Optimization Hint Type.
Definition: MCLinkerOptimizationHint.h:33
StringRef.h
llvm::MCLOHDirective::LOHArgs
SmallVectorImpl< MCSymbol * > LOHArgs
Definition: MCLinkerOptimizationHint.h:115
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::MCLOHContainer::emit
void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Emit all Linker Optimization Hint in one big table.
Definition: MCLinkerOptimizationHint.h:171
llvm::MCLOH_AdrpAddLdr
@ MCLOH_AdrpAddLdr
Adrp _v@PAGE -> Add _v@PAGEOFF -> Ldr.
Definition: MCLinkerOptimizationHint.h:36
llvm::MCLOHContainer::addDirective
void addDirective(MCLOHType Kind, const MCLOHDirective::LOHArgs &Args)
Add the directive of the given kind Kind with the given arguments Args to the container.
Definition: MCLinkerOptimizationHint.h:155
llvm::MCLOHDirectiveName
static StringRef MCLOHDirectiveName()
Definition: MCLinkerOptimizationHint.h:44
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
llvm::MCLOH_AdrpLdrGotLdr
@ MCLOH_AdrpLdrGotLdr
Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Ldr.
Definition: MCLinkerOptimizationHint.h:37
llvm::MCLOHIdToName
static StringRef MCLOHIdToName(MCLOHType Kind)
Definition: MCLinkerOptimizationHint.h:67
llvm::MCLOHContainer
Definition: MCLinkerOptimizationHint.h:136
llvm::MCLOHContainer::LOHDirectives
SmallVectorImpl< MCLOHDirective > LOHDirectives
Definition: MCLinkerOptimizationHint.h:144
MCLOHCaseNameToId
#define MCLOHCaseNameToId(Name)
llvm::MachObjectWriter
Definition: MCMachObjectWriter.h:84
llvm::MCLOHContainer::reset
void reset()
Definition: MCLinkerOptimizationHint.h:176
llvm::MCLOHContainer::getDirectives
const LOHDirectives & getDirectives() const
Const accessor to the directives.
Definition: MCLinkerOptimizationHint.h:149
llvm::isValidMCLOHType
static bool isValidMCLOHType(unsigned Kind)
Definition: MCLinkerOptimizationHint.h:48
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
MCLOHCaseIdToName
#define MCLOHCaseIdToName(Name)
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
uint64_t
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::MCLOHDirective
Store Linker Optimization Hint information (LOH).
Definition: MCLinkerOptimizationHint.h:102
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCLOH_AdrpAddStr
@ MCLOH_AdrpAddStr
Adrp _v@PAGE -> Add _v@PAGEOFF -> Str.
Definition: MCLinkerOptimizationHint.h:38
llvm::MCLOHNameToId
static int MCLOHNameToId(StringRef Name)
Definition: MCLinkerOptimizationHint.h:52
llvm::MCLOHDirective::emit
void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Emit this directive as: <kind, numArgs, addr1, ..., addrN>
Definition: MCLinkerOptimizationHint.cpp:36
llvm::MCLOHIdToNbArgs
static int MCLOHIdToNbArgs(MCLOHType Kind)
Definition: MCLinkerOptimizationHint.h:83
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MCLOHDirective::MCLOHDirective
MCLOHDirective(MCLOHType Kind, const LOHArgs &Args)
Definition: MCLinkerOptimizationHint.h:117
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::MCLOH_AdrpAdrp
@ MCLOH_AdrpAdrp
Adrp xY, _v1@PAGE -> Adrp xY, _v2@PAGE.
Definition: MCLinkerOptimizationHint.h:34
StringSwitch.h
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:585
llvm::MCLOH_AdrpAdd
@ MCLOH_AdrpAdd
Adrp _v@PAGE -> Add _v@PAGEOFF.
Definition: MCLinkerOptimizationHint.h:40
llvm::MCLOH_AdrpLdr
@ MCLOH_AdrpLdr
Adrp _v@PAGE -> Ldr _v@PAGEOFF.
Definition: MCLinkerOptimizationHint.h:35
llvm::MCLOH_AdrpLdrGotStr
@ MCLOH_AdrpLdrGotStr
Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Str.
Definition: MCLinkerOptimizationHint.h:39
SmallVector.h
llvm::MCLOHDirective::getEmitSize
uint64_t getEmitSize(const MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Get the size in bytes of this directive if emitted in ObjWriter with the given Layout.
Definition: MCLinkerOptimizationHint.cpp:42
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
llvm::MCLOHContainer::MCLOHContainer
MCLOHContainer()=default
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::MCLOH_AdrpLdrGot
@ MCLOH_AdrpLdrGot
Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF.
Definition: MCLinkerOptimizationHint.h:41
llvm::MCLOHDirective::getKind
MCLOHType getKind() const
Definition: MCLinkerOptimizationHint.h:122
raw_ostream.h
llvm::MCLOHContainer::getEmitSize
uint64_t getEmitSize(const MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Get the size of the directives if emitted.
Definition: MCLinkerOptimizationHint.h:160
llvm::ISD::MCSymbol
@ MCSymbol
Definition: ISDOpcodes.h:172