blob: 9ddc3aa97006cfd7b6366adec3a39a2f8e6e39aa [file] [log] [blame]
Dan Gohman3d5aff52010-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 Gohman565cc442010-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 Gohman3d5aff52010-10-14 23:06:10 +000012//
13//===----------------------------------------------------------------------===//
14
15#ifndef CLANG_CODEGEN_CODEGENTBAA_H
16#define CLANG_CODEGEN_CODEGENTBAA_H
17
Chris Lattner686775d2011-07-20 06:58:45 +000018#include "clang/Basic/LLVM.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000019#include "llvm/ADT/DenseMap.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000020#include "llvm/IR/MDBuilder.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000021
22namespace llvm {
23 class LLVMContext;
24 class MDNode;
25}
26
27namespace clang {
28 class ASTContext;
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000029 class CodeGenOptions;
Dan Gohman3d5aff52010-10-14 23:06:10 +000030 class LangOptions;
Peter Collingbourne14110472011-01-13 18:57:25 +000031 class MangleContext;
Dan Gohman3d5aff52010-10-14 23:06:10 +000032 class QualType;
33 class Type;
34
35namespace CodeGen {
Dan Gohman3d5aff52010-10-14 23:06:10 +000036 class CGRecordLayout;
37
Manman Renb37a73d2013-04-04 21:53:22 +000038 struct TBAAPathTag {
39 TBAAPathTag(const Type *B, const llvm::MDNode *A, uint64_t O)
40 : BaseT(B), AccessN(A), Offset(O) {}
41 const Type *BaseT;
42 const llvm::MDNode *AccessN;
43 uint64_t Offset;
44 };
45
Dan Gohman3d5aff52010-10-14 23:06:10 +000046/// CodeGenTBAA - This class organizes the cross-module state that is used
47/// while lowering AST types to LLVM types.
48class CodeGenTBAA {
49 ASTContext &Context;
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000050 const CodeGenOptions &CodeGenOpts;
Dan Gohman3d5aff52010-10-14 23:06:10 +000051 const LangOptions &Features;
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000052 MangleContext &MContext;
Dan Gohman3d5aff52010-10-14 23:06:10 +000053
Duncan Sands2d7cb062012-04-15 18:04:54 +000054 // MDHelper - Helper for creating metadata.
55 llvm::MDBuilder MDHelper;
56
Manman Renb37a73d2013-04-04 21:53:22 +000057 /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
58 /// them.
Dan Gohman3d5aff52010-10-14 23:06:10 +000059 llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
Manman Renb37a73d2013-04-04 21:53:22 +000060 /// This maps clang::Types to a struct node in the type DAG.
61 llvm::DenseMap<const Type *, llvm::MDNode *> StructTypeMetadataCache;
62 /// This maps TBAAPathTags to a tag node.
63 llvm::DenseMap<TBAAPathTag, llvm::MDNode *> StructTagMetadataCache;
Dan Gohman3d5aff52010-10-14 23:06:10 +000064
Dan Gohmanb22c7dc2012-09-28 21:58:29 +000065 /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
66 /// them for struct assignments.
67 llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
68
Dan Gohman3d5aff52010-10-14 23:06:10 +000069 llvm::MDNode *Root;
Dan Gohman3d5aff52010-10-14 23:06:10 +000070 llvm::MDNode *Char;
71
Dan Gohman224d7592010-10-25 21:48:30 +000072 /// getRoot - This is the mdnode for the root of the metadata type graph
73 /// for this translation unit.
74 llvm::MDNode *getRoot();
75
76 /// getChar - This is the mdnode for "char", which is special, and any types
77 /// considered to be equivalent to it.
78 llvm::MDNode *getChar();
79
Dan Gohmanb22c7dc2012-09-28 21:58:29 +000080 /// CollectFields - Collect information about the fields of a type for
81 /// !tbaa.struct metadata formation. Return false for an unsupported type.
82 bool CollectFields(uint64_t BaseOffset,
83 QualType Ty,
84 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
85 bool MayAlias);
86
Dan Gohman3d5aff52010-10-14 23:06:10 +000087public:
88 CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000089 const CodeGenOptions &CGO,
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000090 const LangOptions &Features,
91 MangleContext &MContext);
Dan Gohman3d5aff52010-10-14 23:06:10 +000092 ~CodeGenTBAA();
93
Dan Gohman565cc442010-10-21 18:49:12 +000094 /// getTBAAInfo - Get the TBAA MDNode to be used for a dereference
95 /// of the given type.
Dan Gohman3d5aff52010-10-14 23:06:10 +000096 llvm::MDNode *getTBAAInfo(QualType QTy);
Kostya Serebryany8cb4a072012-03-26 17:03:51 +000097
98 /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a
99 /// dereference of a vtable pointer.
100 llvm::MDNode *getTBAAInfoForVTablePtr();
Dan Gohmanb22c7dc2012-09-28 21:58:29 +0000101
102 /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
103 /// the given type.
104 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renb37a73d2013-04-04 21:53:22 +0000105
106 /// Get the MDNode in the type DAG for given struct type QType.
107 llvm::MDNode *getTBAAStructTypeInfo(QualType QType);
108 /// Get the tag MDNode for a given base type, the actual sclar access MDNode
109 /// and offset into the base type.
110 llvm::MDNode *getTBAAStructTagInfo(QualType BaseQType,
111 llvm::MDNode *AccessNode, uint64_t Offset);
Dan Gohman3d5aff52010-10-14 23:06:10 +0000112};
113
114} // end namespace CodeGen
115} // end namespace clang
116
Manman Renb37a73d2013-04-04 21:53:22 +0000117namespace llvm {
118
119template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> {
120 static clang::CodeGen::TBAAPathTag getEmptyKey() {
121 return clang::CodeGen::TBAAPathTag(
122 DenseMapInfo<const clang::Type *>::getEmptyKey(),
123 DenseMapInfo<const MDNode *>::getEmptyKey(),
124 DenseMapInfo<uint64_t>::getEmptyKey());
125 }
126
127 static clang::CodeGen::TBAAPathTag getTombstoneKey() {
128 return clang::CodeGen::TBAAPathTag(
129 DenseMapInfo<const clang::Type *>::getTombstoneKey(),
130 DenseMapInfo<const MDNode *>::getTombstoneKey(),
131 DenseMapInfo<uint64_t>::getTombstoneKey());
132 }
133
134 static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) {
135 return DenseMapInfo<const clang::Type *>::getHashValue(Val.BaseT) ^
136 DenseMapInfo<const MDNode *>::getHashValue(Val.AccessN) ^
137 DenseMapInfo<uint64_t>::getHashValue(Val.Offset);
138 }
139
140 static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS,
141 const clang::CodeGen::TBAAPathTag &RHS) {
142 return LHS.BaseT == RHS.BaseT &&
143 LHS.AccessN == RHS.AccessN &&
144 LHS.Offset == RHS.Offset;
145 }
146};
147
148} // end namespace llvm
149
Dan Gohman3d5aff52010-10-14 23:06:10 +0000150#endif