1 //===- CXXPredicates.h ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_UTILS_TABLEGEN_COMMON_GLOBALISEL_CXXPREDICATES_H
16 #define LLVM_UTILS_TABLEGEN_COMMON_GLOBALISEL_CXXPREDICATES_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Hashing.h"
20 #include "llvm/ADT/StringRef.h"
28 /// Entry into the static pool of all CXX Predicate code. This contains
29 /// fully expanded C++ code.
31 /// The static pool is hidden inside the object and can be accessed through
32 /// getAllMatchCode/getAllApplyCode
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
;
42 /// Sorts a `CXXPredicateCodePool` by their IDs and returns it.
43 static std::vector
<const CXXPredicateCode
*>
44 getSorted(const CXXPredicateCodePool
&Pool
);
46 /// Gets an instance of `CXXPredicateCode` for \p Code, or returns an already
48 static const CXXPredicateCode
&get(CXXPredicateCodePool
&Pool
,
51 CXXPredicateCode(std::string Code
, unsigned ID
);
54 static const CXXPredicateCode
&getMatchCode(std::string Code
) {
55 return get(AllCXXMatchCode
, std::move(Code
));
58 static const CXXPredicateCode
&getCustomActionCode(std::string Code
) {
59 return get(AllCXXCustomActionCode
, std::move(Code
));
62 static std::vector
<const CXXPredicateCode
*> getAllMatchCode() {
63 return getSorted(AllCXXMatchCode
);
66 static std::vector
<const CXXPredicateCode
*> getAllCustomActionsCode() {
67 return getSorted(AllCXXCustomActionCode
);
70 const std::string Code
;
72 const std::string BaseEnumName
;
74 bool needsUnreachable() const {
75 return !StringRef(Code
).starts_with("return");
78 std::string
getEnumNameWithPrefix(StringRef Prefix
) const {
79 return Prefix
.str() + BaseEnumName
;
84 } // end namespace llvm
86 #endif // LLVM_UTILS_TABLEGEN_COMMON_GLOBALISEL_CXXPREDICATES_H