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