xref: /minix3/external/bsd/llvm/dist/clang/lib/CodeGen/CodeGenTBAA.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- CodeGenTBAA.h - TBAA information for LLVM CodeGen ------*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This is the code that manages TBAA information and defines the TBAA policy
11f4a2713aSLionel Sambuc // for the optimizer to use.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
16*0a6a1f1dSLionel Sambuc #define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc #include "clang/Basic/LLVM.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h"
20f4a2713aSLionel Sambuc #include "llvm/IR/MDBuilder.h"
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc namespace llvm {
23f4a2713aSLionel Sambuc   class LLVMContext;
24f4a2713aSLionel Sambuc   class MDNode;
25f4a2713aSLionel Sambuc }
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc namespace clang {
28f4a2713aSLionel Sambuc   class ASTContext;
29f4a2713aSLionel Sambuc   class CodeGenOptions;
30f4a2713aSLionel Sambuc   class LangOptions;
31f4a2713aSLionel Sambuc   class MangleContext;
32f4a2713aSLionel Sambuc   class QualType;
33f4a2713aSLionel Sambuc   class Type;
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc namespace CodeGen {
36f4a2713aSLionel Sambuc   class CGRecordLayout;
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc   struct TBAAPathTag {
TBAAPathTagTBAAPathTag39f4a2713aSLionel Sambuc     TBAAPathTag(const Type *B, const llvm::MDNode *A, uint64_t O)
40f4a2713aSLionel Sambuc       : BaseT(B), AccessN(A), Offset(O) {}
41f4a2713aSLionel Sambuc     const Type *BaseT;
42f4a2713aSLionel Sambuc     const llvm::MDNode *AccessN;
43f4a2713aSLionel Sambuc     uint64_t Offset;
44f4a2713aSLionel Sambuc   };
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc /// CodeGenTBAA - This class organizes the cross-module state that is used
47f4a2713aSLionel Sambuc /// while lowering AST types to LLVM types.
48f4a2713aSLionel Sambuc class CodeGenTBAA {
49f4a2713aSLionel Sambuc   ASTContext &Context;
50f4a2713aSLionel Sambuc   const CodeGenOptions &CodeGenOpts;
51f4a2713aSLionel Sambuc   const LangOptions &Features;
52f4a2713aSLionel Sambuc   MangleContext &MContext;
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   // MDHelper - Helper for creating metadata.
55f4a2713aSLionel Sambuc   llvm::MDBuilder MDHelper;
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc   /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
58f4a2713aSLionel Sambuc   /// them.
59f4a2713aSLionel Sambuc   llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
60f4a2713aSLionel Sambuc   /// This maps clang::Types to a struct node in the type DAG.
61f4a2713aSLionel Sambuc   llvm::DenseMap<const Type *, llvm::MDNode *> StructTypeMetadataCache;
62f4a2713aSLionel Sambuc   /// This maps TBAAPathTags to a tag node.
63f4a2713aSLionel Sambuc   llvm::DenseMap<TBAAPathTag, llvm::MDNode *> StructTagMetadataCache;
64f4a2713aSLionel Sambuc   /// This maps a scalar type to a scalar tag node.
65f4a2713aSLionel Sambuc   llvm::DenseMap<const llvm::MDNode *, llvm::MDNode *> ScalarTagMetadataCache;
66f4a2713aSLionel Sambuc 
67f4a2713aSLionel Sambuc   /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
68f4a2713aSLionel Sambuc   /// them for struct assignments.
69f4a2713aSLionel Sambuc   llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc   llvm::MDNode *Root;
72f4a2713aSLionel Sambuc   llvm::MDNode *Char;
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc   /// getRoot - This is the mdnode for the root of the metadata type graph
75f4a2713aSLionel Sambuc   /// for this translation unit.
76f4a2713aSLionel Sambuc   llvm::MDNode *getRoot();
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc   /// getChar - This is the mdnode for "char", which is special, and any types
79f4a2713aSLionel Sambuc   /// considered to be equivalent to it.
80f4a2713aSLionel Sambuc   llvm::MDNode *getChar();
81f4a2713aSLionel Sambuc 
82f4a2713aSLionel Sambuc   /// CollectFields - Collect information about the fields of a type for
83f4a2713aSLionel Sambuc   /// !tbaa.struct metadata formation. Return false for an unsupported type.
84f4a2713aSLionel Sambuc   bool CollectFields(uint64_t BaseOffset,
85f4a2713aSLionel Sambuc                      QualType Ty,
86f4a2713aSLionel Sambuc                      SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
87f4a2713aSLionel Sambuc                      bool MayAlias);
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc   /// A wrapper function to create a scalar type. For struct-path aware TBAA,
90f4a2713aSLionel Sambuc   /// the scalar type has the same format as the struct type: name, offset,
91f4a2713aSLionel Sambuc   /// pointer to another node in the type DAG.
92f4a2713aSLionel Sambuc   llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc public:
95f4a2713aSLionel Sambuc   CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
96f4a2713aSLionel Sambuc               const CodeGenOptions &CGO,
97f4a2713aSLionel Sambuc               const LangOptions &Features,
98f4a2713aSLionel Sambuc               MangleContext &MContext);
99f4a2713aSLionel Sambuc   ~CodeGenTBAA();
100f4a2713aSLionel Sambuc 
101f4a2713aSLionel Sambuc   /// getTBAAInfo - Get the TBAA MDNode to be used for a dereference
102f4a2713aSLionel Sambuc   /// of the given type.
103f4a2713aSLionel Sambuc   llvm::MDNode *getTBAAInfo(QualType QTy);
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc   /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a
106f4a2713aSLionel Sambuc   /// dereference of a vtable pointer.
107f4a2713aSLionel Sambuc   llvm::MDNode *getTBAAInfoForVTablePtr();
108f4a2713aSLionel Sambuc 
109f4a2713aSLionel Sambuc   /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
110f4a2713aSLionel Sambuc   /// the given type.
111f4a2713aSLionel Sambuc   llvm::MDNode *getTBAAStructInfo(QualType QTy);
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc   /// Get the MDNode in the type DAG for given struct type QType.
114f4a2713aSLionel Sambuc   llvm::MDNode *getTBAAStructTypeInfo(QualType QType);
115f4a2713aSLionel Sambuc   /// Get the tag MDNode for a given base type, the actual scalar access MDNode
116f4a2713aSLionel Sambuc   /// and offset into the base type.
117f4a2713aSLionel Sambuc   llvm::MDNode *getTBAAStructTagInfo(QualType BaseQType,
118f4a2713aSLionel Sambuc                                      llvm::MDNode *AccessNode, uint64_t Offset);
119f4a2713aSLionel Sambuc 
120f4a2713aSLionel Sambuc   /// Get the scalar tag MDNode for a given scalar type.
121f4a2713aSLionel Sambuc   llvm::MDNode *getTBAAScalarTagInfo(llvm::MDNode *AccessNode);
122f4a2713aSLionel Sambuc };
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc }  // end namespace CodeGen
125f4a2713aSLionel Sambuc }  // end namespace clang
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc namespace llvm {
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> {
130f4a2713aSLionel Sambuc   static clang::CodeGen::TBAAPathTag getEmptyKey() {
131f4a2713aSLionel Sambuc     return clang::CodeGen::TBAAPathTag(
132f4a2713aSLionel Sambuc       DenseMapInfo<const clang::Type *>::getEmptyKey(),
133f4a2713aSLionel Sambuc       DenseMapInfo<const MDNode *>::getEmptyKey(),
134f4a2713aSLionel Sambuc       DenseMapInfo<uint64_t>::getEmptyKey());
135f4a2713aSLionel Sambuc   }
136f4a2713aSLionel Sambuc 
137f4a2713aSLionel Sambuc   static clang::CodeGen::TBAAPathTag getTombstoneKey() {
138f4a2713aSLionel Sambuc     return clang::CodeGen::TBAAPathTag(
139f4a2713aSLionel Sambuc       DenseMapInfo<const clang::Type *>::getTombstoneKey(),
140f4a2713aSLionel Sambuc       DenseMapInfo<const MDNode *>::getTombstoneKey(),
141f4a2713aSLionel Sambuc       DenseMapInfo<uint64_t>::getTombstoneKey());
142f4a2713aSLionel Sambuc   }
143f4a2713aSLionel Sambuc 
144f4a2713aSLionel Sambuc   static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) {
145f4a2713aSLionel Sambuc     return DenseMapInfo<const clang::Type *>::getHashValue(Val.BaseT) ^
146f4a2713aSLionel Sambuc            DenseMapInfo<const MDNode *>::getHashValue(Val.AccessN) ^
147f4a2713aSLionel Sambuc            DenseMapInfo<uint64_t>::getHashValue(Val.Offset);
148f4a2713aSLionel Sambuc   }
149f4a2713aSLionel Sambuc 
150f4a2713aSLionel Sambuc   static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS,
151f4a2713aSLionel Sambuc                       const clang::CodeGen::TBAAPathTag &RHS) {
152f4a2713aSLionel Sambuc     return LHS.BaseT == RHS.BaseT &&
153f4a2713aSLionel Sambuc            LHS.AccessN == RHS.AccessN &&
154f4a2713aSLionel Sambuc            LHS.Offset == RHS.Offset;
155f4a2713aSLionel Sambuc   }
156f4a2713aSLionel Sambuc };
157f4a2713aSLionel Sambuc 
158f4a2713aSLionel Sambuc }  // end namespace llvm
159f4a2713aSLionel Sambuc 
160f4a2713aSLionel Sambuc #endif
161