blob: 71c53d43f793feaabddef0a2639b592ea48d2b6d [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 {
Dan Gohman947c9af2010-10-14 23:06:10 +000033 class CGRecordLayout;
34
Manman Renc451e572013-04-04 21:53:22 +000035 struct TBAAPathTag {
36 TBAAPathTag(const Type *B, const llvm::MDNode *A, uint64_t O)
37 : BaseT(B), AccessN(A), Offset(O) {}
38 const Type *BaseT;
39 const llvm::MDNode *AccessN;
40 uint64_t Offset;
41 };
42
Dan Gohman947c9af2010-10-14 23:06:10 +000043/// CodeGenTBAA - This class organizes the cross-module state that is used
44/// while lowering AST types to LLVM types.
45class CodeGenTBAA {
46 ASTContext &Context;
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000047 const CodeGenOptions &CodeGenOpts;
Dan Gohman947c9af2010-10-14 23:06:10 +000048 const LangOptions &Features;
Dan Gohman2e29eb52010-10-15 20:23:12 +000049 MangleContext &MContext;
Dan Gohman947c9af2010-10-14 23:06:10 +000050
Duncan Sandsc720e782012-04-15 18:04:54 +000051 // MDHelper - Helper for creating metadata.
52 llvm::MDBuilder MDHelper;
53
Manman Renc451e572013-04-04 21:53:22 +000054 /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
55 /// them.
Dan Gohman947c9af2010-10-14 23:06:10 +000056 llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
Manman Renc451e572013-04-04 21:53:22 +000057 /// This maps clang::Types to a struct node in the type DAG.
58 llvm::DenseMap<const Type *, llvm::MDNode *> StructTypeMetadataCache;
59 /// This maps TBAAPathTags to a tag node.
60 llvm::DenseMap<TBAAPathTag, llvm::MDNode *> StructTagMetadataCache;
Manman Rene1ad74e2013-04-11 23:02:56 +000061 /// This maps a scalar type to a scalar tag node.
62 llvm::DenseMap<const llvm::MDNode *, llvm::MDNode *> ScalarTagMetadataCache;
Dan Gohman947c9af2010-10-14 23:06:10 +000063
Dan Gohman22695fc2012-09-28 21:58:29 +000064 /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
65 /// them for struct assignments.
66 llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
67
Dan Gohman947c9af2010-10-14 23:06:10 +000068 llvm::MDNode *Root;
Dan Gohman947c9af2010-10-14 23:06:10 +000069 llvm::MDNode *Char;
70
Dan Gohman7dfd13c2010-10-25 21:48:30 +000071 /// getRoot - This is the mdnode for the root of the metadata type graph
72 /// for this translation unit.
73 llvm::MDNode *getRoot();
74
75 /// getChar - This is the mdnode for "char", which is special, and any types
76 /// considered to be equivalent to it.
77 llvm::MDNode *getChar();
78
Dan Gohman22695fc2012-09-28 21:58:29 +000079 /// CollectFields - Collect information about the fields of a type for
80 /// !tbaa.struct metadata formation. Return false for an unsupported type.
81 bool CollectFields(uint64_t BaseOffset,
82 QualType Ty,
83 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
84 bool MayAlias);
85
Manman Rene1ad74e2013-04-11 23:02:56 +000086 /// A wrapper function to create a scalar type. For struct-path aware TBAA,
87 /// the scalar type has the same format as the struct type: name, offset,
88 /// pointer to another node in the type DAG.
89 llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
90
Dan Gohman947c9af2010-10-14 23:06:10 +000091public:
92 CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000093 const CodeGenOptions &CGO,
Dan Gohman2e29eb52010-10-15 20:23:12 +000094 const LangOptions &Features,
95 MangleContext &MContext);
Dan Gohman947c9af2010-10-14 23:06:10 +000096 ~CodeGenTBAA();
97
Ivan A. Kosarev289574e2017-10-02 09:54:47 +000098 /// getTypeInfo - Get metadata used to describe accesses to objects of the
99 /// given type.
100 llvm::MDNode *getTypeInfo(QualType QTy);
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000101
102 /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a
103 /// dereference of a vtable pointer.
104 llvm::MDNode *getTBAAInfoForVTablePtr();
Dan Gohman22695fc2012-09-28 21:58:29 +0000105
106 /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
107 /// the given type.
108 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renc451e572013-04-04 21:53:22 +0000109
110 /// Get the MDNode in the type DAG for given struct type QType.
111 llvm::MDNode *getTBAAStructTypeInfo(QualType QType);
Manman Rene1ad74e2013-04-11 23:02:56 +0000112 /// Get the tag MDNode for a given base type, the actual scalar access MDNode
Manman Renc451e572013-04-04 21:53:22 +0000113 /// and offset into the base type.
114 llvm::MDNode *getTBAAStructTagInfo(QualType BaseQType,
115 llvm::MDNode *AccessNode, uint64_t Offset);
Manman Rene1ad74e2013-04-11 23:02:56 +0000116
Michael Gottesmanabfeabb2013-06-08 00:27:19 +0000117 /// Get the scalar tag MDNode for a given scalar type.
Manman Rene1ad74e2013-04-11 23:02:56 +0000118 llvm::MDNode *getTBAAScalarTagInfo(llvm::MDNode *AccessNode);
Ivan A. Kosarev5c8e7592017-10-02 11:10:04 +0000119
120 /// getMayAliasTypeInfo - Get TBAA information that represents may-alias
121 /// accesses.
122 llvm::MDNode *getMayAliasTypeInfo();
Dan Gohman947c9af2010-10-14 23:06:10 +0000123};
124
125} // end namespace CodeGen
126} // end namespace clang
127
Manman Renc451e572013-04-04 21:53:22 +0000128namespace llvm {
129
130template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> {
131 static clang::CodeGen::TBAAPathTag getEmptyKey() {
132 return clang::CodeGen::TBAAPathTag(
133 DenseMapInfo<const clang::Type *>::getEmptyKey(),
134 DenseMapInfo<const MDNode *>::getEmptyKey(),
135 DenseMapInfo<uint64_t>::getEmptyKey());
136 }
137
138 static clang::CodeGen::TBAAPathTag getTombstoneKey() {
139 return clang::CodeGen::TBAAPathTag(
140 DenseMapInfo<const clang::Type *>::getTombstoneKey(),
141 DenseMapInfo<const MDNode *>::getTombstoneKey(),
142 DenseMapInfo<uint64_t>::getTombstoneKey());
143 }
144
145 static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) {
146 return DenseMapInfo<const clang::Type *>::getHashValue(Val.BaseT) ^
147 DenseMapInfo<const MDNode *>::getHashValue(Val.AccessN) ^
148 DenseMapInfo<uint64_t>::getHashValue(Val.Offset);
149 }
150
151 static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS,
152 const clang::CodeGen::TBAAPathTag &RHS) {
153 return LHS.BaseT == RHS.BaseT &&
154 LHS.AccessN == RHS.AccessN &&
155 LHS.Offset == RHS.Offset;
156 }
157};
158
159} // end namespace llvm
160
Dan Gohman947c9af2010-10-14 23:06:10 +0000161#endif