blob: 8fc0c72fcbcce734e1bf6ffbdf3eeb00012c1293 [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. Kosareva511ed72017-10-03 10:52:39 +000035// TBAAAccessInfo - Describes a memory access in terms of TBAA.
36struct TBAAAccessInfo {
Ivan A. Kosarev383890b2017-10-06 08:17:48 +000037 TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType,
38 uint64_t Offset)
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000039 : BaseType(BaseType), AccessType(AccessType), Offset(Offset)
40 {}
41
42 explicit TBAAAccessInfo(llvm::MDNode *AccessType)
Ivan A. Kosarev383890b2017-10-06 08:17:48 +000043 : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType, /* Offset= */ 0)
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000044 {}
45
46 TBAAAccessInfo()
47 : TBAAAccessInfo(/* AccessType= */ nullptr)
48 {}
49
50 /// BaseType - The base/leading access type. May be null if this access
51 /// descriptor represents an access that is not considered to be an access
52 /// to an aggregate or union member.
Ivan A. Kosarev383890b2017-10-06 08:17:48 +000053 llvm::MDNode *BaseType;
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000054
55 /// AccessType - The final access type. May be null if there is no TBAA
56 /// information available about this access.
57 llvm::MDNode *AccessType;
58
59 /// Offset - The byte offset of the final access within the base one. Must be
60 /// zero if the base access type is not specified.
61 uint64_t Offset;
62};
Manman Renc451e572013-04-04 21:53:22 +000063
Dan Gohman947c9af2010-10-14 23:06:10 +000064/// CodeGenTBAA - This class organizes the cross-module state that is used
65/// while lowering AST types to LLVM types.
66class CodeGenTBAA {
67 ASTContext &Context;
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000068 const CodeGenOptions &CodeGenOpts;
Dan Gohman947c9af2010-10-14 23:06:10 +000069 const LangOptions &Features;
Dan Gohman2e29eb52010-10-15 20:23:12 +000070 MangleContext &MContext;
Dan Gohman947c9af2010-10-14 23:06:10 +000071
Duncan Sandsc720e782012-04-15 18:04:54 +000072 // MDHelper - Helper for creating metadata.
73 llvm::MDBuilder MDHelper;
74
Manman Renc451e572013-04-04 21:53:22 +000075 /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
76 /// them.
Dan Gohman947c9af2010-10-14 23:06:10 +000077 llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
Ivan A. Kosarev383890b2017-10-06 08:17:48 +000078 /// This maps clang::Types to a base access type in the type DAG.
79 llvm::DenseMap<const Type *, llvm::MDNode *> BaseTypeMetadataCache;
80 /// This maps TBAA access descriptors to tag nodes.
81 llvm::DenseMap<TBAAAccessInfo, llvm::MDNode *> AccessTagMetadataCache;
Dan Gohman947c9af2010-10-14 23:06:10 +000082
Dan Gohman22695fc2012-09-28 21:58:29 +000083 /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
84 /// them for struct assignments.
85 llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
86
Dan Gohman947c9af2010-10-14 23:06:10 +000087 llvm::MDNode *Root;
Dan Gohman947c9af2010-10-14 23:06:10 +000088 llvm::MDNode *Char;
89
Dan Gohman7dfd13c2010-10-25 21:48:30 +000090 /// getRoot - This is the mdnode for the root of the metadata type graph
91 /// for this translation unit.
92 llvm::MDNode *getRoot();
93
94 /// getChar - This is the mdnode for "char", which is special, and any types
95 /// considered to be equivalent to it.
96 llvm::MDNode *getChar();
97
Dan Gohman22695fc2012-09-28 21:58:29 +000098 /// CollectFields - Collect information about the fields of a type for
99 /// !tbaa.struct metadata formation. Return false for an unsupported type.
100 bool CollectFields(uint64_t BaseOffset,
101 QualType Ty,
102 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
103 bool MayAlias);
104
Manman Rene1ad74e2013-04-11 23:02:56 +0000105 /// A wrapper function to create a scalar type. For struct-path aware TBAA,
106 /// the scalar type has the same format as the struct type: name, offset,
107 /// pointer to another node in the type DAG.
108 llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
109
Dan Gohman947c9af2010-10-14 23:06:10 +0000110public:
111 CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +0000112 const CodeGenOptions &CGO,
Dan Gohman2e29eb52010-10-15 20:23:12 +0000113 const LangOptions &Features,
114 MangleContext &MContext);
Dan Gohman947c9af2010-10-14 23:06:10 +0000115 ~CodeGenTBAA();
116
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000117 /// getTypeInfo - Get metadata used to describe accesses to objects of the
118 /// given type.
119 llvm::MDNode *getTypeInfo(QualType QTy);
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000120
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000121 /// getVTablePtrAccessInfo - Get the TBAA information that describes an
122 /// access to a virtual table pointer.
123 TBAAAccessInfo getVTablePtrAccessInfo();
Dan Gohman22695fc2012-09-28 21:58:29 +0000124
125 /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
126 /// the given type.
127 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renc451e572013-04-04 21:53:22 +0000128
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000129 /// getBaseTypeInfo - Get metadata that describes the given base access type.
130 /// Return null if the type is not suitable for use in TBAA access tags.
131 llvm::MDNode *getBaseTypeInfo(QualType QTy);
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000132
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000133 /// getAccessTagInfo - Get TBAA tag for a given memory access.
134 llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info);
Manman Rene1ad74e2013-04-11 23:02:56 +0000135
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000136 /// getMayAliasAccessInfo - Get TBAA information that represents may-alias
Ivan A. Kosarev5c8e7592017-10-02 11:10:04 +0000137 /// accesses.
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000138 TBAAAccessInfo getMayAliasAccessInfo();
Dan Gohman947c9af2010-10-14 23:06:10 +0000139};
140
141} // end namespace CodeGen
142} // end namespace clang
143
Manman Renc451e572013-04-04 21:53:22 +0000144namespace llvm {
145
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000146template<> struct DenseMapInfo<clang::CodeGen::TBAAAccessInfo> {
147 static clang::CodeGen::TBAAAccessInfo getEmptyKey() {
148 return clang::CodeGen::TBAAAccessInfo(
149 DenseMapInfo<MDNode *>::getEmptyKey(),
150 DenseMapInfo<MDNode *>::getEmptyKey(),
Manman Renc451e572013-04-04 21:53:22 +0000151 DenseMapInfo<uint64_t>::getEmptyKey());
152 }
153
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000154 static clang::CodeGen::TBAAAccessInfo getTombstoneKey() {
155 return clang::CodeGen::TBAAAccessInfo(
156 DenseMapInfo<MDNode *>::getTombstoneKey(),
157 DenseMapInfo<MDNode *>::getTombstoneKey(),
Manman Renc451e572013-04-04 21:53:22 +0000158 DenseMapInfo<uint64_t>::getTombstoneKey());
159 }
160
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000161 static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) {
162 return DenseMapInfo<MDNode *>::getHashValue(Val.BaseType) ^
163 DenseMapInfo<MDNode *>::getHashValue(Val.AccessType) ^
Manman Renc451e572013-04-04 21:53:22 +0000164 DenseMapInfo<uint64_t>::getHashValue(Val.Offset);
165 }
166
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000167 static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS,
168 const clang::CodeGen::TBAAAccessInfo &RHS) {
169 return LHS.BaseType == RHS.BaseType &&
170 LHS.AccessType == RHS.AccessType &&
Manman Renc451e572013-04-04 21:53:22 +0000171 LHS.Offset == RHS.Offset;
172 }
173};
174
175} // end namespace llvm
176
Dan Gohman947c9af2010-10-14 23:06:10 +0000177#endif