Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 1 | //===--- CodeGenTypes.cpp - TBAA information for LLVM CodeGen -------------===// |
| 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. Relevant standards text includes: |
Dan Gohman | 780658a | 2010-10-15 20:54:41 +0000 | [diff] [blame] | 12 | // |
| 13 | // C99 6.5p7 |
| 14 | // C++ [basic.lval] (p10 in n3126, p15 in some earlier versions) |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "CodeGenTBAA.h" |
| 19 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 20 | #include "clang/AST/Attr.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 21 | #include "clang/AST/Mangle.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
Kostya Serebryany | c9fe605 | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallSet.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Constants.h" |
| 26 | #include "llvm/IR/LLVMContext.h" |
| 27 | #include "llvm/IR/Metadata.h" |
| 28 | #include "llvm/IR/Type.h" |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | using namespace CodeGen; |
| 31 | |
| 32 | CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, |
Kostya Serebryany | c9fe605 | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 33 | const CodeGenOptions &CGO, |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 34 | const LangOptions &Features, MangleContext &MContext) |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 35 | : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext), |
Duncan Sands | 2d7cb06 | 2012-04-15 18:04:54 +0000 | [diff] [blame] | 36 | MDHelper(VMContext), Root(0), Char(0) { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | CodeGenTBAA::~CodeGenTBAA() { |
| 40 | } |
| 41 | |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 42 | llvm::MDNode *CodeGenTBAA::getRoot() { |
| 43 | // Define the root of the tree. This identifies the tree, so that |
| 44 | // if our LLVM IR is linked with LLVM IR from a different front-end |
| 45 | // (or a different version of this front-end), their TBAA trees will |
| 46 | // remain distinct, and the optimizer will treat them conservatively. |
| 47 | if (!Root) |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 48 | Root = MDHelper.createTBAARoot("Simple C/C++ TBAA"); |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 49 | |
| 50 | return Root; |
| 51 | } |
| 52 | |
| 53 | llvm::MDNode *CodeGenTBAA::getChar() { |
| 54 | // Define the root of the tree for user-accessible memory. C and C++ |
| 55 | // give special powers to char and certain similar types. However, |
| 56 | // these special powers only cover user-accessible memory, and doesn't |
| 57 | // include things like vtables. |
| 58 | if (!Char) |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 59 | Char = MDHelper.createTBAANode("omnipotent char", getRoot()); |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 60 | |
| 61 | return Char; |
| 62 | } |
| 63 | |
Dan Gohman | 2ea7e73 | 2010-12-13 23:51:08 +0000 | [diff] [blame] | 64 | static bool TypeHasMayAlias(QualType QTy) { |
| 65 | // Tagged types have declarations, and therefore may have attributes. |
| 66 | if (const TagType *TTy = dyn_cast<TagType>(QTy)) |
| 67 | return TTy->getDecl()->hasAttr<MayAliasAttr>(); |
| 68 | |
| 69 | // Typedef types have declarations, and therefore may have attributes. |
| 70 | if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) { |
| 71 | if (TTy->getDecl()->hasAttr<MayAliasAttr>()) |
| 72 | return true; |
| 73 | // Also, their underlying types may have relevant attributes. |
| 74 | return TypeHasMayAlias(TTy->desugar()); |
| 75 | } |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 80 | llvm::MDNode * |
| 81 | CodeGenTBAA::getTBAAInfo(QualType QTy) { |
Kostya Serebryany | c9fe605 | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 82 | // At -O0 TBAA is not emitted for regular types. |
| 83 | if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) |
| 84 | return NULL; |
| 85 | |
Dan Gohman | 2ea7e73 | 2010-12-13 23:51:08 +0000 | [diff] [blame] | 86 | // If the type has the may_alias attribute (even on a typedef), it is |
| 87 | // effectively in the general char alias class. |
| 88 | if (TypeHasMayAlias(QTy)) |
| 89 | return getChar(); |
| 90 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 91 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 92 | |
| 93 | if (llvm::MDNode *N = MetadataCache[Ty]) |
| 94 | return N; |
| 95 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 96 | // Handle builtin types. |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 97 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 98 | switch (BTy->getKind()) { |
Dan Gohman | 8562348 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 99 | // Character types are special and can alias anything. |
| 100 | // In C++, this technically only includes "char" and "unsigned char", |
| 101 | // and not "signed char". In C, it includes all three. For now, |
Dan Gohman | 0a53198 | 2010-10-15 20:24:10 +0000 | [diff] [blame] | 102 | // the risk of exploiting this detail in C++ seems likely to outweigh |
Dan Gohman | 8562348 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 103 | // the benefit. |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 104 | case BuiltinType::Char_U: |
| 105 | case BuiltinType::Char_S: |
| 106 | case BuiltinType::UChar: |
| 107 | case BuiltinType::SChar: |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 108 | return getChar(); |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 109 | |
| 110 | // Unsigned types can alias their corresponding signed types. |
| 111 | case BuiltinType::UShort: |
| 112 | return getTBAAInfo(Context.ShortTy); |
| 113 | case BuiltinType::UInt: |
| 114 | return getTBAAInfo(Context.IntTy); |
| 115 | case BuiltinType::ULong: |
| 116 | return getTBAAInfo(Context.LongTy); |
| 117 | case BuiltinType::ULongLong: |
| 118 | return getTBAAInfo(Context.LongLongTy); |
| 119 | case BuiltinType::UInt128: |
| 120 | return getTBAAInfo(Context.Int128Ty); |
| 121 | |
Dan Gohman | 2f8c21d | 2010-10-15 20:24:53 +0000 | [diff] [blame] | 122 | // Treat all other builtin types as distinct types. This includes |
| 123 | // treating wchar_t, char16_t, and char32_t as distinct from their |
| 124 | // "underlying types". |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 125 | default: |
| 126 | return MetadataCache[Ty] = |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 127 | MDHelper.createTBAANode(BTy->getName(Features), getChar()); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 131 | // Handle pointers. |
Dan Gohman | dc49111 | 2010-10-15 20:26:20 +0000 | [diff] [blame] | 132 | // TODO: Implement C++'s type "similarity" and consider dis-"similar" |
| 133 | // pointers distinct. |
Dan Gohman | c1028f4 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 134 | if (Ty->isPointerType()) |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 135 | return MetadataCache[Ty] = MDHelper.createTBAANode("any pointer", |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 136 | getChar()); |
Dan Gohman | c1028f4 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 137 | |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 138 | // Enum types are distinct types. In C++ they have "underlying types", |
| 139 | // however they aren't related for TBAA. |
| 140 | if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 141 | // In C mode, two anonymous enums are compatible iff their members |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 142 | // are the same -- see C99 6.2.7p1. For now, be conservative. We could |
| 143 | // theoretically implement this by combining information about all the |
| 144 | // members into a single identifying MDNode. |
| 145 | if (!Features.CPlusPlus && |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 146 | ETy->getDecl()->getTypedefNameForAnonDecl()) |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 147 | return MetadataCache[Ty] = getChar(); |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 148 | |
| 149 | // In C++ mode, types have linkage, so we can rely on the ODR and |
| 150 | // on their mangled names, if they're external. |
| 151 | // TODO: Is there a way to get a program-wide unique name for a |
| 152 | // decl with local linkage or no linkage? |
| 153 | if (Features.CPlusPlus && |
| 154 | ETy->getDecl()->getLinkage() != ExternalLinkage) |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 155 | return MetadataCache[Ty] = getChar(); |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 156 | |
| 157 | // TODO: This is using the RTTI name. Is there a better way to get |
| 158 | // a unique string for a type? |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 159 | SmallString<256> OutName; |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 160 | llvm::raw_svector_ostream Out(OutName); |
| 161 | MContext.mangleCXXRTTIName(QualType(ETy, 0), Out); |
| 162 | Out.flush(); |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 163 | return MetadataCache[Ty] = MDHelper.createTBAANode(OutName, getChar()); |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 166 | // For now, handle any other kind of type conservatively. |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 167 | return MetadataCache[Ty] = getChar(); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 168 | } |
Kostya Serebryany | 8cb4a07 | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 169 | |
| 170 | llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() { |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 171 | return MDHelper.createTBAANode("vtable pointer", getRoot()); |
Kostya Serebryany | 8cb4a07 | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 172 | } |
Dan Gohman | b22c7dc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 173 | |
| 174 | bool |
| 175 | CodeGenTBAA::CollectFields(uint64_t BaseOffset, |
| 176 | QualType QTy, |
| 177 | SmallVectorImpl<llvm::MDBuilder::TBAAStructField> & |
| 178 | Fields, |
| 179 | bool MayAlias) { |
| 180 | /* Things not handled yet include: C++ base classes, bitfields, */ |
| 181 | |
| 182 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { |
| 183 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
| 184 | if (RD->hasFlexibleArrayMember()) |
| 185 | return false; |
| 186 | |
| 187 | // TODO: Handle C++ base classes. |
| 188 | if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD)) |
| 189 | if (Decl->bases_begin() != Decl->bases_end()) |
| 190 | return false; |
| 191 | |
| 192 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 193 | |
| 194 | unsigned idx = 0; |
| 195 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 196 | e = RD->field_end(); i != e; ++i, ++idx) { |
| 197 | uint64_t Offset = BaseOffset + |
| 198 | Layout.getFieldOffset(idx) / Context.getCharWidth(); |
| 199 | QualType FieldQTy = i->getType(); |
| 200 | if (!CollectFields(Offset, FieldQTy, Fields, |
| 201 | MayAlias || TypeHasMayAlias(FieldQTy))) |
| 202 | return false; |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | /* Otherwise, treat whatever it is as a field. */ |
| 208 | uint64_t Offset = BaseOffset; |
| 209 | uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity(); |
| 210 | llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy); |
| 211 | Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAAInfo)); |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | llvm::MDNode * |
| 216 | CodeGenTBAA::getTBAAStructInfo(QualType QTy) { |
| 217 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
| 218 | |
| 219 | if (llvm::MDNode *N = StructMetadataCache[Ty]) |
| 220 | return N; |
| 221 | |
| 222 | SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields; |
| 223 | if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy))) |
| 224 | return MDHelper.createTBAAStructNode(Fields); |
| 225 | |
| 226 | // For now, handle any other kind of type conservatively. |
| 227 | return StructMetadataCache[Ty] = NULL; |
| 228 | } |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 229 | |
| 230 | /// Check if the given type can be handled by path-aware TBAA. |
| 231 | static bool isTBAAPathStruct(QualType QTy) { |
| 232 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { |
| 233 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
| 234 | // RD can be struct, union, class, interface or enum. |
| 235 | // For now, we only handle struct. |
| 236 | if (RD->isStruct() && !RD->hasFlexibleArrayMember()) |
| 237 | return true; |
| 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | llvm::MDNode * |
| 243 | CodeGenTBAA::getTBAAStructTypeInfo(QualType QTy) { |
| 244 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
| 245 | assert(isTBAAPathStruct(QTy)); |
| 246 | |
| 247 | if (llvm::MDNode *N = StructTypeMetadataCache[Ty]) |
| 248 | return N; |
| 249 | |
| 250 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { |
| 251 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
| 252 | |
| 253 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 254 | SmallVector <std::pair<uint64_t, llvm::MDNode*>, 4> Fields; |
| 255 | // To reduce the size of MDNode for a given struct type, we only output |
| 256 | // once for all the fields with the same scalar types. |
| 257 | // Offsets for scalar fields in the type DAG are not used. |
| 258 | llvm::SmallSet <llvm::MDNode*, 4> ScalarFieldTypes; |
| 259 | unsigned idx = 0; |
| 260 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 261 | e = RD->field_end(); i != e; ++i, ++idx) { |
| 262 | QualType FieldQTy = i->getType(); |
| 263 | llvm::MDNode *FieldNode; |
| 264 | if (isTBAAPathStruct(FieldQTy)) |
| 265 | FieldNode = getTBAAStructTypeInfo(FieldQTy); |
| 266 | else { |
| 267 | FieldNode = getTBAAInfo(FieldQTy); |
| 268 | // Ignore this field if the type already exists. |
| 269 | if (ScalarFieldTypes.count(FieldNode)) |
| 270 | continue; |
| 271 | ScalarFieldTypes.insert(FieldNode); |
| 272 | } |
| 273 | if (!FieldNode) |
| 274 | return StructTypeMetadataCache[Ty] = NULL; |
| 275 | Fields.push_back(std::make_pair( |
| 276 | Layout.getFieldOffset(idx) / Context.getCharWidth(), FieldNode)); |
| 277 | } |
| 278 | |
| 279 | // TODO: This is using the RTTI name. Is there a better way to get |
| 280 | // a unique string for a type? |
| 281 | SmallString<256> OutName; |
| 282 | llvm::raw_svector_ostream Out(OutName); |
| 283 | MContext.mangleCXXRTTIName(QualType(Ty, 0), Out); |
| 284 | Out.flush(); |
| 285 | // Create the struct type node with a vector of pairs (offset, type). |
| 286 | return StructTypeMetadataCache[Ty] = |
| 287 | MDHelper.createTBAAStructTypeNode(OutName, Fields); |
| 288 | } |
| 289 | |
| 290 | return StructMetadataCache[Ty] = NULL; |
| 291 | } |
| 292 | |
| 293 | llvm::MDNode * |
| 294 | CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode, |
| 295 | uint64_t Offset) { |
| 296 | if (!CodeGenOpts.StructPathTBAA) |
| 297 | return AccessNode; |
| 298 | |
| 299 | const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr(); |
| 300 | TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset); |
| 301 | if (llvm::MDNode *N = StructTagMetadataCache[PathTag]) |
| 302 | return N; |
| 303 | |
| 304 | llvm::MDNode *BNode = 0; |
| 305 | if (isTBAAPathStruct(BaseQTy)) |
| 306 | BNode = getTBAAStructTypeInfo(BaseQTy); |
| 307 | if (!BNode) |
| 308 | return StructTagMetadataCache[PathTag] = AccessNode; |
| 309 | |
| 310 | return StructTagMetadataCache[PathTag] = |
| 311 | MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset); |
| 312 | } |