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/StringRef.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Support/raw_ostream.h"
24 
25 namespace llvm {
26 
27 // Forward declarations.
28 class MCAsmLayout;
29 class MCSymbol;
30 
31 /// Linker Optimization Hint Type.
32 enum MCLOHType {
33   MCLOH_AdrpAdrp = 0x1u,      ///< Adrp xY, _v1@PAGE -> Adrp xY, _v2@PAGE.
34   MCLOH_AdrpLdr = 0x2u,       ///< Adrp _v@PAGE -> Ldr _v@PAGEOFF.
35   MCLOH_AdrpAddLdr = 0x3u,    ///< Adrp _v@PAGE -> Add _v@PAGEOFF -> Ldr.
36   MCLOH_AdrpLdrGotLdr = 0x4u, ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Ldr.
37   MCLOH_AdrpAddStr = 0x5u,    ///< Adrp _v@PAGE -> Add _v@PAGEOFF -> Str.
38   MCLOH_AdrpLdrGotStr = 0x6u, ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Str.
39   MCLOH_AdrpAdd = 0x7u,       ///< Adrp _v@PAGE -> Add _v@PAGEOFF.
40   MCLOH_AdrpLdrGot = 0x8u     ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF.
41 };
42 
MCLOHDirectiveName()43 static inline StringRef MCLOHDirectiveName() {
44   return StringRef(".loh");
45 }
46 
isValidMCLOHType(unsigned Kind)47 static inline bool isValidMCLOHType(unsigned Kind) {
48   return Kind >= MCLOH_AdrpAdrp && Kind <= MCLOH_AdrpLdrGot;
49 }
50 
MCLOHNameToId(StringRef Name)51 static inline int MCLOHNameToId(StringRef Name) {
52 #define MCLOHCaseNameToId(Name)     .Case(#Name, MCLOH_ ## Name)
53   return StringSwitch<int>(Name)
54     MCLOHCaseNameToId(AdrpAdrp)
55     MCLOHCaseNameToId(AdrpLdr)
56     MCLOHCaseNameToId(AdrpAddLdr)
57     MCLOHCaseNameToId(AdrpLdrGotLdr)
58     MCLOHCaseNameToId(AdrpAddStr)
59     MCLOHCaseNameToId(AdrpLdrGotStr)
60     MCLOHCaseNameToId(AdrpAdd)
61     MCLOHCaseNameToId(AdrpLdrGot)
62     .Default(-1);
63 }
64 
MCLOHIdToName(MCLOHType Kind)65 static inline StringRef MCLOHIdToName(MCLOHType Kind) {
66 #define MCLOHCaseIdToName(Name)      case MCLOH_ ## Name: return StringRef(#Name);
67   switch (Kind) {
68     MCLOHCaseIdToName(AdrpAdrp);
69     MCLOHCaseIdToName(AdrpLdr);
70     MCLOHCaseIdToName(AdrpAddLdr);
71     MCLOHCaseIdToName(AdrpLdrGotLdr);
72     MCLOHCaseIdToName(AdrpAddStr);
73     MCLOHCaseIdToName(AdrpLdrGotStr);
74     MCLOHCaseIdToName(AdrpAdd);
75     MCLOHCaseIdToName(AdrpLdrGot);
76   }
77   return StringRef();
78 }
79 
MCLOHIdToNbArgs(MCLOHType Kind)80 static inline int MCLOHIdToNbArgs(MCLOHType Kind) {
81   switch (Kind) {
82     // LOH with two arguments
83   case MCLOH_AdrpAdrp:
84   case MCLOH_AdrpLdr:
85   case MCLOH_AdrpAdd:
86   case MCLOH_AdrpLdrGot:
87     return 2;
88     // LOH with three arguments
89   case MCLOH_AdrpAddLdr:
90   case MCLOH_AdrpLdrGotLdr:
91   case MCLOH_AdrpAddStr:
92   case MCLOH_AdrpLdrGotStr:
93     return 3;
94   }
95   return -1;
96 }
97 
98 /// Store Linker Optimization Hint information (LOH).
99 class MCLOHDirective {
100   MCLOHType Kind;
101 
102   /// Arguments of this directive. Order matters.
103   SmallVector<MCSymbol *, 3> Args;
104 
105 public:
106   typedef SmallVectorImpl<MCSymbol *> LOHArgs;
107 
MCLOHDirective(MCLOHType Kind,const LOHArgs & Args)108   MCLOHDirective(MCLOHType Kind, const LOHArgs &Args)
109       : Kind(Kind), Args(Args.begin(), Args.end()) {
110     assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
111   }
112 
getKind()113   MCLOHType getKind() const { return Kind; }
114 
getArgs()115   const LOHArgs &getArgs() const { return Args; }
116 };
117 
118 class MCLOHContainer {
119   /// Keep track of the emit size of all the LOHs.
120   mutable uint64_t EmitSize;
121 
122   /// Keep track of all LOH directives.
123   SmallVector<MCLOHDirective, 32> Directives;
124 
125 public:
126   typedef SmallVectorImpl<MCLOHDirective> LOHDirectives;
127 
MCLOHContainer()128   MCLOHContainer() : EmitSize(0) {}
129 
130   /// Const accessor to the directives.
getDirectives()131   const LOHDirectives &getDirectives() const {
132     return Directives;
133   }
134 
135   /// Add the directive of the given kind \p Kind with the given arguments
136   /// \p Args to the container.
addDirective(MCLOHType Kind,const MCLOHDirective::LOHArgs & Args)137   void addDirective(MCLOHType Kind, const MCLOHDirective::LOHArgs &Args) {
138     Directives.push_back(MCLOHDirective(Kind, Args));
139   }
140 
reset()141   void reset() {
142     Directives.clear();
143     EmitSize = 0;
144   }
145 };
146 
147 // Add types for specialized template using MCSymbol.
148 typedef MCLOHDirective::LOHArgs MCLOHArgs;
149 typedef MCLOHContainer::LOHDirectives MCLOHDirectives;
150 
151 } // end namespace llvm
152 
153 #endif
154