LLVM 24.0.0git
LastSubtargetCache.h
Go to the documentation of this file.
1//===- LastSubtargetCache.h - Reuse the subtarget of the last function ----===//
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
10/// Cache of the last subtarget a TargetMachine returned, to skip building the
11/// subtarget key for consecutive functions with the same subtarget.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TARGET_LASTSUBTARGETCACHE_H
16#define LLVM_TARGET_LASTSUBTARGETCACHE_H
17
18#include "llvm/IR/Attributes.h"
19
20namespace llvm {
21
22class Module;
23
24/// Cache of the last subtarget returned by getSubtargetImpl. A query hits if
25/// the function has the same function attributes and module as the last one,
26/// or else the same \p AttrsT, which must hold every input to the subtarget
27/// key, including module-level ones.
28///
29/// Attributes are compared by their uniqued pointers, so the cache must be
30/// cleared before it sees functions from a different LLVMContext.
31template <typename SubtargetT, typename AttrsT> class LastSubtargetCache {
32 AttributeSet FnAttrs;
33 const Module *M = nullptr;
34 AttrsT Attrs;
35 const SubtargetT *Subtarget = nullptr;
36
37public:
38 /// Return the last subtarget if the function attributes and module match.
39 const SubtargetT *lookup(AttributeSet NewFnAttrs, const Module *NewM) const {
40 return Subtarget && FnAttrs == NewFnAttrs && M == NewM ? Subtarget
41 : nullptr;
42 }
43
44 /// Return the last subtarget if \p NewAttrs match, and remember
45 /// \p NewFnAttrs and \p NewM for it.
46 const SubtargetT *lookup(AttributeSet NewFnAttrs, const Module *NewM,
47 const AttrsT &NewAttrs) {
48 if (!Subtarget || Attrs != NewAttrs)
49 return nullptr;
50 FnAttrs = NewFnAttrs;
51 M = NewM;
52 return Subtarget;
53 }
54
55 /// Remember \p NewSubtarget for these attributes.
56 void set(AttributeSet NewFnAttrs, const Module *NewM, const AttrsT &NewAttrs,
57 const SubtargetT *NewSubtarget) {
58 FnAttrs = NewFnAttrs;
59 M = NewM;
60 Attrs = NewAttrs;
61 Subtarget = NewSubtarget;
62 }
63
64 void clear() { *this = LastSubtargetCache(); }
65};
66
67} // end namespace llvm
68
69#endif // LLVM_TARGET_LASTSUBTARGETCACHE_H
This file contains the simple types necessary to represent the attributes associated with functions a...
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:448
Cache of the last subtarget returned by getSubtargetImpl.
void set(AttributeSet NewFnAttrs, const Module *NewM, const AttrsT &NewAttrs, const SubtargetT *NewSubtarget)
Remember NewSubtarget for these attributes.
const SubtargetT * lookup(AttributeSet NewFnAttrs, const Module *NewM, const AttrsT &NewAttrs)
Return the last subtarget if NewAttrs match, and remember NewFnAttrs and NewM for it.
const SubtargetT * lookup(AttributeSet NewFnAttrs, const Module *NewM) const
Return the last subtarget if the function attributes and module match.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
This is an optimization pass for GlobalISel generic memory operations.