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 // 10 // This is the code that manages TBAA information. Relevant standards 11 // text includes: 12 // 13 // C99 6.5p7 14 // C++ [basic.lval] (p10 in n3126, p15 in some earlier versions) 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "CodeGenTBAA.h" 19 #include "Mangle.h" 20 #include "clang/AST/ASTContext.h" 21 #include "llvm/LLVMContext.h" 22 #include "llvm/Metadata.h" 23 using namespace clang; 24 using namespace CodeGen; 25 26 CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, 27 const LangOptions &Features, MangleContext &MContext) 28 : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext), 29 Root(0), Char(0) { 30 } 31 32 CodeGenTBAA::~CodeGenTBAA() { 33 } 34 35 llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(llvm::StringRef NameStr, 36 llvm::MDNode *Parent) { 37 llvm::Value *Ops[] = { 38 llvm::MDString::get(VMContext, NameStr), 39 Parent 40 }; 41 42 return llvm::MDNode::get(VMContext, Ops, llvm::array_lengthof(Ops)); 43 } 44 45 llvm::MDNode * 46 CodeGenTBAA::getTBAAInfo(QualType QTy) { 47 Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); 48 49 if (llvm::MDNode *N = MetadataCache[Ty]) 50 return N; 51 52 if (!Root) { 53 Root = getTBAAInfoForNamedType("Experimental TBAA", 0); 54 Char = getTBAAInfoForNamedType("omnipotent char", Root); 55 } 56 57 // For now, just emit a very minimal tree. 58 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { 59 switch (BTy->getKind()) { 60 // Character types are special and can alias anything. 61 // In C++, this technically only includes "char" and "unsigned char", 62 // and not "signed char". In C, it includes all three. For now, 63 // the risk of exploiting this detail in C++ seems likely to outweigh 64 // the benefit. 65 case BuiltinType::Char_U: 66 case BuiltinType::Char_S: 67 case BuiltinType::UChar: 68 case BuiltinType::SChar: 69 return Char; 70 71 // Unsigned types can alias their corresponding signed types. 72 case BuiltinType::UShort: 73 return getTBAAInfo(Context.ShortTy); 74 case BuiltinType::UInt: 75 return getTBAAInfo(Context.IntTy); 76 case BuiltinType::ULong: 77 return getTBAAInfo(Context.LongTy); 78 case BuiltinType::ULongLong: 79 return getTBAAInfo(Context.LongLongTy); 80 case BuiltinType::UInt128: 81 return getTBAAInfo(Context.Int128Ty); 82 83 // Treat all other builtin types as distinct types. This includes 84 // treating wchar_t, char16_t, and char32_t as distinct from their 85 // "underlying types". 86 default: 87 return MetadataCache[Ty] = 88 getTBAAInfoForNamedType(BTy->getName(Features), Char); 89 } 90 } 91 92 // TODO: Implement C++'s type "similarity" and consider dis-"similar" 93 // pointers distinct. 94 if (Ty->isPointerType()) 95 return MetadataCache[Ty] = getTBAAInfoForNamedType("any pointer", Char); 96 97 // Enum types are distinct types. In C++ they have "underlying types", 98 // however they aren't related for TBAA. 99 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { 100 // In C mode, two anonymous enums are compatible iff their members 101 // are the same -- see C99 6.2.7p1. For now, be conservative. We could 102 // theoretically implement this by combining information about all the 103 // members into a single identifying MDNode. 104 if (!Features.CPlusPlus && 105 ETy->getDecl()->getTypedefForAnonDecl()) 106 return MetadataCache[Ty] = Char; 107 108 // In C++ mode, types have linkage, so we can rely on the ODR and 109 // on their mangled names, if they're external. 110 // TODO: Is there a way to get a program-wide unique name for a 111 // decl with local linkage or no linkage? 112 if (Features.CPlusPlus && 113 ETy->getDecl()->getLinkage() != ExternalLinkage) 114 return MetadataCache[Ty] = Char; 115 116 // TODO: This is using the RTTI name. Is there a better way to get 117 // a unique string for a type? 118 llvm::SmallString<256> OutName; 119 MContext.mangleCXXRTTIName(QualType(ETy, 0), OutName); 120 return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, Char); 121 } 122 123 // For now, handle any other kind of type conservatively. 124 return MetadataCache[Ty] = Char; 125 } 126