1 | //===- CXXPredicates.h ------------------------------------------*- 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 | /// \file Contains utilities related to handling C++ code in MIR patterns for |
10 | /// GlobalISel. C++ predicates need to be expanded, and then stored in a |
11 | /// static pool until they can be emitted. |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_UTILS_MIRPATTERNS_CXXPREDICATES_H |
16 | #define LLVM_UTILS_MIRPATTERNS_CXXPREDICATES_H |
17 | |
18 | #include "llvm/ADT/DenseMap.h" |
19 | #include "llvm/ADT/Hashing.h" |
20 | #include "llvm/ADT/StringRef.h" |
21 | #include <memory> |
22 | #include <string> |
23 | #include <vector> |
24 | |
25 | namespace llvm { |
26 | namespace gi { |
27 | |
28 | /// Entry into the static pool of all CXX Predicate code. This contains |
29 | /// fully expanded C++ code. |
30 | /// |
31 | /// The static pool is hidden inside the object and can be accessed through |
32 | /// getAllMatchCode/getAllApplyCode |
33 | /// |
34 | /// Note that CXXPattern trims C++ code, so the Code is already expected to be |
35 | /// free of leading/trailing whitespace. |
36 | class CXXPredicateCode { |
37 | using CXXPredicateCodePool = |
38 | DenseMap<hash_code, std::unique_ptr<CXXPredicateCode>>; |
39 | static CXXPredicateCodePool AllCXXMatchCode; |
40 | static CXXPredicateCodePool AllCXXCustomActionCode; |
41 | |
42 | /// Sorts a `CXXPredicateCodePool` by their IDs and returns it. |
43 | static std::vector<const CXXPredicateCode *> |
44 | getSorted(const CXXPredicateCodePool &Pool); |
45 | |
46 | /// Gets an instance of `CXXPredicateCode` for \p Code, or returns an already |
47 | /// existing one. |
48 | static const CXXPredicateCode &get(CXXPredicateCodePool &Pool, |
49 | std::string Code); |
50 | |
51 | CXXPredicateCode(std::string Code, unsigned ID); |
52 | |
53 | public: |
54 | static const CXXPredicateCode &getMatchCode(std::string Code) { |
55 | return get(Pool&: AllCXXMatchCode, Code: std::move(Code)); |
56 | } |
57 | |
58 | static const CXXPredicateCode &getCustomActionCode(std::string Code) { |
59 | return get(Pool&: AllCXXCustomActionCode, Code: std::move(Code)); |
60 | } |
61 | |
62 | static std::vector<const CXXPredicateCode *> getAllMatchCode() { |
63 | return getSorted(Pool: AllCXXMatchCode); |
64 | } |
65 | |
66 | static std::vector<const CXXPredicateCode *> getAllCustomActionsCode() { |
67 | return getSorted(Pool: AllCXXCustomActionCode); |
68 | } |
69 | |
70 | const std::string Code; |
71 | const unsigned ID; |
72 | const std::string BaseEnumName; |
73 | |
74 | bool needsUnreachable() const { |
75 | return !StringRef(Code).starts_with(Prefix: "return"); |
76 | } |
77 | |
78 | std::string getEnumNameWithPrefix(StringRef Prefix) const { |
79 | return Prefix.str() + BaseEnumName; |
80 | } |
81 | }; |
82 | |
83 | } // namespace gi |
84 | } // end namespace llvm |
85 | |
86 | #endif // ifndef LLVM_UTILS_MIRPATTERNS_CXXPREDICATES_H |
87 |