07016c959fdb1e197596f9add28759094da9b330
[oota-llvm.git] / include / llvm / MC / MCLinkerOptimizationHint.h
1 //===- MCLinkerOptimizationHint.h - LOH interface ---------------*- C++ -*-===//
2 //
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file declares some helpers classes to handle Linker Optimization Hint
12 // (LOH).
13 //
14 // FIXME: LOH interface supports only MachO format at the moment.
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
18 #define LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
19
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/MC/MCMachObjectWriter.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 namespace llvm {
27
28 // Forward declarations.
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(MCLOHType 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
66 static inline StringRef MCLOHIdToName(MCLOHType Kind) {
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:
91   case MCLOH_AdrpLdrGotLdr:
92   case MCLOH_AdrpAddStr:
93   case MCLOH_AdrpLdrGotStr:
94     return 3;
95   }
96   return -1;
97 }
98
99 namespace {
100 class raw_counting_ostream : public raw_ostream {
101   uint64_t Count = 0;
102
103   void write_impl(const char *, size_t size) override { Count += size; }
104
105   uint64_t current_pos() const override { return Count; }
106
107 public:
108   ~raw_counting_ostream() { flush(); }
109 };
110 }
111
112 /// Store Linker Optimization Hint information (LOH).
113 class MCLOHDirective {
114   MCLOHType Kind;
115
116   /// Arguments of this directive. Order matters.
117   SmallVector<MCSymbol *, 3> Args;
118
119   /// Emit this directive in @p OutStream using the information available
120   /// in the given @p ObjWriter and @p Layout to get the address of the
121   /// arguments within the object file.
122   void Emit_impl(raw_ostream &OutStream, const MachObjectWriter &ObjWriter,
123                  const MCAsmLayout &Layout) const;
124
125 public:
126   typedef SmallVectorImpl<MCSymbol *> LOHArgs;
127
128   MCLOHDirective(MCLOHType Kind, const LOHArgs &Args)
129       : Kind(Kind), Args(Args.begin(), Args.end()) {
130     assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
131   }
132
133   MCLOHType getKind() const { return Kind; }
134
135   const LOHArgs &getArgs() const { return Args; }
136
137   /// Emit this directive as:
138   /// <kind, numArgs, addr1, ..., addrN>
139   void Emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const {
140     raw_ostream &OutStream = ObjWriter.getStream();
141     Emit_impl(OutStream, ObjWriter, Layout);
142   }
143
144   /// Get the size in bytes of this directive if emitted in @p ObjWriter with
145   /// the given @p Layout.
146   uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
147                        const MCAsmLayout &Layout) const {
148     raw_counting_ostream OutStream;
149     Emit_impl(OutStream, ObjWriter, Layout);
150     return OutStream.tell();
151   }
152 };
153
154 class MCLOHContainer {
155   /// Keep track of the emit size of all the LOHs.
156   mutable uint64_t EmitSize;
157
158   /// Keep track of all LOH directives.
159   SmallVector<MCLOHDirective, 32> Directives;
160
161 public:
162   typedef SmallVectorImpl<MCLOHDirective> LOHDirectives;
163
164   MCLOHContainer() : EmitSize(0) {};
165
166   /// Const accessor to the directives.
167   const LOHDirectives &getDirectives() const {
168     return Directives;
169   }
170
171   /// Add the directive of the given kind @p Kind with the given arguments
172   /// @p Args to the container.
173   void addDirective(MCLOHType Kind, const MCLOHDirective::LOHArgs &Args) {
174     Directives.push_back(MCLOHDirective(Kind, Args));
175   }
176
177   /// Get the size of the directives if emitted.
178   uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
179                        const MCAsmLayout &Layout) const {
180     if (!EmitSize) {
181       for (const MCLOHDirective &D : Directives)
182         EmitSize += D.getEmitSize(ObjWriter, Layout);
183     }
184     return EmitSize;
185   }
186
187   /// Emit all Linker Optimization Hint in one big table.
188   /// Each line of the table is emitted by LOHDirective::Emit.
189   void Emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const {
190     for (const MCLOHDirective &D : Directives)
191       D.Emit(ObjWriter, Layout);
192   }
193
194   void reset() {
195     Directives.clear();
196     EmitSize = 0;
197   }
198 };
199
200 // Add types for specialized template using MCSymbol.
201 typedef MCLOHDirective::LOHArgs MCLOHArgs;
202 typedef MCLOHContainer::LOHDirectives MCLOHDirectives;
203
204 } // end namespace llvm
205
206 #endif