CodeGenTBAA.cpp revision 7cc0a110bcf3fe9c4d1269ea2ae7e72aa7f37531
16cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer//===--- CodeGenTypes.cpp - TBAA information for LLVM CodeGen -------------===// 26cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// 36cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// The LLVM Compiler Infrastructure 46cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// 56cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// This file is distributed under the University of Illinois Open Source 66cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// License. See LICENSE.TXT for details. 76cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// 86cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer//===----------------------------------------------------------------------===// 96cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// 106cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// This is the code that manages TBAA information and defines the TBAA policy 116cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// for the optimizer to use. Relevant standards text includes: 126cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// 136cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// C99 6.5p7 146cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// C++ [basic.lval] (p10 in n3126, p15 in some earlier versions) 156cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer// 166cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer//===----------------------------------------------------------------------===// 176cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 186cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer#include "CodeGenTBAA.h" 196cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer#include "clang/AST/ASTContext.h" 206cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer#include "clang/AST/Attr.h" 216cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer#include "clang/AST/Mangle.h" 226cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer#include "clang/AST/RecordLayout.h" 236cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer#include "clang/Frontend/CodeGenOptions.h" 246cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer#include "llvm/IR/Constants.h" 256cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer#include "llvm/IR/LLVMContext.h" 266cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer#include "llvm/IR/Metadata.h" 276cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer#include "llvm/IR/Type.h" 286cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramerusing namespace clang; 296cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramerusing namespace CodeGen; 306cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 316cacae8bf9597b8124cd40aedc189c04484e1990Benjamin KramerCodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, 326cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer const CodeGenOptions &CGO, 336cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer const LangOptions &Features, MangleContext &MContext) 346cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext), 356cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer MDHelper(VMContext), Root(0), Char(0) { 366cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer} 376cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 386cacae8bf9597b8124cd40aedc189c04484e1990Benjamin KramerCodeGenTBAA::~CodeGenTBAA() { 396cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer} 406cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 416cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramerllvm::MDNode *CodeGenTBAA::getRoot() { 426cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // Define the root of the tree. This identifies the tree, so that 436cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // if our LLVM IR is linked with LLVM IR from a different front-end 446cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // (or a different version of this front-end), their TBAA trees will 456cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // remain distinct, and the optimizer will treat them conservatively. 466cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer if (!Root) 476cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer Root = MDHelper.createTBAARoot("Simple C/C++ TBAA"); 486cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 496cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer return Root; 506cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer} 516cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 526cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramerllvm::MDNode *CodeGenTBAA::getChar() { 536cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // Define the root of the tree for user-accessible memory. C and C++ 546cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // give special powers to char and certain similar types. However, 556cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // these special powers only cover user-accessible memory, and doesn't 566cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // include things like vtables. 576cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer if (!Char) 586cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer Char = MDHelper.createTBAANode("omnipotent char", getRoot()); 596cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 606cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer return Char; 616cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer} 626cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 636cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramerstatic bool TypeHasMayAlias(QualType QTy) { 646cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // Tagged types have declarations, and therefore may have attributes. 656cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer if (const TagType *TTy = dyn_cast<TagType>(QTy)) 666cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer return TTy->getDecl()->hasAttr<MayAliasAttr>(); 676cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 686cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // Typedef types have declarations, and therefore may have attributes. 696cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) { 706cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer if (TTy->getDecl()->hasAttr<MayAliasAttr>()) 716cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer return true; 726cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // Also, their underlying types may have relevant attributes. 736cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer return TypeHasMayAlias(TTy->desugar()); 746cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer } 756cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 766cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer return false; 776cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer} 786cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 796cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramerllvm::MDNode * 806cacae8bf9597b8124cd40aedc189c04484e1990Benjamin KramerCodeGenTBAA::getTBAAInfo(QualType QTy) { 816cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // At -O0 TBAA is not emitted for regular types. 826cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) 836cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer return NULL; 846cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 856cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // If the type has the may_alias attribute (even on a typedef), it is 866cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // effectively in the general char alias class. 876cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer if (TypeHasMayAlias(QTy)) 886cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer return getChar(); 896cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer 90 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); 91 92 if (llvm::MDNode *N = MetadataCache[Ty]) 93 return N; 94 95 // Handle builtin types. 96 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { 97 switch (BTy->getKind()) { 98 // Character types are special and can alias anything. 99 // In C++, this technically only includes "char" and "unsigned char", 100 // and not "signed char". In C, it includes all three. For now, 101 // the risk of exploiting this detail in C++ seems likely to outweigh 102 // the benefit. 103 case BuiltinType::Char_U: 104 case BuiltinType::Char_S: 105 case BuiltinType::UChar: 106 case BuiltinType::SChar: 107 return getChar(); 108 109 // Unsigned types can alias their corresponding signed types. 110 case BuiltinType::UShort: 111 return getTBAAInfo(Context.ShortTy); 112 case BuiltinType::UInt: 113 return getTBAAInfo(Context.IntTy); 114 case BuiltinType::ULong: 115 return getTBAAInfo(Context.LongTy); 116 case BuiltinType::ULongLong: 117 return getTBAAInfo(Context.LongLongTy); 118 case BuiltinType::UInt128: 119 return getTBAAInfo(Context.Int128Ty); 120 121 // Treat all other builtin types as distinct types. This includes 122 // treating wchar_t, char16_t, and char32_t as distinct from their 123 // "underlying types". 124 default: 125 return MetadataCache[Ty] = 126 MDHelper.createTBAANode(BTy->getName(Features), getChar()); 127 } 128 } 129 130 // Handle pointers. 131 // TODO: Implement C++'s type "similarity" and consider dis-"similar" 132 // pointers distinct. 133 if (Ty->isPointerType()) 134 return MetadataCache[Ty] = MDHelper.createTBAANode("any pointer", 135 getChar()); 136 137 // Enum types are distinct types. In C++ they have "underlying types", 138 // however they aren't related for TBAA. 139 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { 140 // In C mode, two anonymous enums are compatible iff their members 141 // are the same -- see C99 6.2.7p1. For now, be conservative. We could 142 // theoretically implement this by combining information about all the 143 // members into a single identifying MDNode. 144 if (!Features.CPlusPlus && 145 ETy->getDecl()->getTypedefNameForAnonDecl()) 146 return MetadataCache[Ty] = getChar(); 147 148 // In C++ mode, types have linkage, so we can rely on the ODR and 149 // on their mangled names, if they're external. 150 // TODO: Is there a way to get a program-wide unique name for a 151 // decl with local linkage or no linkage? 152 if (Features.CPlusPlus && 153 ETy->getDecl()->getLinkage() != ExternalLinkage) 154 return MetadataCache[Ty] = getChar(); 155 156 // TODO: This is using the RTTI name. Is there a better way to get 157 // a unique string for a type? 158 SmallString<256> OutName; 159 llvm::raw_svector_ostream Out(OutName); 160 MContext.mangleCXXRTTIName(QualType(ETy, 0), Out); 161 Out.flush(); 162 return MetadataCache[Ty] = MDHelper.createTBAANode(OutName, getChar()); 163 } 164 165 // For now, handle any other kind of type conservatively. 166 return MetadataCache[Ty] = getChar(); 167} 168 169llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() { 170 return MDHelper.createTBAANode("vtable pointer", getRoot()); 171} 172 173bool 174CodeGenTBAA::CollectFields(uint64_t BaseOffset, 175 QualType QTy, 176 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> & 177 Fields, 178 bool MayAlias) { 179 /* Things not handled yet include: C++ base classes, bitfields, */ 180 181 if (const RecordType *TTy = QTy->getAs<RecordType>()) { 182 const RecordDecl *RD = TTy->getDecl()->getDefinition(); 183 if (RD->hasFlexibleArrayMember()) 184 return false; 185 186 // TODO: Handle C++ base classes. 187 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD)) 188 if (Decl->bases_begin() != Decl->bases_end()) 189 return false; 190 191 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 192 193 unsigned idx = 0; 194 for (RecordDecl::field_iterator i = RD->field_begin(), 195 e = RD->field_end(); i != e; ++i, ++idx) { 196 uint64_t Offset = BaseOffset + 197 Layout.getFieldOffset(idx) / Context.getCharWidth(); 198 QualType FieldQTy = i->getType(); 199 if (!CollectFields(Offset, FieldQTy, Fields, 200 MayAlias || TypeHasMayAlias(FieldQTy))) 201 return false; 202 } 203 return true; 204 } 205 206 /* Otherwise, treat whatever it is as a field. */ 207 uint64_t Offset = BaseOffset; 208 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity(); 209 llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy); 210 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAAInfo)); 211 return true; 212} 213 214llvm::MDNode * 215CodeGenTBAA::getTBAAStructInfo(QualType QTy) { 216 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); 217 218 if (llvm::MDNode *N = StructMetadataCache[Ty]) 219 return N; 220 221 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields; 222 if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy))) 223 return MDHelper.createTBAAStructNode(Fields); 224 225 // For now, handle any other kind of type conservatively. 226 return StructMetadataCache[Ty] = NULL; 227} 228