blob: a5b1f66bcd1a1ac9ff0082dcc953db36f53a3aa3 [file] [log] [blame]
Dan Gohman947c9af2010-10-14 23:06:10 +00001//===--- 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//
Dan Gohman5419ce62010-10-21 18:49:12 +000010// This is the code that manages TBAA information and defines the TBAA policy
11// for the optimizer to use.
Dan Gohman947c9af2010-10-14 23:06:10 +000012//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
16#define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
Dan Gohman947c9af2010-10-14 23:06:10 +000017
Benjamin Kramer8fdba912016-02-02 14:24:21 +000018#include "clang/AST/Type.h"
Chris Lattner01cf8db2011-07-20 06:58:45 +000019#include "clang/Basic/LLVM.h"
Dan Gohman947c9af2010-10-14 23:06:10 +000020#include "llvm/ADT/DenseMap.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000021#include "llvm/IR/MDBuilder.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000022#include "llvm/IR/Metadata.h"
Dan Gohman947c9af2010-10-14 23:06:10 +000023
24namespace clang {
25 class ASTContext;
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000026 class CodeGenOptions;
Dan Gohman947c9af2010-10-14 23:06:10 +000027 class LangOptions;
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000028 class MangleContext;
Dan Gohman947c9af2010-10-14 23:06:10 +000029 class QualType;
30 class Type;
31
32namespace CodeGen {
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000033class CGRecordLayout;
Dan Gohman947c9af2010-10-14 23:06:10 +000034
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000035// TBAAAccessKind - A kind of TBAA memory access descriptor.
36enum class TBAAAccessKind : unsigned {
Hal Finkela5986b92017-12-03 03:10:13 +000037 Ordinary,
38 MayAlias,
39 Incomplete,
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000040};
41
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000042// TBAAAccessInfo - Describes a memory access in terms of TBAA.
43struct TBAAAccessInfo {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000044 TBAAAccessInfo(TBAAAccessKind Kind, llvm::MDNode *BaseType,
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000045 llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
46 : Kind(Kind), BaseType(BaseType), AccessType(AccessType),
47 Offset(Offset), Size(Size)
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000048 {}
49
Ivan A. Kosarev383890b2017-10-06 08:17:48 +000050 TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType,
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000051 uint64_t Offset, uint64_t Size)
52 : TBAAAccessInfo(TBAAAccessKind::Ordinary, BaseType, AccessType,
53 Offset, Size)
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000054 {}
55
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000056 explicit TBAAAccessInfo(llvm::MDNode *AccessType, uint64_t Size)
57 : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType, /* Offset= */ 0, Size)
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000058 {}
59
60 TBAAAccessInfo()
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000061 : TBAAAccessInfo(/* AccessType= */ nullptr, /* Size= */ 0)
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000062 {}
63
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000064 static TBAAAccessInfo getMayAliasInfo() {
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000065 return TBAAAccessInfo(TBAAAccessKind::MayAlias,
66 /* BaseType= */ nullptr, /* AccessType= */ nullptr,
67 /* Offset= */ 0, /* Size= */ 0);
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000068 }
69
70 bool isMayAlias() const { return Kind == TBAAAccessKind::MayAlias; }
71
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000072 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
Ivan A. Kosareved141ba2017-10-17 09:12:13 +000080 bool operator==(const TBAAAccessInfo &Other) const {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000081 return Kind == Other.Kind &&
82 BaseType == Other.BaseType &&
Ivan A. Kosareved141ba2017-10-17 09:12:13 +000083 AccessType == Other.AccessType &&
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000084 Offset == Other.Offset &&
85 Size == Other.Size;
Ivan A. Kosareved141ba2017-10-17 09:12:13 +000086 }
87
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000088 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
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000099 /// 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.
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000102 llvm::MDNode *BaseType;
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000103
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;
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000111
112 /// Size - The size of access, in bytes.
113 uint64_t Size;
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000114};
Manman Renc451e572013-04-04 21:53:22 +0000115
Dan Gohman947c9af2010-10-14 23:06:10 +0000116/// CodeGenTBAA - This class organizes the cross-module state that is used
117/// while lowering AST types to LLVM types.
118class CodeGenTBAA {
119 ASTContext &Context;
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000120 llvm::Module &Module;
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +0000121 const CodeGenOptions &CodeGenOpts;
Dan Gohman947c9af2010-10-14 23:06:10 +0000122 const LangOptions &Features;
Dan Gohman2e29eb52010-10-15 20:23:12 +0000123 MangleContext &MContext;
Dan Gohman947c9af2010-10-14 23:06:10 +0000124
Duncan Sandsc720e782012-04-15 18:04:54 +0000125 // MDHelper - Helper for creating metadata.
126 llvm::MDBuilder MDHelper;
127
Manman Renc451e572013-04-04 21:53:22 +0000128 /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
129 /// them.
Dan Gohman947c9af2010-10-14 23:06:10 +0000130 llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000131 /// 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;
Dan Gohman947c9af2010-10-14 23:06:10 +0000135
Dan Gohman22695fc2012-09-28 21:58:29 +0000136 /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
137 /// them for struct assignments.
138 llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
139
Dan Gohman947c9af2010-10-14 23:06:10 +0000140 llvm::MDNode *Root;
Dan Gohman947c9af2010-10-14 23:06:10 +0000141 llvm::MDNode *Char;
142
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000143 /// 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
Dan Gohman22695fc2012-09-28 21:58:29 +0000151 /// 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
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000158 /// 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);
Manman Rene1ad74e2013-04-11 23:02:56 +0000162
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000163 /// 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
Dan Gohman947c9af2010-10-14 23:06:10 +0000171public:
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000172 CodeGenTBAA(ASTContext &Ctx, llvm::Module &M, const CodeGenOptions &CGO,
173 const LangOptions &Features, MangleContext &MContext);
Dan Gohman947c9af2010-10-14 23:06:10 +0000174 ~CodeGenTBAA();
175
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000176 /// getTypeInfo - Get metadata used to describe accesses to objects of the
177 /// given type.
178 llvm::MDNode *getTypeInfo(QualType QTy);
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000179
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000180 /// getVTablePtrAccessInfo - Get the TBAA information that describes an
181 /// access to a virtual table pointer.
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000182 TBAAAccessInfo getVTablePtrAccessInfo(llvm::Type *VTablePtrType);
Dan Gohman22695fc2012-09-28 21:58:29 +0000183
184 /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
185 /// the given type.
186 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renc451e572013-04-04 21:53:22 +0000187
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000188 /// getBaseTypeInfo - Get metadata that describes the given base access type.
189 /// Return null if the type is not suitable for use in TBAA access tags.
190 llvm::MDNode *getBaseTypeInfo(QualType QTy);
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000191
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000192 /// getAccessTagInfo - Get TBAA tag for a given memory access.
193 llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info);
Manman Rene1ad74e2013-04-11 23:02:56 +0000194
Ivan A. Kosareved141ba2017-10-17 09:12:13 +0000195 /// mergeTBAAInfoForCast - Get merged TBAA information for the purpose of
196 /// type casts.
197 TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
198 TBAAAccessInfo TargetInfo);
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000199
200 /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
201 /// purpose of conditional operator.
202 TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
203 TBAAAccessInfo InfoB);
Dan Gohman947c9af2010-10-14 23:06:10 +0000204};
205
206} // end namespace CodeGen
207} // end namespace clang
208
Manman Renc451e572013-04-04 21:53:22 +0000209namespace llvm {
210
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000211template<> struct DenseMapInfo<clang::CodeGen::TBAAAccessInfo> {
212 static clang::CodeGen::TBAAAccessInfo getEmptyKey() {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000213 unsigned UnsignedKey = DenseMapInfo<unsigned>::getEmptyKey();
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000214 return clang::CodeGen::TBAAAccessInfo(
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000215 static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000216 DenseMapInfo<MDNode *>::getEmptyKey(),
217 DenseMapInfo<MDNode *>::getEmptyKey(),
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000218 DenseMapInfo<uint64_t>::getEmptyKey(),
Manman Renc451e572013-04-04 21:53:22 +0000219 DenseMapInfo<uint64_t>::getEmptyKey());
220 }
221
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000222 static clang::CodeGen::TBAAAccessInfo getTombstoneKey() {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000223 unsigned UnsignedKey = DenseMapInfo<unsigned>::getTombstoneKey();
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000224 return clang::CodeGen::TBAAAccessInfo(
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000225 static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000226 DenseMapInfo<MDNode *>::getTombstoneKey(),
227 DenseMapInfo<MDNode *>::getTombstoneKey(),
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000228 DenseMapInfo<uint64_t>::getTombstoneKey(),
Manman Renc451e572013-04-04 21:53:22 +0000229 DenseMapInfo<uint64_t>::getTombstoneKey());
230 }
231
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000232 static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000233 auto KindValue = static_cast<unsigned>(Val.Kind);
234 return DenseMapInfo<unsigned>::getHashValue(KindValue) ^
235 DenseMapInfo<MDNode *>::getHashValue(Val.BaseType) ^
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000236 DenseMapInfo<MDNode *>::getHashValue(Val.AccessType) ^
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000237 DenseMapInfo<uint64_t>::getHashValue(Val.Offset) ^
238 DenseMapInfo<uint64_t>::getHashValue(Val.Size);
Manman Renc451e572013-04-04 21:53:22 +0000239 }
240
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000241 static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS,
242 const clang::CodeGen::TBAAAccessInfo &RHS) {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000243 return LHS == RHS;
Manman Renc451e572013-04-04 21:53:22 +0000244 }
245};
246
247} // end namespace llvm
248
Dan Gohman947c9af2010-10-14 23:06:10 +0000249#endif