clang  3.7.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/Basic/LLVM.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/IR/MDBuilder.h"
21 
22 namespace llvm {
23  class LLVMContext;
24  class MDNode;
25 }
26 
27 namespace clang {
28  class ASTContext;
29  class CodeGenOptions;
30  class LangOptions;
31  class MangleContext;
32  class QualType;
33  class Type;
34 
35 namespace CodeGen {
36  class CGRecordLayout;
37 
38  struct TBAAPathTag {
39  TBAAPathTag(const Type *B, const llvm::MDNode *A, uint64_t O)
40  : BaseT(B), AccessN(A), Offset(O) {}
41  const Type *BaseT;
42  const llvm::MDNode *AccessN;
43  uint64_t Offset;
44  };
45 
46 /// CodeGenTBAA - This class organizes the cross-module state that is used
47 /// while lowering AST types to LLVM types.
48 class CodeGenTBAA {
49  ASTContext &Context;
50  const CodeGenOptions &CodeGenOpts;
51  const LangOptions &Features;
52  MangleContext &MContext;
53 
54  // MDHelper - Helper for creating metadata.
55  llvm::MDBuilder MDHelper;
56 
57  /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
58  /// them.
59  llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
60  /// This maps clang::Types to a struct node in the type DAG.
61  llvm::DenseMap<const Type *, llvm::MDNode *> StructTypeMetadataCache;
62  /// This maps TBAAPathTags to a tag node.
63  llvm::DenseMap<TBAAPathTag, llvm::MDNode *> StructTagMetadataCache;
64  /// This maps a scalar type to a scalar tag node.
65  llvm::DenseMap<const llvm::MDNode *, llvm::MDNode *> ScalarTagMetadataCache;
66 
67  /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
68  /// them for struct assignments.
69  llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
70 
71  llvm::MDNode *Root;
72  llvm::MDNode *Char;
73 
74  /// getRoot - This is the mdnode for the root of the metadata type graph
75  /// for this translation unit.
76  llvm::MDNode *getRoot();
77 
78  /// getChar - This is the mdnode for "char", which is special, and any types
79  /// considered to be equivalent to it.
80  llvm::MDNode *getChar();
81 
82  /// CollectFields - Collect information about the fields of a type for
83  /// !tbaa.struct metadata formation. Return false for an unsupported type.
84  bool CollectFields(uint64_t BaseOffset,
85  QualType Ty,
87  bool MayAlias);
88 
89  /// A wrapper function to create a scalar type. For struct-path aware TBAA,
90  /// the scalar type has the same format as the struct type: name, offset,
91  /// pointer to another node in the type DAG.
92  llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
93 
94 public:
95  CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
96  const CodeGenOptions &CGO,
97  const LangOptions &Features,
98  MangleContext &MContext);
99  ~CodeGenTBAA();
100 
101  /// getTBAAInfo - Get the TBAA MDNode to be used for a dereference
102  /// of the given type.
103  llvm::MDNode *getTBAAInfo(QualType QTy);
104 
105  /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a
106  /// dereference of a vtable pointer.
107  llvm::MDNode *getTBAAInfoForVTablePtr();
108 
109  /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
110  /// the given type.
111  llvm::MDNode *getTBAAStructInfo(QualType QTy);
112 
113  /// Get the MDNode in the type DAG for given struct type QType.
114  llvm::MDNode *getTBAAStructTypeInfo(QualType QType);
115  /// Get the tag MDNode for a given base type, the actual scalar access MDNode
116  /// and offset into the base type.
117  llvm::MDNode *getTBAAStructTagInfo(QualType BaseQType,
118  llvm::MDNode *AccessNode, uint64_t Offset);
119 
120  /// Get the scalar tag MDNode for a given scalar type.
121  llvm::MDNode *getTBAAScalarTagInfo(llvm::MDNode *AccessNode);
122 };
123 
124 } // end namespace CodeGen
125 } // end namespace clang
126 
127 namespace llvm {
128 
129 template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> {
135  }
136 
142  }
143 
144  static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) {
148  }
149 
150  static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS,
151  const clang::CodeGen::TBAAPathTag &RHS) {
152  return LHS.BaseT == RHS.BaseT &&
153  LHS.AccessN == RHS.AccessN &&
154  LHS.Offset == RHS.Offset;
155  }
156 };
157 
158 } // end namespace llvm
159 
160 #endif
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:42
static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val)
Definition: CodeGenTBAA.h:144
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:89
llvm::MDNode * getTBAAInfoForVTablePtr()
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...
llvm::MDNode * getTBAAStructTypeInfo(QualType QType)
Get the MDNode in the type DAG for given struct type QType.
llvm::MDNode * getTBAAScalarTagInfo(llvm::MDNode *AccessNode)
Get the scalar tag MDNode for a given scalar type.
TBAAPathTag(const Type *B, const llvm::MDNode *A, uint64_t O)
Definition: CodeGenTBAA.h:39
static clang::CodeGen::TBAAPathTag getEmptyKey()
Definition: CodeGenTBAA.h:130
llvm::MDNode * getTBAAStructInfo(QualType QTy)
llvm::MDNode * getTBAAStructTagInfo(QualType BaseQType, llvm::MDNode *AccessNode, uint64_t Offset)
Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA.
llvm::MDNode * getTBAAInfo(QualType QTy)
Definition: CodeGenTBAA.cpp:88
static clang::CodeGen::TBAAPathTag getTombstoneKey()
Definition: CodeGenTBAA.h:137
static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS, const clang::CodeGen::TBAAPathTag &RHS)
Definition: CodeGenTBAA.h:150