blob: e8e006f41616463dab9aa77c2b01374a06d9ce51 [file] [log] [blame]
Dan Gohman947c9af2010-10-14 23:06:10 +00001//===--- CodeGenTBAA.h - TBAA information for LLVM CodeGen ------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dan Gohman947c9af2010-10-14 23:06:10 +00006//
7//===----------------------------------------------------------------------===//
8//
Dan Gohman5419ce62010-10-21 18:49:12 +00009// This is the code that manages TBAA information and defines the TBAA policy
10// for the optimizer to use.
Dan Gohman947c9af2010-10-14 23:06:10 +000011//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
15#define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
Dan Gohman947c9af2010-10-14 23:06:10 +000016
Benjamin Kramer8fdba912016-02-02 14:24:21 +000017#include "clang/AST/Type.h"
Chris Lattner01cf8db2011-07-20 06:58:45 +000018#include "clang/Basic/LLVM.h"
Dan Gohman947c9af2010-10-14 23:06:10 +000019#include "llvm/ADT/DenseMap.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000020#include "llvm/IR/MDBuilder.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000021#include "llvm/IR/Metadata.h"
Dan Gohman947c9af2010-10-14 23:06:10 +000022
23namespace clang {
24 class ASTContext;
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000025 class CodeGenOptions;
Dan Gohman947c9af2010-10-14 23:06:10 +000026 class LangOptions;
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000027 class MangleContext;
Dan Gohman947c9af2010-10-14 23:06:10 +000028 class QualType;
29 class Type;
30
31namespace CodeGen {
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000032class CGRecordLayout;
Dan Gohman947c9af2010-10-14 23:06:10 +000033
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000034// TBAAAccessKind - A kind of TBAA memory access descriptor.
35enum class TBAAAccessKind : unsigned {
Hal Finkela5986b92017-12-03 03:10:13 +000036 Ordinary,
37 MayAlias,
38 Incomplete,
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000039};
40
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000041// TBAAAccessInfo - Describes a memory access in terms of TBAA.
42struct TBAAAccessInfo {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000043 TBAAAccessInfo(TBAAAccessKind Kind, llvm::MDNode *BaseType,
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000044 llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
45 : Kind(Kind), BaseType(BaseType), AccessType(AccessType),
46 Offset(Offset), Size(Size)
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000047 {}
48
Ivan A. Kosarev383890b2017-10-06 08:17:48 +000049 TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType,
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000050 uint64_t Offset, uint64_t Size)
51 : TBAAAccessInfo(TBAAAccessKind::Ordinary, BaseType, AccessType,
52 Offset, Size)
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000053 {}
54
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000055 explicit TBAAAccessInfo(llvm::MDNode *AccessType, uint64_t Size)
56 : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType, /* Offset= */ 0, Size)
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000057 {}
58
59 TBAAAccessInfo()
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000060 : TBAAAccessInfo(/* AccessType= */ nullptr, /* Size= */ 0)
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000061 {}
62
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000063 static TBAAAccessInfo getMayAliasInfo() {
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000064 return TBAAAccessInfo(TBAAAccessKind::MayAlias,
65 /* BaseType= */ nullptr, /* AccessType= */ nullptr,
66 /* Offset= */ 0, /* Size= */ 0);
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000067 }
68
69 bool isMayAlias() const { return Kind == TBAAAccessKind::MayAlias; }
70
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000071 static TBAAAccessInfo getIncompleteInfo() {
72 return TBAAAccessInfo(TBAAAccessKind::Incomplete,
73 /* BaseType= */ nullptr, /* AccessType= */ nullptr,
74 /* Offset= */ 0, /* Size= */ 0);
75 }
76
77 bool isIncomplete() const { return Kind == TBAAAccessKind::Incomplete; }
78
Ivan A. Kosareved141ba2017-10-17 09:12:13 +000079 bool operator==(const TBAAAccessInfo &Other) const {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000080 return Kind == Other.Kind &&
81 BaseType == Other.BaseType &&
Ivan A. Kosareved141ba2017-10-17 09:12:13 +000082 AccessType == Other.AccessType &&
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000083 Offset == Other.Offset &&
84 Size == Other.Size;
Ivan A. Kosareved141ba2017-10-17 09:12:13 +000085 }
86
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000087 bool operator!=(const TBAAAccessInfo &Other) const {
88 return !(*this == Other);
89 }
90
91 explicit operator bool() const {
92 return *this != TBAAAccessInfo();
93 }
94
95 /// Kind - The kind of the access descriptor.
96 TBAAAccessKind Kind;
97
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000098 /// BaseType - The base/leading access type. May be null if this access
99 /// descriptor represents an access that is not considered to be an access
100 /// to an aggregate or union member.
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000101 llvm::MDNode *BaseType;
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000102
103 /// AccessType - The final access type. May be null if there is no TBAA
104 /// information available about this access.
105 llvm::MDNode *AccessType;
106
107 /// Offset - The byte offset of the final access within the base one. Must be
108 /// zero if the base access type is not specified.
109 uint64_t Offset;
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000110
111 /// Size - The size of access, in bytes.
112 uint64_t Size;
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000113};
Manman Renc451e572013-04-04 21:53:22 +0000114
Dan Gohman947c9af2010-10-14 23:06:10 +0000115/// CodeGenTBAA - This class organizes the cross-module state that is used
116/// while lowering AST types to LLVM types.
117class CodeGenTBAA {
118 ASTContext &Context;
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000119 llvm::Module &Module;
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +0000120 const CodeGenOptions &CodeGenOpts;
Dan Gohman947c9af2010-10-14 23:06:10 +0000121 const LangOptions &Features;
Dan Gohman2e29eb52010-10-15 20:23:12 +0000122 MangleContext &MContext;
Dan Gohman947c9af2010-10-14 23:06:10 +0000123
Duncan Sandsc720e782012-04-15 18:04:54 +0000124 // MDHelper - Helper for creating metadata.
125 llvm::MDBuilder MDHelper;
126
Manman Renc451e572013-04-04 21:53:22 +0000127 /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
128 /// them.
Dan Gohman947c9af2010-10-14 23:06:10 +0000129 llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000130 /// This maps clang::Types to a base access type in the type DAG.
131 llvm::DenseMap<const Type *, llvm::MDNode *> BaseTypeMetadataCache;
132 /// This maps TBAA access descriptors to tag nodes.
133 llvm::DenseMap<TBAAAccessInfo, llvm::MDNode *> AccessTagMetadataCache;
Dan Gohman947c9af2010-10-14 23:06:10 +0000134
Dan Gohman22695fc2012-09-28 21:58:29 +0000135 /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
136 /// them for struct assignments.
137 llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
138
Dan Gohman947c9af2010-10-14 23:06:10 +0000139 llvm::MDNode *Root;
Dan Gohman947c9af2010-10-14 23:06:10 +0000140 llvm::MDNode *Char;
141
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000142 /// getRoot - This is the mdnode for the root of the metadata type graph
143 /// for this translation unit.
144 llvm::MDNode *getRoot();
145
146 /// getChar - This is the mdnode for "char", which is special, and any types
147 /// considered to be equivalent to it.
148 llvm::MDNode *getChar();
149
Dan Gohman22695fc2012-09-28 21:58:29 +0000150 /// CollectFields - Collect information about the fields of a type for
151 /// !tbaa.struct metadata formation. Return false for an unsupported type.
152 bool CollectFields(uint64_t BaseOffset,
153 QualType Ty,
154 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
155 bool MayAlias);
156
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000157 /// createScalarTypeNode - A wrapper function to create a metadata node
158 /// describing a scalar type.
159 llvm::MDNode *createScalarTypeNode(StringRef Name, llvm::MDNode *Parent,
160 uint64_t Size);
Manman Rene1ad74e2013-04-11 23:02:56 +0000161
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000162 /// getTypeInfoHelper - An internal helper function to generate metadata used
163 /// to describe accesses to objects of the given type.
164 llvm::MDNode *getTypeInfoHelper(const Type *Ty);
165
166 /// getBaseTypeInfoHelper - An internal helper function to generate metadata
167 /// used to describe accesses to objects of the given base type.
168 llvm::MDNode *getBaseTypeInfoHelper(const Type *Ty);
169
Dan Gohman947c9af2010-10-14 23:06:10 +0000170public:
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000171 CodeGenTBAA(ASTContext &Ctx, llvm::Module &M, const CodeGenOptions &CGO,
172 const LangOptions &Features, MangleContext &MContext);
Dan Gohman947c9af2010-10-14 23:06:10 +0000173 ~CodeGenTBAA();
174
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000175 /// getTypeInfo - Get metadata used to describe accesses to objects of the
176 /// given type.
177 llvm::MDNode *getTypeInfo(QualType QTy);
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000178
Ivan A. Kosarev124a2182018-02-20 12:33:04 +0000179 /// getAccessInfo - Get TBAA information that describes an access to
180 /// an object of the given type.
181 TBAAAccessInfo getAccessInfo(QualType AccessType);
182
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000183 /// getVTablePtrAccessInfo - Get the TBAA information that describes an
184 /// access to a virtual table pointer.
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000185 TBAAAccessInfo getVTablePtrAccessInfo(llvm::Type *VTablePtrType);
Dan Gohman22695fc2012-09-28 21:58:29 +0000186
187 /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
188 /// the given type.
189 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renc451e572013-04-04 21:53:22 +0000190
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000191 /// getBaseTypeInfo - Get metadata that describes the given base access type.
192 /// Return null if the type is not suitable for use in TBAA access tags.
193 llvm::MDNode *getBaseTypeInfo(QualType QTy);
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000194
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000195 /// getAccessTagInfo - Get TBAA tag for a given memory access.
196 llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info);
Manman Rene1ad74e2013-04-11 23:02:56 +0000197
Ivan A. Kosareved141ba2017-10-17 09:12:13 +0000198 /// mergeTBAAInfoForCast - Get merged TBAA information for the purpose of
199 /// type casts.
200 TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
201 TBAAAccessInfo TargetInfo);
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000202
203 /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
204 /// purpose of conditional operator.
205 TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
206 TBAAAccessInfo InfoB);
Ivan A. Kosarev1860b522018-01-25 14:21:55 +0000207
208 /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
209 /// purpose of memory transfer calls.
210 TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
211 TBAAAccessInfo SrcInfo);
Dan Gohman947c9af2010-10-14 23:06:10 +0000212};
213
214} // end namespace CodeGen
215} // end namespace clang
216
Manman Renc451e572013-04-04 21:53:22 +0000217namespace llvm {
218
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000219template<> struct DenseMapInfo<clang::CodeGen::TBAAAccessInfo> {
220 static clang::CodeGen::TBAAAccessInfo getEmptyKey() {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000221 unsigned UnsignedKey = DenseMapInfo<unsigned>::getEmptyKey();
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000222 return clang::CodeGen::TBAAAccessInfo(
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000223 static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000224 DenseMapInfo<MDNode *>::getEmptyKey(),
225 DenseMapInfo<MDNode *>::getEmptyKey(),
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000226 DenseMapInfo<uint64_t>::getEmptyKey(),
Manman Renc451e572013-04-04 21:53:22 +0000227 DenseMapInfo<uint64_t>::getEmptyKey());
228 }
229
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000230 static clang::CodeGen::TBAAAccessInfo getTombstoneKey() {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000231 unsigned UnsignedKey = DenseMapInfo<unsigned>::getTombstoneKey();
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000232 return clang::CodeGen::TBAAAccessInfo(
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000233 static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000234 DenseMapInfo<MDNode *>::getTombstoneKey(),
235 DenseMapInfo<MDNode *>::getTombstoneKey(),
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000236 DenseMapInfo<uint64_t>::getTombstoneKey(),
Manman Renc451e572013-04-04 21:53:22 +0000237 DenseMapInfo<uint64_t>::getTombstoneKey());
238 }
239
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000240 static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000241 auto KindValue = static_cast<unsigned>(Val.Kind);
242 return DenseMapInfo<unsigned>::getHashValue(KindValue) ^
243 DenseMapInfo<MDNode *>::getHashValue(Val.BaseType) ^
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000244 DenseMapInfo<MDNode *>::getHashValue(Val.AccessType) ^
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000245 DenseMapInfo<uint64_t>::getHashValue(Val.Offset) ^
246 DenseMapInfo<uint64_t>::getHashValue(Val.Size);
Manman Renc451e572013-04-04 21:53:22 +0000247 }
248
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000249 static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS,
250 const clang::CodeGen::TBAAAccessInfo &RHS) {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000251 return LHS == RHS;
Manman Renc451e572013-04-04 21:53:22 +0000252 }
253};
254
255} // end namespace llvm
256
Dan Gohman947c9af2010-10-14 23:06:10 +0000257#endif