LLVM  9.0.0svn
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 }
65 
67 #define MCLOHCaseIdToName(Name) case MCLOH_ ## Name: return StringRef(#Name);
68  switch (Kind) {
69  MCLOHCaseIdToName(AdrpAdrp);
70  MCLOHCaseIdToName(AdrpLdr);
71  MCLOHCaseIdToName(AdrpAddLdr);
72  MCLOHCaseIdToName(AdrpLdrGotLdr);
73  MCLOHCaseIdToName(AdrpAddStr);
74  MCLOHCaseIdToName(AdrpLdrGotStr);
75  MCLOHCaseIdToName(AdrpAdd);
76  MCLOHCaseIdToName(AdrpLdrGot);
77  }
78  return StringRef();
79 }
80 
81 static inline int MCLOHIdToNbArgs(MCLOHType Kind) {
82  switch (Kind) {
83  // LOH with two arguments
84  case MCLOH_AdrpAdrp:
85  case MCLOH_AdrpLdr:
86  case MCLOH_AdrpAdd:
87  case MCLOH_AdrpLdrGot:
88  return 2;
89  // LOH with three arguments
90  case MCLOH_AdrpAddLdr:
92  case MCLOH_AdrpAddStr:
94  return 3;
95  }
96  return -1;
97 }
98 
99 /// Store Linker Optimization Hint information (LOH).
101  MCLOHType Kind;
102 
103  /// Arguments of this directive. Order matters.
105 
106  /// Emit this directive in \p OutStream using the information available
107  /// in the given \p ObjWriter and \p Layout to get the address of the
108  /// arguments within the object file.
109  void emit_impl(raw_ostream &OutStream, const MachObjectWriter &ObjWriter,
110  const MCAsmLayout &Layout) const;
111 
112 public:
114 
115  MCLOHDirective(MCLOHType Kind, const LOHArgs &Args)
116  : Kind(Kind), Args(Args.begin(), Args.end()) {
117  assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
118  }
119 
120  MCLOHType getKind() const { return Kind; }
121 
122  const LOHArgs &getArgs() const { return Args; }
123 
124  /// Emit this directive as:
125  /// <kind, numArgs, addr1, ..., addrN>
126  void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const;
127 
128  /// Get the size in bytes of this directive if emitted in \p ObjWriter with
129  /// the given \p Layout.
130  uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
131  const MCAsmLayout &Layout) const;
132 };
133 
135  /// Keep track of the emit size of all the LOHs.
136  mutable uint64_t EmitSize = 0;
137 
138  /// Keep track of all LOH directives.
140 
141 public:
143 
144  MCLOHContainer() = default;
145 
146  /// Const accessor to the directives.
147  const LOHDirectives &getDirectives() const {
148  return Directives;
149  }
150 
151  /// Add the directive of the given kind \p Kind with the given arguments
152  /// \p Args to the container.
154  Directives.push_back(MCLOHDirective(Kind, Args));
155  }
156 
157  /// Get the size of the directives if emitted.
158  uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
159  const MCAsmLayout &Layout) const {
160  if (!EmitSize) {
161  for (const MCLOHDirective &D : Directives)
162  EmitSize += D.getEmitSize(ObjWriter, Layout);
163  }
164  return EmitSize;
165  }
166 
167  /// Emit all Linker Optimization Hint in one big table.
168  /// Each line of the table is emitted by LOHDirective::emit.
169  void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const {
170  for (const MCLOHDirective &D : Directives)
171  D.emit(ObjWriter, Layout);
172  }
173 
174  void reset() {
175  Directives.clear();
176  EmitSize = 0;
177  }
178 };
179 
180 // Add types for specialized template using MCSymbol.
183 
184 } // end namespace llvm
185 
186 #endif // LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Emit this directive as: <kind, numArgs, addr1, ..., addrN>
Adrp _v -> Add _v -> Ldr.
Adrp _v -> Ldr _v -> Ldr.
Store Linker Optimization Hint information (LOH).
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
MCLOHDirective(MCLOHType Kind, const LOHArgs &Args)
#define MCLOHCaseIdToName(Name)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
uint64_t getEmitSize(const MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Get the size of the directives if emitted.
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.
#define MCLOHCaseNameToId(Name)
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Emit all Linker Optimization Hint in one big table.
static int MCLOHNameToId(StringRef Name)
MCLOHType
Linker Optimization Hint Type.
const LOHArgs & getArgs() const
Adrp _v -> Add _v -> Str.
void addDirective(MCLOHType Kind, const MCLOHDirective::LOHArgs &Args)
Add the directive of the given kind Kind with the given arguments Args to the container.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static StringRef MCLOHDirectiveName()
SmallVectorImpl< MCLOHDirective > LOHDirectives
SmallVectorImpl< MCSymbol * > LOHArgs
Adrp xY, _v1 -> Adrp xY, _v2.
static bool isValidMCLOHType(unsigned Kind)
Adrp _v -> Ldr _v -> Str.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static int MCLOHIdToNbArgs(MCLOHType Kind)
static StringRef MCLOHIdToName(MCLOHType Kind)
const LOHDirectives & getDirectives() const
Const accessor to the directives.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48