1 | //===- bolt/Utils/Utils.h - Common helper functions -------------*- 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 | // Common helper functions. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef BOLT_UTILS_UTILS_H |
14 | #define BOLT_UTILS_UTILS_H |
15 | |
16 | #include "llvm/ADT/StringRef.h" |
17 | #include "llvm/ADT/Twine.h" |
18 | #include "llvm/Support/Error.h" |
19 | |
20 | namespace llvm { |
21 | class MCCFIInstruction; |
22 | namespace bolt { |
23 | |
24 | /// Free memory allocated for \p List. |
25 | template <typename T> void clearList(T &List) { |
26 | T TempList; |
27 | TempList.swap(List); |
28 | } |
29 | |
30 | void report_error(StringRef Message, std::error_code EC); |
31 | |
32 | void report_error(StringRef Message, Error E); |
33 | |
34 | void check_error(std::error_code EC, StringRef Message); |
35 | |
36 | void check_error(Error E, Twine Message); |
37 | |
38 | /// Return the name with escaped whitespace and backslash characters |
39 | std::string getEscapedName(const StringRef &Name); |
40 | |
41 | /// Return the unescaped name |
42 | std::string getUnescapedName(const StringRef &Name); |
43 | |
44 | /// LTO-generated function names take a form: |
45 | /// |
46 | /// <function_name>.lto_priv.<decimal_number>/... |
47 | /// or |
48 | /// <function_name>.constprop.<decimal_number>/... |
49 | /// |
50 | /// they can also be: |
51 | /// |
52 | /// <function_name>.lto_priv.<decimal_number1>.lto_priv.<decimal_number2>/... |
53 | /// |
54 | /// The <decimal_number> is a global counter used for the whole program. As a |
55 | /// result, a tiny change in a program may affect the naming of many LTO |
56 | /// functions. For us this means that if we do a precise name matching, then |
57 | /// a large set of functions could be left without a profile. |
58 | /// |
59 | /// To solve this issue, we try to match a function to any profile: |
60 | /// |
61 | /// <function_name>.(lto_priv|consprop).* |
62 | /// |
63 | /// The name before an asterisk above represents a common LTO name for a family |
64 | /// of functions. Later, out of all matching profiles we pick the one with the |
65 | /// best match. |
66 | /// |
67 | /// Return a common part of LTO name for a given \p Name. |
68 | std::optional<StringRef> getLTOCommonName(const StringRef Name); |
69 | |
70 | // Determines which register a given DWARF expression is being assigned to. |
71 | // If the expression is defining the CFA, return std::nullopt. |
72 | std::optional<uint8_t> readDWARFExpressionTargetReg(StringRef ExprBytes); |
73 | |
74 | } // namespace bolt |
75 | |
76 | bool operator==(const llvm::MCCFIInstruction &L, |
77 | const llvm::MCCFIInstruction &R); |
78 | |
79 | } // namespace llvm |
80 | |
81 | #endif |
82 | |