1 //===- FaultMaps.h - The "FaultMaps" section --------------------*- C++ -*-===//
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 #ifndef LLVM_CODEGEN_FAULTMAPS_H
10 #define LLVM_CODEGEN_FAULTMAPS_H
11 
12 #include "llvm/MC/MCSymbol.h"
13 #include "llvm/Support/Endian.h"
14 #include <map>
15 #include <vector>
16 
17 namespace llvm {
18 
19 class AsmPrinter;
20 class MCExpr;
21 class raw_ostream;
22 
23 class FaultMaps {
24 public:
25   enum FaultKind {
26     FaultingLoad = 1,
27     FaultingLoadStore,
28     FaultingStore,
29     FaultKindMax
30   };
31 
32   explicit FaultMaps(AsmPrinter &AP);
33 
34   static const char *faultTypeToString(FaultKind);
35 
36   void recordFaultingOp(FaultKind FaultTy, const MCSymbol *FaultingLabel,
37                         const MCSymbol *HandlerLabel);
38   void serializeToFaultMapSection();
reset()39   void reset() {
40     FunctionInfos.clear();
41   }
42 
43 private:
44   static const char *WFMP;
45 
46   struct FaultInfo {
47     FaultKind Kind = FaultKindMax;
48     const MCExpr *FaultingOffsetExpr = nullptr;
49     const MCExpr *HandlerOffsetExpr = nullptr;
50 
51     FaultInfo() = default;
52 
FaultInfoFaultInfo53     explicit FaultInfo(FaultMaps::FaultKind Kind, const MCExpr *FaultingOffset,
54                        const MCExpr *HandlerOffset)
55         : Kind(Kind), FaultingOffsetExpr(FaultingOffset),
56           HandlerOffsetExpr(HandlerOffset) {}
57   };
58 
59   using FunctionFaultInfos = std::vector<FaultInfo>;
60 
61   // We'd like to keep a stable iteration order for FunctionInfos to help
62   // FileCheck based testing.
63   struct MCSymbolComparator {
operatorMCSymbolComparator64     bool operator()(const MCSymbol *LHS, const MCSymbol *RHS) const {
65       return LHS->getName() < RHS->getName();
66     }
67   };
68 
69   std::map<const MCSymbol *, FunctionFaultInfos, MCSymbolComparator>
70       FunctionInfos;
71   AsmPrinter &AP;
72 
73   void emitFunctionInfo(const MCSymbol *FnLabel, const FunctionFaultInfos &FFI);
74 };
75 
76 } // end namespace llvm
77 
78 #endif // LLVM_CODEGEN_FAULTMAPS_H
79