CodeGenTBAA.h revision b37a73d5c6a0c8bb1f6e363d3b53980e4fa0cead
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 CLANG_CODEGEN_CODEGENTBAA_H
16#define CLANG_CODEGEN_CODEGENTBAA_H
17
18#include "clang/Basic/LLVM.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/IR/MDBuilder.h"
21
22namespace llvm {
23  class LLVMContext;
24  class MDNode;
25}
26
27namespace clang {
28  class ASTContext;
29  class CodeGenOptions;
30  class LangOptions;
31  class MangleContext;
32  class QualType;
33  class Type;
34
35namespace 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.
48class 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
65  /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
66  /// them for struct assignments.
67  llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
68
69  llvm::MDNode *Root;
70  llvm::MDNode *Char;
71
72  /// getRoot - This is the mdnode for the root of the metadata type graph
73  /// for this translation unit.
74  llvm::MDNode *getRoot();
75
76  /// getChar - This is the mdnode for "char", which is special, and any types
77  /// considered to be equivalent to it.
78  llvm::MDNode *getChar();
79
80  /// CollectFields - Collect information about the fields of a type for
81  /// !tbaa.struct metadata formation. Return false for an unsupported type.
82  bool CollectFields(uint64_t BaseOffset,
83                     QualType Ty,
84                     SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
85                     bool MayAlias);
86
87public:
88  CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
89              const CodeGenOptions &CGO,
90              const LangOptions &Features,
91              MangleContext &MContext);
92  ~CodeGenTBAA();
93
94  /// getTBAAInfo - Get the TBAA MDNode to be used for a dereference
95  /// of the given type.
96  llvm::MDNode *getTBAAInfo(QualType QTy);
97
98  /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a
99  /// dereference of a vtable pointer.
100  llvm::MDNode *getTBAAInfoForVTablePtr();
101
102  /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
103  /// the given type.
104  llvm::MDNode *getTBAAStructInfo(QualType QTy);
105
106  /// Get the MDNode in the type DAG for given struct type QType.
107  llvm::MDNode *getTBAAStructTypeInfo(QualType QType);
108  /// Get the tag MDNode for a given base type, the actual sclar access MDNode
109  /// and offset into the base type.
110  llvm::MDNode *getTBAAStructTagInfo(QualType BaseQType,
111                                     llvm::MDNode *AccessNode, uint64_t Offset);
112};
113
114}  // end namespace CodeGen
115}  // end namespace clang
116
117namespace llvm {
118
119template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> {
120  static clang::CodeGen::TBAAPathTag getEmptyKey() {
121    return clang::CodeGen::TBAAPathTag(
122      DenseMapInfo<const clang::Type *>::getEmptyKey(),
123      DenseMapInfo<const MDNode *>::getEmptyKey(),
124      DenseMapInfo<uint64_t>::getEmptyKey());
125  }
126
127  static clang::CodeGen::TBAAPathTag getTombstoneKey() {
128    return clang::CodeGen::TBAAPathTag(
129      DenseMapInfo<const clang::Type *>::getTombstoneKey(),
130      DenseMapInfo<const MDNode *>::getTombstoneKey(),
131      DenseMapInfo<uint64_t>::getTombstoneKey());
132  }
133
134  static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) {
135    return DenseMapInfo<const clang::Type *>::getHashValue(Val.BaseT) ^
136           DenseMapInfo<const MDNode *>::getHashValue(Val.AccessN) ^
137           DenseMapInfo<uint64_t>::getHashValue(Val.Offset);
138  }
139
140  static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS,
141                      const clang::CodeGen::TBAAPathTag &RHS) {
142    return LHS.BaseT == RHS.BaseT &&
143           LHS.AccessN == RHS.AccessN &&
144           LHS.Offset == RHS.Offset;
145  }
146};
147
148}  // end namespace llvm
149
150#endif
151