CodeGenTBAA.cpp revision facde171ae4b8926622a1bffa833732a06f1875b
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/Mangle.h" 21#include "clang/Frontend/CodeGenOptions.h" 22#include "llvm/LLVMContext.h" 23#include "llvm/Metadata.h" 24#include "llvm/Constants.h" 25#include "llvm/Type.h" 26using namespace clang; 27using namespace CodeGen; 28 29CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, 30 const CodeGenOptions &CGO, 31 const LangOptions &Features, MangleContext &MContext) 32 : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext), 33 MDHelper(VMContext), Root(0), Char(0) { 34} 35 36CodeGenTBAA::~CodeGenTBAA() { 37} 38 39llvm::MDNode *CodeGenTBAA::getRoot() { 40 // Define the root of the tree. This identifies the tree, so that 41 // if our LLVM IR is linked with LLVM IR from a different front-end 42 // (or a different version of this front-end), their TBAA trees will 43 // remain distinct, and the optimizer will treat them conservatively. 44 if (!Root) 45 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA"); 46 47 return Root; 48} 49 50llvm::MDNode *CodeGenTBAA::getChar() { 51 // Define the root of the tree for user-accessible memory. C and C++ 52 // give special powers to char and certain similar types. However, 53 // these special powers only cover user-accessible memory, and doesn't 54 // include things like vtables. 55 if (!Char) 56 Char = MDHelper.createTBAANode("omnipotent char", getRoot()); 57 58 return Char; 59} 60 61static bool TypeHasMayAlias(QualType QTy) { 62 // Tagged types have declarations, and therefore may have attributes. 63 if (const TagType *TTy = dyn_cast<TagType>(QTy)) 64 return TTy->getDecl()->hasAttr<MayAliasAttr>(); 65 66 // Typedef types have declarations, and therefore may have attributes. 67 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) { 68 if (TTy->getDecl()->hasAttr<MayAliasAttr>()) 69 return true; 70 // Also, their underlying types may have relevant attributes. 71 return TypeHasMayAlias(TTy->desugar()); 72 } 73 74 return false; 75} 76 77llvm::MDNode * 78CodeGenTBAA::getTBAAInfo(QualType QTy) { 79 // At -O0 TBAA is not emitted for regular types. 80 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) 81 return NULL; 82 83 // If the type has the may_alias attribute (even on a typedef), it is 84 // effectively in the general char alias class. 85 if (TypeHasMayAlias(QTy)) 86 return getChar(); 87 88 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); 89 90 if (llvm::MDNode *N = MetadataCache[Ty]) 91 return N; 92 93 // Handle builtin types. 94 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { 95 switch (BTy->getKind()) { 96 // Character types are special and can alias anything. 97 // In C++, this technically only includes "char" and "unsigned char", 98 // and not "signed char". In C, it includes all three. For now, 99 // the risk of exploiting this detail in C++ seems likely to outweigh 100 // the benefit. 101 case BuiltinType::Char_U: 102 case BuiltinType::Char_S: 103 case BuiltinType::UChar: 104 case BuiltinType::SChar: 105 return getChar(); 106 107 // Unsigned types can alias their corresponding signed types. 108 case BuiltinType::UShort: 109 return getTBAAInfo(Context.ShortTy); 110 case BuiltinType::UInt: 111 return getTBAAInfo(Context.IntTy); 112 case BuiltinType::ULong: 113 return getTBAAInfo(Context.LongTy); 114 case BuiltinType::ULongLong: 115 return getTBAAInfo(Context.LongLongTy); 116 case BuiltinType::UInt128: 117 return getTBAAInfo(Context.Int128Ty); 118 119 // Treat all other builtin types as distinct types. This includes 120 // treating wchar_t, char16_t, and char32_t as distinct from their 121 // "underlying types". 122 default: 123 return MetadataCache[Ty] = 124 MDHelper.createTBAANode(BTy->getName(Features), getChar()); 125 } 126 } 127 128 // Handle pointers. 129 // TODO: Implement C++'s type "similarity" and consider dis-"similar" 130 // pointers distinct. 131 if (Ty->isPointerType()) 132 return MetadataCache[Ty] = MDHelper.createTBAANode("any pointer", 133 getChar()); 134 135 // Enum types are distinct types. In C++ they have "underlying types", 136 // however they aren't related for TBAA. 137 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { 138 // In C mode, two anonymous enums are compatible iff their members 139 // are the same -- see C99 6.2.7p1. For now, be conservative. We could 140 // theoretically implement this by combining information about all the 141 // members into a single identifying MDNode. 142 if (!Features.CPlusPlus && 143 ETy->getDecl()->getTypedefNameForAnonDecl()) 144 return MetadataCache[Ty] = getChar(); 145 146 // In C++ mode, types have linkage, so we can rely on the ODR and 147 // on their mangled names, if they're external. 148 // TODO: Is there a way to get a program-wide unique name for a 149 // decl with local linkage or no linkage? 150 if (Features.CPlusPlus && 151 ETy->getDecl()->getLinkage() != ExternalLinkage) 152 return MetadataCache[Ty] = getChar(); 153 154 // TODO: This is using the RTTI name. Is there a better way to get 155 // a unique string for a type? 156 SmallString<256> OutName; 157 llvm::raw_svector_ostream Out(OutName); 158 MContext.mangleCXXRTTIName(QualType(ETy, 0), Out); 159 Out.flush(); 160 return MetadataCache[Ty] = MDHelper.createTBAANode(OutName, getChar()); 161 } 162 163 // For now, handle any other kind of type conservatively. 164 return MetadataCache[Ty] = getChar(); 165} 166 167llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() { 168 return MDHelper.createTBAANode("vtable pointer", getRoot()); 169} 170