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