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 
90   /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
91   /// them for struct assignments.
92   llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
93 
94   llvm::MDNode *Root;
95   llvm::MDNode *Char;
96 
97   /// getRoot - This is the mdnode for the root of the metadata type graph
98   /// for this translation unit.
99   llvm::MDNode *getRoot();
100 
101   /// getChar - This is the mdnode for "char", which is special, and any types
102   /// considered to be equivalent to it.
103   llvm::MDNode *getChar();
104 
105   /// CollectFields - Collect information about the fields of a type for
106   /// !tbaa.struct metadata formation. Return false for an unsupported type.
107   bool CollectFields(uint64_t BaseOffset,
108                      QualType Ty,
109                      SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
110                      bool MayAlias);
111 
112   /// A wrapper function to create a scalar type. For struct-path aware TBAA,
113   /// the scalar type has the same format as the struct type: name, offset,
114   /// pointer to another node in the type DAG.
115   llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
116 
117 public:
118   CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
119               const CodeGenOptions &CGO,
120               const LangOptions &Features,
121               MangleContext &MContext);
122   ~CodeGenTBAA();
123 
124   /// getTypeInfo - Get metadata used to describe accesses to objects of the
125   /// given type.
126   llvm::MDNode *getTypeInfo(QualType QTy);
127 
128   /// getVTablePtrAccessInfo - Get the TBAA information that describes an
129   /// access to a virtual table pointer.
130   TBAAAccessInfo getVTablePtrAccessInfo();
131 
132   /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
133   /// the given type.
134   llvm::MDNode *getTBAAStructInfo(QualType QTy);
135 
136   /// getBaseTypeInfo - Get metadata node for a given base access type.
137   llvm::MDNode *getBaseTypeInfo(QualType QType);
138 
139   /// getAccessTagInfo - Get TBAA tag for a given memory access.
140   llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info);
141 
142   /// getMayAliasAccessInfo - Get TBAA information that represents may-alias
143   /// accesses.
144   TBAAAccessInfo getMayAliasAccessInfo();
145 };
146 
147 }  // end namespace CodeGen
148 }  // end namespace clang
149 
150 namespace llvm {
151 
152 template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> {
153   static clang::CodeGen::TBAAPathTag getEmptyKey() {
154     return clang::CodeGen::TBAAPathTag(
155       DenseMapInfo<const clang::Type *>::getEmptyKey(),
156       DenseMapInfo<const MDNode *>::getEmptyKey(),
157       DenseMapInfo<uint64_t>::getEmptyKey());
158   }
159 
160   static clang::CodeGen::TBAAPathTag getTombstoneKey() {
161     return clang::CodeGen::TBAAPathTag(
162       DenseMapInfo<const clang::Type *>::getTombstoneKey(),
163       DenseMapInfo<const MDNode *>::getTombstoneKey(),
164       DenseMapInfo<uint64_t>::getTombstoneKey());
165   }
166 
167   static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) {
168     return DenseMapInfo<const clang::Type *>::getHashValue(Val.BaseT) ^
169            DenseMapInfo<const MDNode *>::getHashValue(Val.AccessN) ^
170            DenseMapInfo<uint64_t>::getHashValue(Val.Offset);
171   }
172 
173   static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS,
174                       const clang::CodeGen::TBAAPathTag &RHS) {
175     return LHS.BaseT == RHS.BaseT &&
176            LHS.AccessN == RHS.AccessN &&
177            LHS.Offset == RHS.Offset;
178   }
179 };
180 
181 }  // end namespace llvm
182 
183 #endif
184