Files
clang-p2996/llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.cpp
Pierre van Houtryve 7d81062352 [GlobalISel] Refactor Combiner MatchData & Apply C++ Code Handling (#92239)
Combiners that use C++ code in their "apply" pattern only use that. They
never mix it with MIR patterns as that has little added value.

This patch restricts C++ apply code so that if C++ is used, we cannot
use MIR patterns or builtins with it. Adding this restriction allows us
to merge calls to match and apply C++ code together, which in turns
makes it so we can just have MatchData variables on the stack.

So before, we would have
```
  GIM_CheckCxxInsnPredicate // match
  GIM_CheckCxxInsnPredicate // apply
  GIR_Done
```
Alongside a massive C++ struct holding the MatchData of all rules
possible (which was a big space/perf issue).

Now we just have
```
GIR_DoneWithCustomAction
```

And the function being ran just does
```
unsigned SomeMatchData;
if (match(SomeMatchData))
  apply(SomeMatchData)
```

This approach solves multiple issues in one:
- MatchData handling is greatly simplified and more efficient, "don't
pay for what you don't use"
  - We reduce the size of the match table
- Calling C++ code has a certain overhead (we need a switch), and this
overhead is only paid once now.

Handling of C++ code inside PatFrags is unchanged though, that still
emits a `GIM_CheckCxxInsnPredicate`. This is completely fine as they
can't use MatchDatas.
2024-05-16 13:39:00 +02:00

52 lines
1.9 KiB
C++

//===- CXXPredicates.cpp ----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//
#include "CXXPredicates.h"
#include "llvm/ADT/STLExtras.h"
namespace llvm {
namespace gi {
std::vector<const CXXPredicateCode *>
CXXPredicateCode::getSorted(const CXXPredicateCodePool &Pool) {
std::vector<const CXXPredicateCode *> Out;
std::transform(Pool.begin(), Pool.end(), std::back_inserter(Out),
[&](auto &Elt) { return Elt.second.get(); });
sort(Out, [](const auto *A, const auto *B) { return A->ID < B->ID; });
return Out;
}
const CXXPredicateCode &CXXPredicateCode::get(CXXPredicateCodePool &Pool,
std::string Code) {
// Check if we already have an identical piece of code, if not, create an
// entry in the pool.
const auto CodeHash = hash_value(Code);
if (auto It = Pool.find(CodeHash); It != Pool.end())
return *It->second;
const auto ID = Pool.size();
auto OwnedData = std::unique_ptr<CXXPredicateCode>(
new CXXPredicateCode(std::move(Code), ID));
const auto &DataRef = *OwnedData;
Pool[CodeHash] = std::move(OwnedData);
return DataRef;
}
// TODO: Make BaseEnumName prefix configurable.
CXXPredicateCode::CXXPredicateCode(std::string Code, unsigned ID)
: Code(Code), ID(ID), BaseEnumName("GICombiner" + std::to_string(ID)) {}
CXXPredicateCode::CXXPredicateCodePool CXXPredicateCode::AllCXXMatchCode;
CXXPredicateCode::CXXPredicateCodePool CXXPredicateCode::AllCXXCustomActionCode;
} // namespace gi
} // namespace llvm