Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/FaultMaps.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- FaultMaps.cpp ------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/CodeGen/FaultMaps.h"
10
#include "llvm/ADT/Twine.h"
11
#include "llvm/CodeGen/AsmPrinter.h"
12
#include "llvm/MC/MCContext.h"
13
#include "llvm/MC/MCExpr.h"
14
#include "llvm/MC/MCObjectFileInfo.h"
15
#include "llvm/MC/MCStreamer.h"
16
#include "llvm/Support/Debug.h"
17
#include "llvm/Support/ErrorHandling.h"
18
#include "llvm/Support/Format.h"
19
#include "llvm/Support/raw_ostream.h"
20
21
using namespace llvm;
22
23
#define DEBUG_TYPE "faultmaps"
24
25
static const int FaultMapVersion = 1;
26
const char *FaultMaps::WFMP = "Fault Maps: ";
27
28
12.3k
FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
29
30
void FaultMaps::recordFaultingOp(FaultKind FaultTy,
31
25
                                 const MCSymbol *HandlerLabel) {
32
25
  MCContext &OutContext = AP.OutStreamer->getContext();
33
25
  MCSymbol *FaultingLabel = OutContext.createTempSymbol();
34
25
35
25
  AP.OutStreamer->EmitLabel(FaultingLabel);
36
25
37
25
  const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
38
25
      MCSymbolRefExpr::create(FaultingLabel, OutContext),
39
25
      MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
40
25
41
25
  const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
42
25
      MCSymbolRefExpr::create(HandlerLabel, OutContext),
43
25
      MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
44
25
45
25
  FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
46
25
                                              HandlerOffset);
47
25
}
48
49
11.3k
void FaultMaps::serializeToFaultMapSection() {
50
11.3k
  if (FunctionInfos.empty())
51
11.3k
    return;
52
3
53
3
  MCContext &OutContext = AP.OutStreamer->getContext();
54
3
  MCStreamer &OS = *AP.OutStreamer;
55
3
56
3
  // Create the section.
57
3
  MCSection *FaultMapSection =
58
3
      OutContext.getObjectFileInfo()->getFaultMapSection();
59
3
  OS.SwitchSection(FaultMapSection);
60
3
61
3
  // Emit a dummy symbol to force section inclusion.
62
3
  OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
63
3
64
3
  LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
65
3
66
3
  // Header
67
3
  OS.EmitIntValue(FaultMapVersion, 1); // Version.
68
3
  OS.EmitIntValue(0, 1);               // Reserved.
69
3
  OS.EmitIntValue(0, 2);               // Reserved.
70
3
71
3
  LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
72
3
  OS.EmitIntValue(FunctionInfos.size(), 4);
73
3
74
3
  LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
75
3
76
3
  for (const auto &FFI : FunctionInfos)
77
24
    emitFunctionInfo(FFI.first, FFI.second);
78
3
}
79
80
void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
81
24
                                 const FunctionFaultInfos &FFI) {
82
24
  MCStreamer &OS = *AP.OutStreamer;
83
24
84
24
  LLVM_DEBUG(dbgs() << WFMP << "  function addr: " << *FnLabel << "\n");
85
24
  OS.EmitSymbolValue(FnLabel, 8);
86
24
87
24
  LLVM_DEBUG(dbgs() << WFMP << "  #faulting PCs: " << FFI.size() << "\n");
88
24
  OS.EmitIntValue(FFI.size(), 4);
89
24
90
24
  OS.EmitIntValue(0, 4); // Reserved
91
24
92
25
  for (auto &Fault : FFI) {
93
25
    LLVM_DEBUG(dbgs() << WFMP << "    fault type: "
94
25
                      << faultTypeToString(Fault.Kind) << "\n");
95
25
    OS.EmitIntValue(Fault.Kind, 4);
96
25
97
25
    LLVM_DEBUG(dbgs() << WFMP << "    faulting PC offset: "
98
25
                      << *Fault.FaultingOffsetExpr << "\n");
99
25
    OS.EmitValue(Fault.FaultingOffsetExpr, 4);
100
25
101
25
    LLVM_DEBUG(dbgs() << WFMP << "    fault handler PC offset: "
102
25
                      << *Fault.HandlerOffsetExpr << "\n");
103
25
    OS.EmitValue(Fault.HandlerOffsetExpr, 4);
104
25
  }
105
24
}
106
107
6
const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
108
6
  switch (FT) {
109
6
  default:
110
0
    llvm_unreachable("unhandled fault type!");
111
6
  case FaultMaps::FaultingLoad:
112
4
    return "FaultingLoad";
113
6
  case FaultMaps::FaultingLoadStore:
114
0
    return "FaultingLoadStore";
115
6
  case FaultMaps::FaultingStore:
116
2
    return "FaultingStore";
117
6
  }
118
6
}
119
120
raw_ostream &llvm::
121
operator<<(raw_ostream &OS,
122
6
           const FaultMapParser::FunctionFaultInfoAccessor &FFI) {
123
6
  OS << "Fault kind: "
124
6
     << FaultMaps::faultTypeToString((FaultMaps::FaultKind)FFI.getFaultKind())
125
6
     << ", faulting PC offset: " << FFI.getFaultingPCOffset()
126
6
     << ", handling PC offset: " << FFI.getHandlerPCOffset();
127
6
  return OS;
128
6
}
129
130
raw_ostream &llvm::
131
6
operator<<(raw_ostream &OS, const FaultMapParser::FunctionInfoAccessor &FI) {
132
6
  OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
133
6
     << ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
134
12
  for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; 
++i6
)
135
6
    OS << FI.getFunctionFaultInfoAt(i) << "\n";
136
6
  return OS;
137
6
}
138
139
4
raw_ostream &llvm::operator<<(raw_ostream &OS, const FaultMapParser &FMP) {
140
4
  OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
141
4
  OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
142
4
143
4
  if (FMP.getNumFunctions() == 0)
144
1
    return OS;
145
3
146
3
  FaultMapParser::FunctionInfoAccessor FI;
147
3
148
9
  for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; 
++i6
) {
149
6
    FI = (i == 0) ? 
FMP.getFirstFunctionInfo()3
:
FI.getNextFunctionInfo()3
;
150
6
    OS << FI;
151
6
  }
152
3
153
3
  return OS;
154
3
}