Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 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 | // |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 10 | // This is the code that manages TBAA information and defines the TBAA policy |
| 11 | // for the optimizer to use. |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef CLANG_CODEGEN_CODEGENTBAA_H |
| 16 | #define CLANG_CODEGEN_CODEGENTBAA_H |
| 17 | |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LLVM.h" |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 20 | #include "llvm/IR/MDBuilder.h" |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
| 23 | class LLVMContext; |
| 24 | class MDNode; |
| 25 | } |
| 26 | |
| 27 | namespace clang { |
| 28 | class ASTContext; |
Kostya Serebryany | c9fe605 | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 29 | class CodeGenOptions; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 30 | class LangOptions; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 31 | class MangleContext; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 32 | class QualType; |
| 33 | class Type; |
| 34 | |
| 35 | namespace CodeGen { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 36 | class CGRecordLayout; |
| 37 | |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 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 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 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; |
Kostya Serebryany | c9fe605 | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 50 | const CodeGenOptions &CodeGenOpts; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 51 | const LangOptions &Features; |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 52 | MangleContext &MContext; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 53 | |
Duncan Sands | 2d7cb06 | 2012-04-15 18:04:54 +0000 | [diff] [blame] | 54 | // MDHelper - Helper for creating metadata. |
| 55 | llvm::MDBuilder MDHelper; |
| 56 | |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 57 | /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing |
| 58 | /// them. |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 59 | llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache; |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 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; |
Manman Ren | ca83518 | 2013-04-11 23:02:56 +0000 | [diff] [blame^] | 64 | /// This maps a scalar type to a scalar tag node. |
| 65 | llvm::DenseMap<const llvm::MDNode *, llvm::MDNode *> ScalarTagMetadataCache; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 66 | |
Dan Gohman | b22c7dc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 67 | /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing |
| 68 | /// them for struct assignments. |
| 69 | llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache; |
| 70 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 71 | llvm::MDNode *Root; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 72 | llvm::MDNode *Char; |
| 73 | |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 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 | |
Dan Gohman | b22c7dc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 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, |
| 86 | SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields, |
| 87 | bool MayAlias); |
| 88 | |
Manman Ren | ca83518 | 2013-04-11 23:02:56 +0000 | [diff] [blame^] | 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 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 94 | public: |
| 95 | CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext, |
Kostya Serebryany | c9fe605 | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 96 | const CodeGenOptions &CGO, |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 97 | const LangOptions &Features, |
| 98 | MangleContext &MContext); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 99 | ~CodeGenTBAA(); |
| 100 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 101 | /// getTBAAInfo - Get the TBAA MDNode to be used for a dereference |
| 102 | /// of the given type. |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 103 | llvm::MDNode *getTBAAInfo(QualType QTy); |
Kostya Serebryany | 8cb4a07 | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 104 | |
| 105 | /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a |
| 106 | /// dereference of a vtable pointer. |
| 107 | llvm::MDNode *getTBAAInfoForVTablePtr(); |
Dan Gohman | b22c7dc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 108 | |
| 109 | /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of |
| 110 | /// the given type. |
| 111 | llvm::MDNode *getTBAAStructInfo(QualType QTy); |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 112 | |
| 113 | /// Get the MDNode in the type DAG for given struct type QType. |
| 114 | llvm::MDNode *getTBAAStructTypeInfo(QualType QType); |
Manman Ren | ca83518 | 2013-04-11 23:02:56 +0000 | [diff] [blame^] | 115 | /// Get the tag MDNode for a given base type, the actual scalar access MDNode |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 116 | /// and offset into the base type. |
| 117 | llvm::MDNode *getTBAAStructTagInfo(QualType BaseQType, |
| 118 | llvm::MDNode *AccessNode, uint64_t Offset); |
Manman Ren | ca83518 | 2013-04-11 23:02:56 +0000 | [diff] [blame^] | 119 | |
| 120 | /// Get the sclar tag MDNode for a given scalar type. |
| 121 | llvm::MDNode *getTBAAScalarTagInfo(llvm::MDNode *AccessNode); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // end namespace CodeGen |
| 125 | } // end namespace clang |
| 126 | |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 127 | namespace llvm { |
| 128 | |
| 129 | template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> { |
| 130 | static clang::CodeGen::TBAAPathTag getEmptyKey() { |
| 131 | return clang::CodeGen::TBAAPathTag( |
| 132 | DenseMapInfo<const clang::Type *>::getEmptyKey(), |
| 133 | DenseMapInfo<const MDNode *>::getEmptyKey(), |
| 134 | DenseMapInfo<uint64_t>::getEmptyKey()); |
| 135 | } |
| 136 | |
| 137 | static clang::CodeGen::TBAAPathTag getTombstoneKey() { |
| 138 | return clang::CodeGen::TBAAPathTag( |
| 139 | DenseMapInfo<const clang::Type *>::getTombstoneKey(), |
| 140 | DenseMapInfo<const MDNode *>::getTombstoneKey(), |
| 141 | DenseMapInfo<uint64_t>::getTombstoneKey()); |
| 142 | } |
| 143 | |
| 144 | static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) { |
| 145 | return DenseMapInfo<const clang::Type *>::getHashValue(Val.BaseT) ^ |
| 146 | DenseMapInfo<const MDNode *>::getHashValue(Val.AccessN) ^ |
| 147 | DenseMapInfo<uint64_t>::getHashValue(Val.Offset); |
| 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 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 160 | #endif |