LLVM 24.0.0git
IRUnitRef.h
Go to the documentation of this file.
1//===- llvm/IR/IRUnitRef.h - Reference to an IR unit ------------*- 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
10/// This file defines IRUnitRef, a type-erased reference to the IR unit a pass
11/// or analysis is running on, and IRUnitKindTraits, which IR units specialize
12/// to opt into being referred to by one.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_IR_IRUNITREF_H
17#define LLVM_IR_IRUNITREF_H
18
20#include <cstddef>
21#include <type_traits>
22
23namespace llvm {
24
25class Function;
26class Loop;
27class MachineFunction;
28class Module;
29
30/// The IR units a pass can run on.
38
39/// Map an IR unit type to its IRUnitKind.
40template <typename IRUnitT> struct IRUnitKindTraits {};
41
42template <> struct IRUnitKindTraits<Module> {
44};
45template <> struct IRUnitKindTraits<Function> {
47};
48template <> struct IRUnitKindTraits<Loop> {
49 static constexpr IRUnitKind Kind = IRUnitKind::Loop;
50};
51template <> struct IRUnitKindTraits<MachineFunction> {
53};
54
55// IRUnitKindTraits<LazyCallGraph::SCC> needs to be defined in
56// Analysis/LazyCallGraph.h.
57
58/// A type-erased reference to the IR unit a pass or analysis is running on,
59/// together with the kind of IR unit it refers to.
60class IRUnitRef {
61 template <typename To, typename From, typename Enable> friend struct CastInfo;
62
63 const void *Ptr;
64 IRUnitKind Kind;
65
66 /// Which kind of IR unit is wrapped.
67 IRUnitKind getKind() const { return Kind; }
68
69 /// The wrapped IR unit, type-erased.
70 const void *getPointer() const { return Ptr; }
71
72public:
73 template <typename IRUnitT, IRUnitKind K = IRUnitKindTraits<IRUnitT>::Kind>
74 IRUnitRef(const IRUnitT &IR) : Ptr(&IR), Kind(K) {}
75};
76
77static_assert(!std::is_constructible_v<IRUnitRef, std::nullptr_t>,
78 "IRUnitRef must not be constructible from nullptr");
79
80/// Lets isa/cast/dyn_cast query which IR unit an IRUnitRef holds, naming the IR
81/// unit itself rather than a pointer to it: dyn_cast<Module>(IR).
82template <typename To> struct CastInfo<To, IRUnitRef> {
83 static bool isPossible(IRUnitRef IR) {
84 return IR.getKind() == IRUnitKindTraits<To>::Kind;
85 }
86
87 static const To *doCast(IRUnitRef IR) {
88 return static_cast<const To *>(IR.getPointer());
89 }
90
91 static const To *castFailed() { return nullptr; }
92
93 static const To *doCastIfPossible(IRUnitRef IR) {
94 return isPossible(IR) ? doCast(IR) : castFailed();
95 }
96};
97
98template <typename To>
99struct CastInfo<To, const IRUnitRef> : public CastInfo<To, IRUnitRef> {};
100
101} // end namespace llvm
102
103#endif // LLVM_IR_IRUNITREF_H
aarch64 promote const
Value * getPointer(Value *Ptr)
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:81
A type-erased reference to the IR unit a pass or analysis is running on, together with the kind of IR...
Definition IRUnitRef.h:60
IRUnitRef(const IRUnitT &IR)
Definition IRUnitRef.h:74
friend struct CastInfo
Definition IRUnitRef.h:61
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
This is an optimization pass for GlobalISel generic memory operations.
IRUnitKind
The IR units a pass can run on.
Definition IRUnitRef.h:31
static const To * doCastIfPossible(IRUnitRef IR)
Definition IRUnitRef.h:93
static bool isPossible(IRUnitRef IR)
Definition IRUnitRef.h:83
static const To * castFailed()
Definition IRUnitRef.h:91
static const To * doCast(IRUnitRef IR)
Definition IRUnitRef.h:87
This struct provides a method for customizing the way a cast is performed.
Definition Casting.h:476
static CastReturnType castFailed()
Definition Casting.h:490
static CastReturnType doCast(const From &f)
Definition Casting.h:481
static bool isPossible(const From &f)
Definition Casting.h:254
static constexpr IRUnitKind Kind
Definition IRUnitRef.h:46
static constexpr IRUnitKind Kind
Definition IRUnitRef.h:49
static constexpr IRUnitKind Kind
Definition IRUnitRef.h:52
static constexpr IRUnitKind Kind
Definition IRUnitRef.h:43
Map an IR unit type to its IRUnitKind.
Definition IRUnitRef.h:40