clang 24.0.0git
CIRGenTBAA.h
Go to the documentation of this file.
1//===--- CIRGenTBAA.h - TBAA information for CIR CodeGen --------*- 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// This is the code that manages TBAA information and defines the TBAA policy
10// for the optimizer to use. It mirrors clang/lib/CodeGen/CodeGenTBAA.h, but
11// produces CIR TBAA attributes instead of LLVM metadata.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef CLANG_LIB_CIR_CODEGEN_CIRGENTBAA_H
16#define CLANG_LIB_CIR_CODEGEN_CIRGENTBAA_H
17
18#include "mlir/IR/Attributes.h"
19#include "mlir/IR/MLIRContext.h"
20#include "clang/AST/Type.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/SmallVector.h"
24
25#include <memory>
26
27namespace clang {
28class ASTContext;
29class CodeGenOptions;
30class LangOptions;
31class MangleContext;
32} // namespace clang
33
34namespace clang::CIRGen {
35class CIRGenCXXABI;
36
37enum class TBAAAccessKind : unsigned {
41};
42
43/// Describes a memory access in terms of TBAA.
50
56
59
60 TBAAAccessInfo() : TBAAAccessInfo(/*accessType=*/{}, /*size=*/0) {}
61
63 return TBAAAccessInfo(TBAAAccessKind::MayAlias, /*baseType=*/{},
64 /*accessType=*/{}, /*offset=*/0, /*size=*/0);
65 }
66
67 bool isMayAlias() const { return kind == TBAAAccessKind::MayAlias; }
68
70 return TBAAAccessInfo(TBAAAccessKind::Incomplete, /*baseType=*/{},
71 /*accessType=*/{}, /*offset=*/0, /*size=*/0);
72 }
73
74 bool isIncomplete() const { return kind == TBAAAccessKind::Incomplete; }
75
76 bool operator==(const TBAAAccessInfo &other) const {
77 return kind == other.kind && baseType == other.baseType &&
78 accessType == other.accessType && offset == other.offset &&
79 size == other.size;
80 }
81
82 bool operator!=(const TBAAAccessInfo &other) const {
83 return !(*this == other);
84 }
85
86 explicit operator bool() const { return *this != TBAAAccessInfo(); }
87
88 /// The kind of the access descriptor.
90
91 /// The base/leading access type. May be null if this access descriptor
92 /// represents an access that is not considered to be an access to an
93 /// aggregate or union member.
95
96 /// The final access type. May be null if there is no TBAA information
97 /// available about this access.
99
100 /// The byte offset of the final access within the base one. Must be zero if
101 /// the base access type is not specified.
103
104 /// The size of access, in bytes.
106};
107
108/// This class organizes the cross-module state that is used while generating
109/// TBAA attributes for memory accesses.
111 mlir::MLIRContext &mlirContext;
112 clang::ASTContext &astContext;
113 CIRGenCXXABI &cxxABI;
114 const clang::CodeGenOptions &codeGenOpts;
115 const clang::LangOptions &features;
116 std::unique_ptr<clang::MangleContext> mangleCtx;
117
118 /// Maps clang types to the type descriptors used to describe accesses to
119 /// objects of these types.
120 llvm::DenseMap<const clang::Type *, cir::TBAATypeDescriptorAttr>
121 metadataCache;
122 /// Maps clang types to their base access type descriptors.
123 llvm::DenseMap<const clang::Type *, cir::TBAATypeDescriptorAttr>
124 baseTypeMetadataCache;
125
126 /// The "any pointer" type descriptors, indexed by pointer depth minus one.
128
129 /// Returns the type descriptor of the omnipotent char type.
130 cir::TBAAOmnipotentCharAttr getChar();
131
132 /// Returns the type descriptor of the "any" pointer of depth \p ptrDepth.
133 cir::TBAAScalarAttr getAnyPtr(unsigned ptrDepth = 1);
134
135 /// Creates a scalar type descriptor; a null \p parent means omnipotent char.
136 cir::TBAAScalarAttr createScalarTypeNode(llvm::StringRef name,
137 cir::TBAAScalarAttr parent);
138
139 /// Returns the type descriptor of the given type, without caching.
140 cir::TBAATypeDescriptorAttr getTypeInfoHelper(const clang::Type *ty);
141
142 /// Returns the base type descriptor of the given valid base type.
143 cir::TBAATypeDescriptorAttr getValidBaseTypeInfo(clang::QualType qty);
144
145 /// Returns the base type descriptor of the given type, without caching.
146 cir::TBAATypeDescriptorAttr getBaseTypeInfoHelper(const clang::Type *ty);
147
148public:
149 CIRGenTBAA(mlir::MLIRContext &mlirContext, clang::ASTContext &astContext,
150 CIRGenCXXABI &cxxABI, const clang::CodeGenOptions &codeGenOpts,
151 const clang::LangOptions &features);
153
154 /// Returns the type descriptor used to describe accesses to objects of the
155 /// given type.
157
158 /// Returns the TBAA information that describes an access to an object of the
159 /// given type.
161
162 /// Returns the type descriptor that describes the given base access type, or
163 /// null if the type is not suitable for use in TBAA access tags.
165
166 /// Returns the access tag for the given memory access, or null if the access
167 /// has no TBAA information.
168 cir::TBAATagAttr getAccessTagInfo(TBAAAccessInfo info);
169
170 /// Returns the merged TBAA information for the purpose of type casts.
172 TBAAAccessInfo targetInfo);
173
174 /// Returns the merged TBAA information for the purpose of conditional
175 /// operator.
177 TBAAAccessInfo infoB);
178};
179
180} // namespace clang::CIRGen
181
182#endif // CLANG_LIB_CIR_CODEGEN_CIRGENTBAA_H
C Language Family Type Representation.
Base class of the TBAA type descriptor attributes.
Definition CIRAttrs.h:48
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:246
Implements C++ ABI-specific code generation functions.
TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo infoA, TBAAAccessInfo infoB)
Returns the merged TBAA information for the purpose of conditional operator.
cir::TBAATypeDescriptorAttr getTypeInfo(clang::QualType qty)
Returns the type descriptor used to describe accesses to objects of the given type.
cir::TBAATagAttr getAccessTagInfo(TBAAAccessInfo info)
Returns the access tag for the given memory access, or null if the access has no TBAA information.
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo sourceInfo, TBAAAccessInfo targetInfo)
Returns the merged TBAA information for the purpose of type casts.
TBAAAccessInfo getAccessInfo(clang::QualType accessType)
Returns the TBAA information that describes an access to an object of the given type.
CIRGenTBAA(mlir::MLIRContext &mlirContext, clang::ASTContext &astContext, CIRGenCXXABI &cxxABI, const clang::CodeGenOptions &codeGenOpts, const clang::LangOptions &features)
cir::TBAATypeDescriptorAttr getBaseTypeInfo(clang::QualType qty)
Returns the type descriptor that describes the given base access type, or null if the type is not sui...
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition Mangle.h:56
A (possibly-)qualified type.
Definition TypeBase.h:938
The base class of the type hierarchy.
Definition TypeBase.h:1879
Top level wrappers for InstallAPI frontend operations.
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
Describes a memory access in terms of TBAA.
Definition CIRGenTBAA.h:44
bool operator!=(const TBAAAccessInfo &other) const
Definition CIRGenTBAA.h:82
static TBAAAccessInfo getIncompleteInfo()
Definition CIRGenTBAA.h:69
TBAAAccessInfo(cir::TBAATypeDescriptorAttr accessType, uint64_t size)
Definition CIRGenTBAA.h:57
TBAAAccessInfo(TBAAAccessKind kind, cir::TBAATypeDescriptorAttr baseType, cir::TBAATypeDescriptorAttr accessType, uint64_t offset, uint64_t size)
Definition CIRGenTBAA.h:45
bool operator==(const TBAAAccessInfo &other) const
Definition CIRGenTBAA.h:76
cir::TBAATypeDescriptorAttr accessType
The final access type.
Definition CIRGenTBAA.h:98
static TBAAAccessInfo getMayAliasInfo()
Definition CIRGenTBAA.h:62
uint64_t offset
The byte offset of the final access within the base one.
Definition CIRGenTBAA.h:102
cir::TBAATypeDescriptorAttr baseType
The base/leading access type.
Definition CIRGenTBAA.h:94
TBAAAccessKind kind
The kind of the access descriptor.
Definition CIRGenTBAA.h:89
TBAAAccessInfo(cir::TBAATypeDescriptorAttr baseType, cir::TBAATypeDescriptorAttr accessType, uint64_t offset, uint64_t size)
Definition CIRGenTBAA.h:51
uint64_t size
The size of access, in bytes.
Definition CIRGenTBAA.h:105