clang  3.9.0
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  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 /// CodeGenTBAA - This class organizes the cross-module state that is used
44 /// while lowering AST types to LLVM types.
45 class CodeGenTBAA {
46  ASTContext &Context;
47  const CodeGenOptions &CodeGenOpts;
48  const LangOptions &Features;
49  MangleContext &MContext;
50 
51  // MDHelper - Helper for creating metadata.
52  llvm::MDBuilder MDHelper;
53 
54  /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
55  /// them.
56  llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
57  /// This maps clang::Types to a struct node in the type DAG.
58  llvm::DenseMap<const Type *, llvm::MDNode *> StructTypeMetadataCache;
59  /// This maps TBAAPathTags to a tag node.
60  llvm::DenseMap<TBAAPathTag, llvm::MDNode *> StructTagMetadataCache;
61  /// This maps a scalar type to a scalar tag node.
62  llvm::DenseMap<const llvm::MDNode *, llvm::MDNode *> ScalarTagMetadataCache;
63 
64  /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
65  /// them for struct assignments.
66  llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
67 
68  llvm::MDNode *Root;
69  llvm::MDNode *Char;
70 
71  /// getRoot - This is the mdnode for the root of the metadata type graph
72  /// for this translation unit.
73  llvm::MDNode *getRoot();
74 
75  /// getChar - This is the mdnode for "char", which is special, and any types
76  /// considered to be equivalent to it.
77  llvm::MDNode *getChar();
78 
79  /// CollectFields - Collect information about the fields of a type for
80  /// !tbaa.struct metadata formation. Return false for an unsupported type.
81  bool CollectFields(uint64_t BaseOffset,
82  QualType Ty,
84  bool MayAlias);
85 
86  /// A wrapper function to create a scalar type. For struct-path aware TBAA,
87  /// the scalar type has the same format as the struct type: name, offset,
88  /// pointer to another node in the type DAG.
89  llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
90 
91 public:
92  CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
93  const CodeGenOptions &CGO,
94  const LangOptions &Features,
95  MangleContext &MContext);
96  ~CodeGenTBAA();
97 
98  /// getTBAAInfo - Get the TBAA MDNode to be used for a dereference
99  /// of the given type.
100  llvm::MDNode *getTBAAInfo(QualType QTy);
101 
102  /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a
103  /// dereference of a vtable pointer.
104  llvm::MDNode *getTBAAInfoForVTablePtr();
105 
106  /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
107  /// the given type.
108  llvm::MDNode *getTBAAStructInfo(QualType QTy);
109 
110  /// Get the MDNode in the type DAG for given struct type QType.
111  llvm::MDNode *getTBAAStructTypeInfo(QualType QType);
112  /// Get the tag MDNode for a given base type, the actual scalar access MDNode
113  /// and offset into the base type.
114  llvm::MDNode *getTBAAStructTagInfo(QualType BaseQType,
115  llvm::MDNode *AccessNode, uint64_t Offset);
116 
117  /// Get the scalar tag MDNode for a given scalar type.
118  llvm::MDNode *getTBAAScalarTagInfo(llvm::MDNode *AccessNode);
119 };
120 
121 } // end namespace CodeGen
122 } // end namespace clang
123 
124 namespace llvm {
125 
126 template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> {
132  }
133 
139  }
140 
141  static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) {
145  }
146 
147  static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS,
148  const clang::CodeGen::TBAAPathTag &RHS) {
149  return LHS.BaseT == RHS.BaseT &&
150  LHS.AccessN == RHS.AccessN &&
151  LHS.Offset == RHS.Offset;
152  }
153 };
154 
155 } // end namespace llvm
156 
157 #endif
A (possibly-)qualified type.
Definition: Type.h:598
C Language Family Type Representation.
The base class of the type hierarchy.
Definition: Type.h:1281
CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext, const CodeGenOptions &CGO, const LangOptions &Features, MangleContext &MContext)
Definition: CodeGenTBAA.cpp:32
const llvm::MDNode * AccessN
Definition: CodeGenTBAA.h:39
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4549
static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val)
Definition: CodeGenTBAA.h:141
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:92
llvm::MDNode * getTBAAInfoForVTablePtr()
getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a dereference of a vtable pointer...
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:44
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
llvm::MDNode * getTBAAStructTypeInfo(QualType QType)
Get the MDNode in the type DAG for given struct type QType.
friend class ASTContext
Definition: Type.h:4178
llvm::MDNode * getTBAAScalarTagInfo(llvm::MDNode *AccessNode)
Get the scalar tag MDNode for a given scalar type.
The l-value was considered opaque, so the alignment was determined from a type.
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:42
TBAAPathTag(const Type *B, const llvm::MDNode *A, uint64_t O)
Definition: CodeGenTBAA.h:36
static clang::CodeGen::TBAAPathTag getEmptyKey()
Definition: CodeGenTBAA.h:127
llvm::MDNode * getTBAAStructInfo(QualType QTy)
getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of the given type...
llvm::MDNode * getTBAAStructTagInfo(QualType BaseQType, llvm::MDNode *AccessNode, uint64_t Offset)
Get the tag MDNode for a given base type, the actual scalar access MDNode and offset into the base ty...
llvm::MDNode * getTBAAInfo(QualType QTy)
getTBAAInfo - Get the TBAA MDNode to be used for a dereference of the given type. ...
Definition: CodeGenTBAA.cpp:92
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
static clang::CodeGen::TBAAPathTag getTombstoneKey()
Definition: CodeGenTBAA.h:134
CodeGenTBAA - This class organizes the cross-module state that is used while lowering AST types to LL...
Definition: CodeGenTBAA.h:45
static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS, const clang::CodeGen::TBAAPathTag &RHS)
Definition: CodeGenTBAA.h:147