Coverage Report

Created: 2019-02-21 13:17

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/MC/MCLinkerOptimizationHint.h
Line
Count
Source (jump to first uncovered line)
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"
22
#include "llvm/Support/raw_ostream.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
699
static inline StringRef MCLOHDirectiveName() {
45
699
  return StringRef(".loh");
46
699
}
AArch64AsmParser.cpp:llvm::MCLOHDirectiveName()
Line
Count
Source
44
459
static inline StringRef MCLOHDirectiveName() {
45
459
  return StringRef(".loh");
46
459
}
MCAsmStreamer.cpp:llvm::MCLOHDirectiveName()
Line
Count
Source
44
240
static inline StringRef MCLOHDirectiveName() {
45
240
  return StringRef(".loh");
46
240
}
47
48
11
static inline bool isValidMCLOHType(unsigned Kind) {
49
11
  return Kind >= MCLOH_AdrpAdrp && Kind <= MCLOH_AdrpLdrGot;
50
11
}
51
52
12
static inline int MCLOHNameToId(StringRef Name) {
53
96
#define MCLOHCaseNameToId(Name)     .Case(#Name, MCLOH_ ## Name)
54
12
  return StringSwitch<int>(Name)
55
12
    MCLOHCaseNameToId(AdrpAdrp)
56
12
    MCLOHCaseNameToId(AdrpLdr)
57
12
    MCLOHCaseNameToId(AdrpAddLdr)
58
12
    MCLOHCaseNameToId(AdrpLdrGotLdr)
59
12
    MCLOHCaseNameToId(AdrpAddStr)
60
12
    MCLOHCaseNameToId(AdrpLdrGotStr)
61
12
    MCLOHCaseNameToId(AdrpAdd)
62
12
    MCLOHCaseNameToId(AdrpLdrGot)
63
12
    .Default(-1);
64
12
}
65
66
240
static inline StringRef MCLOHIdToName(MCLOHType Kind) {
67
240
#define MCLOHCaseIdToName(Name)      case MCLOH_ ## Name: return StringRef(#Name);
68
240
  switch (Kind) {
69
240
    
MCLOHCaseIdToName13
(AdrpAdrp);
70
63
    MCLOHCaseIdToName
(AdrpLdr)0
;
71
4
    MCLOHCaseIdToName
(AdrpAddLdr)0
;
72
44
    MCLOHCaseIdToName
(AdrpLdrGotLdr)0
;
73
3
    MCLOHCaseIdToName
(AdrpAddStr)0
;
74
16
    MCLOHCaseIdToName
(AdrpLdrGotStr)0
;
75
24
    MCLOHCaseIdToName
(AdrpAdd)0
;
76
73
    MCLOHCaseIdToName
(AdrpLdrGot)0
;0
77
240
  }
78
240
  
return StringRef()0
;
79
240
}
80
81
21
static inline int MCLOHIdToNbArgs(MCLOHType Kind) {
82
21
  switch (Kind) {
83
21
    // LOH with two arguments
84
21
  case MCLOH_AdrpAdrp:
85
13
  case MCLOH_AdrpLdr:
86
13
  case MCLOH_AdrpAdd:
87
13
  case MCLOH_AdrpLdrGot:
88
13
    return 2;
89
13
    // LOH with three arguments
90
13
  case MCLOH_AdrpAddLdr:
91
8
  case MCLOH_AdrpLdrGotLdr:
92
8
  case MCLOH_AdrpAddStr:
93
8
  case MCLOH_AdrpLdrGotStr:
94
8
    return 3;
95
0
  }
96
0
  return -1;
97
0
}
98
99
/// Store Linker Optimization Hint information (LOH).
100
class MCLOHDirective {
101
  MCLOHType Kind;
102
103
  /// Arguments of this directive. Order matters.
104
  SmallVector<MCSymbol *, 3> Args;
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:
113
  using LOHArgs = SmallVectorImpl<MCSymbol *>;
114
115
  MCLOHDirective(MCLOHType Kind, const LOHArgs &Args)
116
664k
      : Kind(Kind), Args(Args.begin(), Args.end()) {
117
664k
    assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
118
664k
  }
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
134
class MCLOHContainer {
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.
139
  SmallVector<MCLOHDirective, 32> Directives;
140
141
public:
142
  using LOHDirectives = SmallVectorImpl<MCLOHDirective>;
143
144
43.1k
  MCLOHContainer() = default;
145
146
  /// Const accessor to the directives.
147
0
  const LOHDirectives &getDirectives() const {
148
0
    return Directives;
149
0
  }
150
151
  /// Add the directive of the given kind \p Kind with the given arguments
152
  /// \p Args to the container.
153
664k
  void addDirective(MCLOHType Kind, const MCLOHDirective::LOHArgs &Args) {
154
664k
    Directives.push_back(MCLOHDirective(Kind, Args));
155
664k
  }
156
157
  /// Get the size of the directives if emitted.
158
  uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
159
13.7k
                       const MCAsmLayout &Layout) const {
160
13.7k
    if (!EmitSize) {
161
13.7k
      for (const MCLOHDirective &D : Directives)
162
664k
        EmitSize += D.getEmitSize(ObjWriter, Layout);
163
13.7k
    }
164
13.7k
    return EmitSize;
165
13.7k
  }
166
167
  /// Emit all Linker Optimization Hint in one big table.
168
  /// Each line of the table is emitted by LOHDirective::emit.
169
6.53k
  void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const {
170
6.53k
    for (const MCLOHDirective &D : Directives)
171
664k
      D.emit(ObjWriter, Layout);
172
6.53k
  }
173
174
28.2k
  void reset() {
175
28.2k
    Directives.clear();
176
28.2k
    EmitSize = 0;
177
28.2k
  }
178
};
179
180
// Add types for specialized template using MCSymbol.
181
using MCLOHArgs = MCLOHDirective::LOHArgs;
182
using MCLOHDirectives = MCLOHContainer::LOHDirectives;
183
184
} // end namespace llvm
185
186
#endif // LLVM_MC_MCLINKEROPTIMIZATIONHINT_H