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