1 //===--- 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 //
10 // This is the code that manages TBAA information and defines the TBAA policy
11 // for the optimizer to use.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
16 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
17 
18 #include "clang/AST/Type.h"
19 #include "clang/Basic/LLVM.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/IR/Metadata.h"
23 
24 namespace clang {
25   class ASTContext;
26   class CodeGenOptions;
27   class LangOptions;
28   class MangleContext;
29   class QualType;
30   class Type;
31 
32 namespace CodeGen {
33 class CGRecordLayout;
34 
35 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 
43 // TBAAAccessInfo - Describes a memory access in terms of TBAA.
44 struct TBAAAccessInfo {
45   TBAAAccessInfo(QualType BaseType, llvm::MDNode *AccessType, uint64_t Offset)
46     : BaseType(BaseType), AccessType(AccessType), Offset(Offset)
47   {}
48 
49   explicit TBAAAccessInfo(llvm::MDNode *AccessType)
50     : TBAAAccessInfo(/* BaseType= */ QualType(), AccessType, /* Offset= */ 0)
51   {}
52 
53   TBAAAccessInfo()
54     : TBAAAccessInfo(/* AccessType= */ nullptr)
55   {}
56 
57   /// BaseType - The base/leading access type. May be null if this access
58   /// descriptor represents an access that is not considered to be an access
59   /// to an aggregate or union member.
60   QualType BaseType;
61 
62   /// AccessType - The final access type. May be null if there is no TBAA
63   /// information available about this access.
64   llvm::MDNode *AccessType;
65 
66   /// Offset - The byte offset of the final access within the base one. Must be
67   /// zero if the base access type is not specified.
68   uint64_t Offset;
69 };
70 
71 /// CodeGenTBAA - This class organizes the cross-module state that is used
72 /// while lowering AST types to LLVM types.
73 class CodeGenTBAA {
74   ASTContext &Context;
75   const CodeGenOptions &CodeGenOpts;
76   const LangOptions &Features;
77   MangleContext &MContext;
78 
79   // MDHelper - Helper for creating metadata.
80   llvm::MDBuilder MDHelper;
81 
82   /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
83   /// them.
84   llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
85   /// This maps clang::Types to a struct node in the type DAG.
86   llvm::DenseMap<const Type *, llvm::MDNode *> StructTypeMetadataCache;
87   /// This maps TBAAPathTags to a tag node.
88   llvm::DenseMap<TBAAPathTag, llvm::MDNode *> StructTagMetadataCache;
89   /// This maps a scalar type to a scalar tag node.
90   llvm::DenseMap<const llvm::MDNode *, llvm::MDNode *> ScalarTagMetadataCache;
91 
92   /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
93   /// them for struct assignments.
94   llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
95 
96   llvm::MDNode *Root;
97   llvm::MDNode *Char;
98 
99   /// getRoot - This is the mdnode for the root of the metadata type graph
100   /// for this translation unit.
101   llvm::MDNode *getRoot();
102 
103   /// getChar - This is the mdnode for "char", which is special, and any types
104   /// considered to be equivalent to it.
105   llvm::MDNode *getChar();
106 
107   /// CollectFields - Collect information about the fields of a type for
108   /// !tbaa.struct metadata formation. Return false for an unsupported type.
109   bool CollectFields(uint64_t BaseOffset,
110                      QualType Ty,
111                      SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
112                      bool MayAlias);
113 
114   /// A wrapper function to create a scalar type. For struct-path aware TBAA,
115   /// the scalar type has the same format as the struct type: name, offset,
116   /// pointer to another node in the type DAG.
117   llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
118 
119 public:
120   CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
121               const CodeGenOptions &CGO,
122               const LangOptions &Features,
123               MangleContext &MContext);
124   ~CodeGenTBAA();
125 
126   /// getTypeInfo - Get metadata used to describe accesses to objects of the
127   /// given type.
128   llvm::MDNode *getTypeInfo(QualType QTy);
129 
130   /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a
131   /// dereference of a vtable pointer.
132   llvm::MDNode *getTBAAInfoForVTablePtr();
133 
134   /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
135   /// the given type.
136   llvm::MDNode *getTBAAStructInfo(QualType QTy);
137 
138   /// Get the MDNode in the type DAG for given struct type QType.
139   llvm::MDNode *getTBAAStructTypeInfo(QualType QType);
140 
141   /// Get path-aware TBAA tag for a given memory access.
142   llvm::MDNode *getTBAAStructTagInfo(TBAAAccessInfo Info);
143 
144   /// Get the scalar tag MDNode for a given scalar type.
145   llvm::MDNode *getTBAAScalarTagInfo(llvm::MDNode *AccessNode);
146 
147   /// getMayAliasTypeInfo - Get TBAA information that represents may-alias
148   /// accesses.
149   llvm::MDNode *getMayAliasTypeInfo();
150 };
151 
152 }  // end namespace CodeGen
153 }  // end namespace clang
154 
155 namespace llvm {
156 
157 template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> {
158   static clang::CodeGen::TBAAPathTag getEmptyKey() {
159     return clang::CodeGen::TBAAPathTag(
160       DenseMapInfo<const clang::Type *>::getEmptyKey(),
161       DenseMapInfo<const MDNode *>::getEmptyKey(),
162       DenseMapInfo<uint64_t>::getEmptyKey());
163   }
164 
165   static clang::CodeGen::TBAAPathTag getTombstoneKey() {
166     return clang::CodeGen::TBAAPathTag(
167       DenseMapInfo<const clang::Type *>::getTombstoneKey(),
168       DenseMapInfo<const MDNode *>::getTombstoneKey(),
169       DenseMapInfo<uint64_t>::getTombstoneKey());
170   }
171 
172   static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) {
173     return DenseMapInfo<const clang::Type *>::getHashValue(Val.BaseT) ^
174            DenseMapInfo<const MDNode *>::getHashValue(Val.AccessN) ^
175            DenseMapInfo<uint64_t>::getHashValue(Val.Offset);
176   }
177 
178   static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS,
179                       const clang::CodeGen::TBAAPathTag &RHS) {
180     return LHS.BaseT == RHS.BaseT &&
181            LHS.AccessN == RHS.AccessN &&
182            LHS.Offset == RHS.Offset;
183   }
184 };
185 
186 }  // end namespace llvm
187 
188 #endif
189