clang  6.0.0svn
CodeGenTBAA.h
Go to the documentation of this file.
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 // TBAAAccessInfo - Describes a memory access in terms of TBAA.
37  TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType,
38  uint64_t Offset)
39  : BaseType(BaseType), AccessType(AccessType), Offset(Offset)
40  {}
41 
42  explicit TBAAAccessInfo(llvm::MDNode *AccessType)
43  : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType, /* Offset= */ 0)
44  {}
45 
47  : TBAAAccessInfo(/* AccessType= */ nullptr)
48  {}
49 
50  bool operator==(const TBAAAccessInfo &Other) const {
51  return BaseType == Other.BaseType &&
52  AccessType == Other.AccessType &&
53  Offset == Other.Offset;
54  }
55 
56  /// BaseType - The base/leading access type. May be null if this access
57  /// descriptor represents an access that is not considered to be an access
58  /// to an aggregate or union member.
59  llvm::MDNode *BaseType;
60 
61  /// AccessType - The final access type. May be null if there is no TBAA
62  /// information available about this access.
63  llvm::MDNode *AccessType;
64 
65  /// Offset - The byte offset of the final access within the base one. Must be
66  /// zero if the base access type is not specified.
67  uint64_t Offset;
68 };
69 
70 /// CodeGenTBAA - This class organizes the cross-module state that is used
71 /// while lowering AST types to LLVM types.
72 class CodeGenTBAA {
73  ASTContext &Context;
74  const CodeGenOptions &CodeGenOpts;
75  const LangOptions &Features;
76  MangleContext &MContext;
77 
78  // MDHelper - Helper for creating metadata.
79  llvm::MDBuilder MDHelper;
80 
81  /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
82  /// them.
83  llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
84  /// This maps clang::Types to a base access type in the type DAG.
85  llvm::DenseMap<const Type *, llvm::MDNode *> BaseTypeMetadataCache;
86  /// This maps TBAA access descriptors to tag nodes.
87  llvm::DenseMap<TBAAAccessInfo, llvm::MDNode *> AccessTagMetadataCache;
88 
89  /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
90  /// them for struct assignments.
91  llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
92 
93  llvm::MDNode *Root;
94  llvm::MDNode *Char;
95 
96  /// getRoot - This is the mdnode for the root of the metadata type graph
97  /// for this translation unit.
98  llvm::MDNode *getRoot();
99 
100  /// getChar - This is the mdnode for "char", which is special, and any types
101  /// considered to be equivalent to it.
102  llvm::MDNode *getChar();
103 
104  /// CollectFields - Collect information about the fields of a type for
105  /// !tbaa.struct metadata formation. Return false for an unsupported type.
106  bool CollectFields(uint64_t BaseOffset,
107  QualType Ty,
109  bool MayAlias);
110 
111  /// A wrapper function to create a scalar type. For struct-path aware TBAA,
112  /// the scalar type has the same format as the struct type: name, offset,
113  /// pointer to another node in the type DAG.
114  llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
115 
116 public:
117  CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
118  const CodeGenOptions &CGO,
119  const LangOptions &Features,
120  MangleContext &MContext);
121  ~CodeGenTBAA();
122 
123  /// getTypeInfo - Get metadata used to describe accesses to objects of the
124  /// given type.
125  llvm::MDNode *getTypeInfo(QualType QTy);
126 
127  /// getVTablePtrAccessInfo - Get the TBAA information that describes an
128  /// access to a virtual table pointer.
129  TBAAAccessInfo getVTablePtrAccessInfo();
130 
131  /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
132  /// the given type.
133  llvm::MDNode *getTBAAStructInfo(QualType QTy);
134 
135  /// getBaseTypeInfo - Get metadata that describes the given base access type.
136  /// Return null if the type is not suitable for use in TBAA access tags.
137  llvm::MDNode *getBaseTypeInfo(QualType QTy);
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  /// mergeTBAAInfoForCast - Get merged TBAA information for the purpose of
147  /// type casts.
148  TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
150 };
151 
152 } // end namespace CodeGen
153 } // end namespace clang
154 
155 namespace llvm {
156 
157 template<> struct DenseMapInfo<clang::CodeGen::TBAAAccessInfo> {
163  }
164 
170  }
171 
172  static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) {
176  }
177 
178  static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS,
179  const clang::CodeGen::TBAAAccessInfo &RHS) {
180  return LHS.BaseType == RHS.BaseType &&
181  LHS.AccessType == RHS.AccessType &&
182  LHS.Offset == RHS.Offset;
183  }
184 };
185 
186 } // end namespace llvm
187 
188 #endif
TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType, uint64_t Offset)
Definition: CodeGenTBAA.h:37
A (possibly-)qualified type.
Definition: Type.h:614
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
TBAAAccessInfo(llvm::MDNode *AccessType)
Definition: CodeGenTBAA.h:42
C Language Family Type Representation.
llvm::MDNode * AccessType
AccessType - The final access type.
Definition: CodeGenTBAA.h:63
static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS, const clang::CodeGen::TBAAAccessInfo &RHS)
Definition: CodeGenTBAA.h:178
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
uint32_t Offset
Definition: CacheTokens.cpp:43
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
static DeltaTreeNode * getRoot(void *Root)
Definition: DeltaTree.cpp:383
NodeId Parent
Definition: ASTDiff.cpp:192
static clang::CodeGen::TBAAAccessInfo getTombstoneKey()
Definition: CodeGenTBAA.h:165
Exposes information about the current target.
Definition: TargetInfo.h:55
The l-value was considered opaque, so the alignment was determined from a type.
bool operator==(const TBAAAccessInfo &Other) const
Definition: CodeGenTBAA.h:50
llvm::MDNode * BaseType
BaseType - The base/leading access type.
Definition: CodeGenTBAA.h:59
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:42
Dataflow Directional Tag Classes.
static clang::CodeGen::TBAAAccessInfo getEmptyKey()
Definition: CodeGenTBAA.h:158
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
CodeGenTBAA - This class organizes the cross-module state that is used while lowering AST types to LL...
Definition: CodeGenTBAA.h:72
static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val)
Definition: CodeGenTBAA.h:172
uint64_t Offset
Offset - The byte offset of the final access within the base one.
Definition: CodeGenTBAA.h:67