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; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 64 | |
Dan Gohman | b22c7dc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 65 | /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing |
| 66 | /// them for struct assignments. |
| 67 | llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache; |
| 68 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 69 | llvm::MDNode *Root; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 70 | llvm::MDNode *Char; |
| 71 | |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 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 | |
Dan Gohman | b22c7dc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 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 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 87 | public: |
| 88 | CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext, |
Kostya Serebryany | c9fe605 | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 89 | const CodeGenOptions &CGO, |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 90 | const LangOptions &Features, |
| 91 | MangleContext &MContext); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 92 | ~CodeGenTBAA(); |
| 93 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 94 | /// getTBAAInfo - Get the TBAA MDNode to be used for a dereference |
| 95 | /// of the given type. |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 96 | llvm::MDNode *getTBAAInfo(QualType QTy); |
Kostya Serebryany | 8cb4a07 | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 97 | |
| 98 | /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a |
| 99 | /// dereference of a vtable pointer. |
| 100 | llvm::MDNode *getTBAAInfoForVTablePtr(); |
Dan Gohman | b22c7dc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 101 | |
| 102 | /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of |
| 103 | /// the given type. |
| 104 | llvm::MDNode *getTBAAStructInfo(QualType QTy); |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 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); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } // end namespace CodeGen |
| 115 | } // end namespace clang |
| 116 | |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 117 | namespace llvm { |
| 118 | |
| 119 | template<> 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 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 150 | #endif |