clang 8.0.0

CodeGenTBAA.h
Go to the documentation of this file.
1 //===--- CodeGenTBAA.h - TBAA information for LLVM CodeGen ------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is the code that manages TBAA information and defines the TBAA policy
11 // for the optimizer to use.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
16 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
17
18 #include "clang/AST/Type.h"
19 #include "clang/Basic/LLVM.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/IR/Metadata.h"
23
24 namespace clang {
25 class ASTContext;
26 class CodeGenOptions;
27 class LangOptions;
28 class MangleContext;
29 class QualType;
30 class Type;
31
32 namespace CodeGen {
33 class CGRecordLayout;
34
35 // TBAAAccessKind - A kind of TBAA memory access descriptor.
36 enum class TBAAAccessKind : unsigned {
37 Ordinary,
38 MayAlias,
39 Incomplete,
40 };
41
42 // TBAAAccessInfo - Describes a memory access in terms of TBAA.
43 struct TBAAAccessInfo {
44 TBAAAccessInfo(TBAAAccessKind Kind, llvm::MDNode *BaseType,
45 llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
46 : Kind(Kind), BaseType(BaseType), AccessType(AccessType),
47 Offset(Offset), Size(Size)
48 {}
49
50 TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType,
51 uint64_t Offset, uint64_t Size)
52 : TBAAAccessInfo(TBAAAccessKind::Ordinary, BaseType, AccessType,
53 Offset, Size)
54 {}
55
56 explicit TBAAAccessInfo(llvm::MDNode *AccessType, uint64_t Size)
57 : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType, /* Offset= */ 0, Size)
58 {}
59
60 TBAAAccessInfo()
61 : TBAAAccessInfo(/* AccessType= */ nullptr, /* Size= */ 0)
62 {}
63
64 static TBAAAccessInfo getMayAliasInfo() {
65 return TBAAAccessInfo(TBAAAccessKind::MayAlias,
66 /* BaseType= */ nullptr, /* AccessType= */ nullptr,
67 /* Offset= */ 0, /* Size= */ 0);
68 }
69
70 bool isMayAlias() const { return Kind == TBAAAccessKind::MayAlias; }
71
72 static TBAAAccessInfo getIncompleteInfo() {
73 return TBAAAccessInfo(TBAAAccessKind::Incomplete,
74 /* BaseType= */ nullptr, /* AccessType= */ nullptr,
75 /* Offset= */ 0, /* Size= */ 0);
76 }
77
78 bool isIncomplete() const { return Kind == TBAAAccessKind::Incomplete; }
79
80 bool operator==(const TBAAAccessInfo &Other) const {
81 return Kind == Other.Kind &&
82 BaseType == Other.BaseType &&
83 AccessType == Other.AccessType &&
84 Offset == Other.Offset &&
85 Size == Other.Size;
86 }
87
88 bool operator!=(const TBAAAccessInfo &Other) const {
89 return !(*this == Other);
90 }
91
92 explicit operator bool() const {
93 return *this != TBAAAccessInfo();
94 }
95
96 /// Kind - The kind of the access descriptor.
97 TBAAAccessKind Kind;
98
99 /// BaseType - The base/leading access type. May be null if this access
100 /// descriptor represents an access that is not considered to be an access
101 /// to an aggregate or union member.
102 llvm::MDNode *BaseType;
103
104 /// AccessType - The final access type. May be null if there is no TBAA
105 /// information available about this access.
106 llvm::MDNode *AccessType;
107
108 /// Offset - The byte offset of the final access within the base one. Must be
109 /// zero if the base access type is not specified.
110 uint64_t Offset;
111
112 /// Size - The size of access, in bytes.
113 uint64_t Size;
114 };
115
116 /// CodeGenTBAA - This class organizes the cross-module state that is used
117 /// while lowering AST types to LLVM types.
118 class CodeGenTBAA {
119 ASTContext &Context;
120 llvm::Module &Module;
121 const CodeGenOptions &CodeGenOpts;
122 const LangOptions &Features;
123 MangleContext &MContext;
124
125 // MDHelper - Helper for creating metadata.
126 llvm::MDBuilder MDHelper;
127
128 /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
129 /// them.
130 llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
131 /// This maps clang::Types to a base access type in the type DAG.
132 llvm::DenseMap<const Type *, llvm::MDNode *> BaseTypeMetadataCache;
133 /// This maps TBAA access descriptors to tag nodes.
134 llvm::DenseMap<TBAAAccessInfo, llvm::MDNode *> AccessTagMetadataCache;
135
136 /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
137 /// them for struct assignments.
138 llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
139
140 llvm::MDNode *Root;
141 llvm::MDNode *Char;
142
143 /// getRoot - This is the mdnode for the root of the metadata type graph
144 /// for this translation unit.
145 llvm::MDNode *getRoot();
146
147 /// getChar - This is the mdnode for "char", which is special, and any types
148 /// considered to be equivalent to it.
149 llvm::MDNode *getChar();
150
151 /// CollectFields - Collect information about the fields of a type for
152 /// !tbaa.struct metadata formation. Return false for an unsupported type.
153 bool CollectFields(uint64_t BaseOffset,
154 QualType Ty,
155 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
156 bool MayAlias);
157
158 /// createScalarTypeNode - A wrapper function to create a metadata node
159 /// describing a scalar type.
160 llvm::MDNode *createScalarTypeNode(StringRef Name, llvm::MDNode *Parent,
161 uint64_t Size);
162
163 /// getTypeInfoHelper - An internal helper function to generate metadata used
164 /// to describe accesses to objects of the given type.
165 llvm::MDNode *getTypeInfoHelper(const Type *Ty);
166
167 /// getBaseTypeInfoHelper - An internal helper function to generate metadata
168 /// used to describe accesses to objects of the given base type.
169 llvm::MDNode *getBaseTypeInfoHelper(const Type *Ty);
170
171 public:
172 CodeGenTBAA(ASTContext &Ctx, llvm::Module &M, const CodeGenOptions &CGO,
173 const LangOptions &Features, MangleContext &MContext);
174 ~CodeGenTBAA();
175
176 /// getTypeInfo - Get metadata used to describe accesses to objects of the
177 /// given type.
178 llvm::MDNode *getTypeInfo(QualType QTy);
179
180 /// getAccessInfo - Get TBAA information that describes an access to
181 /// an object of the given type.
182 TBAAAccessInfo getAccessInfo(QualType AccessType);
183
184 /// getVTablePtrAccessInfo - Get the TBAA information that describes an
185 /// access to a virtual table pointer.
186 TBAAAccessInfo getVTablePtrAccessInfo(llvm::Type *VTablePtrType);
187
188 /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
189 /// the given type.
190 llvm::MDNode *getTBAAStructInfo(QualType QTy);
191
192 /// getBaseTypeInfo - Get metadata that describes the given base access type.
193 /// Return null if the type is not suitable for use in TBAA access tags.
194 llvm::MDNode *getBaseTypeInfo(QualType QTy);
195
196 /// getAccessTagInfo - Get TBAA tag for a given memory access.
197 llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info);
198
199 /// mergeTBAAInfoForCast - Get merged TBAA information for the purpose of
200 /// type casts.
201 TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
202 TBAAAccessInfo TargetInfo);
203
204 /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
205 /// purpose of conditional operator.
206 TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
207 TBAAAccessInfo InfoB);
208
209 /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
210 /// purpose of memory transfer calls.
211 TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
212 TBAAAccessInfo SrcInfo);
213 };
214
215 } // end namespace CodeGen
216 } // end namespace clang
217
218 namespace llvm {
219
220 template<> struct DenseMapInfo<clang::CodeGen::TBAAAccessInfo> {
221 static clang::CodeGen::TBAAAccessInfo getEmptyKey() {
222 unsigned UnsignedKey = DenseMapInfo<unsigned>::getEmptyKey();
223 return clang::CodeGen::TBAAAccessInfo(
224 static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
225 DenseMapInfo<MDNode *>::getEmptyKey(),
226 DenseMapInfo<MDNode *>::getEmptyKey(),
227 DenseMapInfo<uint64_t>::getEmptyKey(),
228 DenseMapInfo<uint64_t>::getEmptyKey());
229 }
230
231 static clang::CodeGen::TBAAAccessInfo getTombstoneKey() {
232 unsigned UnsignedKey = DenseMapInfo<unsigned>::getTombstoneKey();
233 return clang::CodeGen::TBAAAccessInfo(
234 static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
235 DenseMapInfo<MDNode *>::getTombstoneKey(),
236 DenseMapInfo<MDNode *>::getTombstoneKey(),
237 DenseMapInfo<uint64_t>::getTombstoneKey(),
238 DenseMapInfo<uint64_t>::getTombstoneKey());
239 }
240
241 static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) {
242 auto KindValue = static_cast<unsigned>(Val.Kind);
243 return DenseMapInfo<unsigned>::getHashValue(KindValue) ^
244 DenseMapInfo<MDNode *>::getHashValue(Val.BaseType) ^
245 DenseMapInfo<MDNode *>::getHashValue(Val.AccessType) ^
246 DenseMapInfo<uint64_t>::getHashValue(Val.Offset) ^
247 DenseMapInfo<uint64_t>::getHashValue(Val.Size);
248 }
249
250 static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS,
251 const clang::CodeGen::TBAAAccessInfo &RHS) {
252 return LHS == RHS;
253 }
254 };
255
256 } // end namespace llvm
257
258 #endif
clang::CodeGen::TBAAAccessInfo::TBAAAccessInfo
TBAAAccessInfo()
Definition: CodeGenTBAA.h:60
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:638
clang::CodeGen::TBAAAccessInfo::TBAAAccessInfo
TBAAAccessInfo(llvm::MDNode *AccessType, uint64_t Size)
Definition: CodeGenTBAA.h:56
llvm
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
Type.h
C Language Family Type Representation.
clang::CodeGen::TBAAAccessInfo::AccessType
llvm::MDNode * AccessType
AccessType - The final access type.
Definition: CodeGenTBAA.h:106
clang::CodeGen::TBAAAccessInfo::Kind
TBAAAccessKind Kind
Kind - The kind of the access descriptor.
Definition: CodeGenTBAA.h:97
llvm::DenseMapInfo< clang::CodeGen::TBAAAccessInfo >::isEqual
static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS, const clang::CodeGen::TBAAAccessInfo &RHS)
Definition: CodeGenTBAA.h:250
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1407
llvm::DenseMapInfo
Definition: TypeOrdering.h:38
clang::CodeGen::TBAAAccessKind::MayAlias
clang::CodeGen::TBAAAccessInfo::isMayAlias
bool isMayAlias() const
Definition: CodeGenTBAA.h:70
clang::CodeGen::TBAAAccessInfo::TBAAAccessInfo
TBAAAccessInfo(TBAAAccessKind Kind, llvm::MDNode *BaseType, llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
Definition: CodeGenTBAA.h:44
clang::CodeGen::TBAAAccessKind::Incomplete
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
clang::Module
Describes a module or submodule.
Definition: Module.h:65
llvm::SmallVectorImpl
Definition: LLVM.h:37
clang::CodeGen::TBAAAccessInfo::isIncomplete
bool isIncomplete() const
Definition: CodeGenTBAA.h:78
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::CodeGen::TBAAAccessKind::Ordinary
clang::CodeGen::TBAAAccessInfo::getIncompleteInfo
static TBAAAccessInfo getIncompleteInfo()
Definition: CodeGenTBAA.h:72
getRoot
static DeltaTreeNode * getRoot(void *Root)
Definition: DeltaTree.cpp:387
Parent
NodeId Parent
Definition: ASTDiff.cpp:192
llvm::DenseMapInfo< clang::CodeGen::TBAAAccessInfo >::getTombstoneKey
static clang::CodeGen::TBAAAccessInfo getTombstoneKey()
Definition: CodeGenTBAA.h:231
Offset
unsigned Offset
Definition: Format.cpp:1631
clang::TargetInfo
Exposes information about the current target.
Definition: TargetInfo.h:54
clang::CodeGen::TBAAAccessKind
TBAAAccessKind
Definition: CodeGenTBAA.h:36
bool
#define bool
Definition: stdbool.h:31
clang::CodeGen::AlignmentSource::Type
The l-value was considered opaque, so the alignment was determined from a type.
Kind
Kind
Definition: ChrootChecker.cpp:29
clang::CodeGen::TBAAAccessInfo::operator==
bool operator==(const TBAAAccessInfo &Other) const
Definition: CodeGenTBAA.h:80
clang::CodeGen::TBAAAccessInfo::BaseType
llvm::MDNode * BaseType
BaseType - The base/leading access type.
Definition: CodeGenTBAA.h:102
clang::MangleContext
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:44
clang::CodeGen::TBAAAccessInfo::Size
uint64_t Size
Size - The size of access, in bytes.
Definition: CodeGenTBAA.h:113
clang::CodeGen::TBAAAccessInfo::operator!=
bool operator!=(const TBAAAccessInfo &Other) const
Definition: CodeGenTBAA.h:88
Type
Optional< types::ID > Type
Definition: InterpolatingCompilationDatabase.cpp:128
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
llvm::DenseMapInfo< clang::CodeGen::TBAAAccessInfo >::getEmptyKey
static clang::CodeGen::TBAAAccessInfo getEmptyKey()
Definition: CodeGenTBAA.h:221
clang::CodeGenOptions
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Definition: CodeGenOptions.h:46
clang::CodeGen::CodeGenTBAA
CodeGenTBAA - This class organizes the cross-module state that is used while lowering AST types to LL...
Definition: CodeGenTBAA.h:118
llvm::DenseMapInfo< clang::CodeGen::TBAAAccessInfo >::getHashValue
static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val)
Definition: CodeGenTBAA.h:241
clang::CodeGen::TBAAAccessInfo
Definition: CodeGenTBAA.h:43
clang::CodeGen::TBAAAccessInfo::Offset
uint64_t Offset
Offset - The byte offset of the final access within the base one.
Definition: CodeGenTBAA.h:110
clang::CodeGen::TBAAAccessInfo::getMayAliasInfo
static TBAAAccessInfo getMayAliasInfo()
Definition: CodeGenTBAA.h:64
clang::CodeGen::TBAAAccessInfo::TBAAAccessInfo
TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
Definition: CodeGenTBAA.h:50