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 and defines the TBAA policy 11 // for the optimizer to use. Relevant standards 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 "clang/AST/ASTContext.h" 20 #include "clang/AST/RecordLayout.h" 21 #include "clang/AST/Mangle.h" 22 #include "clang/Frontend/CodeGenOptions.h" 23 #include "llvm/LLVMContext.h" 24 #include "llvm/Metadata.h" 25 #include "llvm/Constants.h" 26 #include "llvm/Type.h" 27 using namespace clang; 28 using namespace CodeGen; 29 30 CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, 31 const CodeGenOptions &CGO, 32 const LangOptions &Features, MangleContext &MContext) 33 : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext), 34 MDHelper(VMContext), Root(0), Char(0) { 35 } 36 37 CodeGenTBAA::~CodeGenTBAA() { 38 } 39 40 llvm::MDNode *CodeGenTBAA::getRoot() { 41 // Define the root of the tree. This identifies the tree, so that 42 // if our LLVM IR is linked with LLVM IR from a different front-end 43 // (or a different version of this front-end), their TBAA trees will 44 // remain distinct, and the optimizer will treat them conservatively. 45 if (!Root) 46 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA"); 47 48 return Root; 49 } 50 51 llvm::MDNode *CodeGenTBAA::getChar() { 52 // Define the root of the tree for user-accessible memory. C and C++ 53 // give special powers to char and certain similar types. However, 54 // these special powers only cover user-accessible memory, and doesn't 55 // include things like vtables. 56 if (!Char) 57 Char = MDHelper.createTBAANode("omnipotent char", getRoot()); 58 59 return Char; 60 } 61 62 static bool TypeHasMayAlias(QualType QTy) { 63 // Tagged types have declarations, and therefore may have attributes. 64 if (const TagType *TTy = dyn_cast<TagType>(QTy)) 65 return TTy->getDecl()->hasAttr<MayAliasAttr>(); 66 67 // Typedef types have declarations, and therefore may have attributes. 68 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) { 69 if (TTy->getDecl()->hasAttr<MayAliasAttr>()) 70 return true; 71 // Also, their underlying types may have relevant attributes. 72 return TypeHasMayAlias(TTy->desugar()); 73 } 74 75 return false; 76 } 77 78 llvm::MDNode * 79 CodeGenTBAA::getTBAAInfo(QualType QTy) { 80 // At -O0 TBAA is not emitted for regular types. 81 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) 82 return NULL; 83 84 // If the type has the may_alias attribute (even on a typedef), it is 85 // effectively in the general char alias class. 86 if (TypeHasMayAlias(QTy)) 87 return getChar(); 88 89 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); 90 91 if (llvm::MDNode *N = MetadataCache[Ty]) 92 return N; 93 94 // Handle builtin types. 95 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { 96 switch (BTy->getKind()) { 97 // Character types are special and can alias anything. 98 // In C++, this technically only includes "char" and "unsigned char", 99 // and not "signed char". In C, it includes all three. For now, 100 // the risk of exploiting this detail in C++ seems likely to outweigh 101 // the benefit. 102 case BuiltinType::Char_U: 103 case BuiltinType::Char_S: 104 case BuiltinType::UChar: 105 case BuiltinType::SChar: 106 return getChar(); 107 108 // Unsigned types can alias their corresponding signed types. 109 case BuiltinType::UShort: 110 return getTBAAInfo(Context.ShortTy); 111 case BuiltinType::UInt: 112 return getTBAAInfo(Context.IntTy); 113 case BuiltinType::ULong: 114 return getTBAAInfo(Context.LongTy); 115 case BuiltinType::ULongLong: 116 return getTBAAInfo(Context.LongLongTy); 117 case BuiltinType::UInt128: 118 return getTBAAInfo(Context.Int128Ty); 119 120 // Treat all other builtin types as distinct types. This includes 121 // treating wchar_t, char16_t, and char32_t as distinct from their 122 // "underlying types". 123 default: 124 return MetadataCache[Ty] = 125 MDHelper.createTBAANode(BTy->getName(Features), getChar()); 126 } 127 } 128 129 // Handle pointers. 130 // TODO: Implement C++'s type "similarity" and consider dis-"similar" 131 // pointers distinct. 132 if (Ty->isPointerType()) 133 return MetadataCache[Ty] = MDHelper.createTBAANode("any pointer", 134 getChar()); 135 136 // Enum types are distinct types. In C++ they have "underlying types", 137 // however they aren't related for TBAA. 138 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { 139 // In C mode, two anonymous enums are compatible iff their members 140 // are the same -- see C99 6.2.7p1. For now, be conservative. We could 141 // theoretically implement this by combining information about all the 142 // members into a single identifying MDNode. 143 if (!Features.CPlusPlus && 144 ETy->getDecl()->getTypedefNameForAnonDecl()) 145 return MetadataCache[Ty] = getChar(); 146 147 // In C++ mode, types have linkage, so we can rely on the ODR and 148 // on their mangled names, if they're external. 149 // TODO: Is there a way to get a program-wide unique name for a 150 // decl with local linkage or no linkage? 151 if (Features.CPlusPlus && 152 ETy->getDecl()->getLinkage() != ExternalLinkage) 153 return MetadataCache[Ty] = getChar(); 154 155 // TODO: This is using the RTTI name. Is there a better way to get 156 // a unique string for a type? 157 SmallString<256> OutName; 158 llvm::raw_svector_ostream Out(OutName); 159 MContext.mangleCXXRTTIName(QualType(ETy, 0), Out); 160 Out.flush(); 161 return MetadataCache[Ty] = MDHelper.createTBAANode(OutName, getChar()); 162 } 163 164 // For now, handle any other kind of type conservatively. 165 return MetadataCache[Ty] = getChar(); 166 } 167 168 llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() { 169 return MDHelper.createTBAANode("vtable pointer", getRoot()); 170 } 171 172 bool 173 CodeGenTBAA::CollectFields(uint64_t BaseOffset, 174 QualType QTy, 175 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> & 176 Fields, 177 bool MayAlias) { 178 /* Things not handled yet include: C++ base classes, bitfields, */ 179 180 if (const RecordType *TTy = QTy->getAs<RecordType>()) { 181 const RecordDecl *RD = TTy->getDecl()->getDefinition(); 182 if (RD->hasFlexibleArrayMember()) 183 return false; 184 185 // TODO: Handle C++ base classes. 186 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD)) 187 if (Decl->bases_begin() != Decl->bases_end()) 188 return false; 189 190 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 191 192 unsigned idx = 0; 193 for (RecordDecl::field_iterator i = RD->field_begin(), 194 e = RD->field_end(); i != e; ++i, ++idx) { 195 uint64_t Offset = BaseOffset + 196 Layout.getFieldOffset(idx) / Context.getCharWidth(); 197 QualType FieldQTy = i->getType(); 198 if (!CollectFields(Offset, FieldQTy, Fields, 199 MayAlias || TypeHasMayAlias(FieldQTy))) 200 return false; 201 } 202 return true; 203 } 204 205 /* Otherwise, treat whatever it is as a field. */ 206 uint64_t Offset = BaseOffset; 207 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity(); 208 llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy); 209 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAAInfo)); 210 return true; 211 } 212 213 llvm::MDNode * 214 CodeGenTBAA::getTBAAStructInfo(QualType QTy) { 215 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); 216 217 if (llvm::MDNode *N = StructMetadataCache[Ty]) 218 return N; 219 220 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields; 221 if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy))) 222 return MDHelper.createTBAAStructNode(Fields); 223 224 // For now, handle any other kind of type conservatively. 225 return StructMetadataCache[Ty] = NULL; 226 } 227