clang API Documentation
00001 //===--- CodeGenTypes.cpp - TBAA information for LLVM CodeGen -------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This is the code that manages TBAA information and defines the TBAA policy 00011 // for the optimizer to use. Relevant standards text includes: 00012 // 00013 // C99 6.5p7 00014 // C++ [basic.lval] (p10 in n3126, p15 in some earlier versions) 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #include "CodeGenTBAA.h" 00019 #include "clang/AST/ASTContext.h" 00020 #include "clang/AST/Mangle.h" 00021 #include "clang/Frontend/CodeGenOptions.h" 00022 #include "llvm/LLVMContext.h" 00023 #include "llvm/Metadata.h" 00024 #include "llvm/Constants.h" 00025 #include "llvm/Type.h" 00026 using namespace clang; 00027 using namespace CodeGen; 00028 00029 CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, 00030 const CodeGenOptions &CGO, 00031 const LangOptions &Features, MangleContext &MContext) 00032 : Context(Ctx), VMContext(VMContext), CodeGenOpts(CGO), 00033 Features(Features), MContext(MContext), 00034 MDHelper(VMContext), Root(0), Char(0) { 00035 } 00036 00037 CodeGenTBAA::~CodeGenTBAA() { 00038 } 00039 00040 llvm::MDNode *CodeGenTBAA::getRoot() { 00041 // Define the root of the tree. This identifies the tree, so that 00042 // if our LLVM IR is linked with LLVM IR from a different front-end 00043 // (or a different version of this front-end), their TBAA trees will 00044 // remain distinct, and the optimizer will treat them conservatively. 00045 if (!Root) 00046 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA"); 00047 00048 return Root; 00049 } 00050 00051 llvm::MDNode *CodeGenTBAA::getChar() { 00052 // Define the root of the tree for user-accessible memory. C and C++ 00053 // give special powers to char and certain similar types. However, 00054 // these special powers only cover user-accessible memory, and doesn't 00055 // include things like vtables. 00056 if (!Char) 00057 Char = MDHelper.createTBAANode("omnipotent char", getRoot()); 00058 00059 return Char; 00060 } 00061 00062 static bool TypeHasMayAlias(QualType QTy) { 00063 // Tagged types have declarations, and therefore may have attributes. 00064 if (const TagType *TTy = dyn_cast<TagType>(QTy)) 00065 return TTy->getDecl()->hasAttr<MayAliasAttr>(); 00066 00067 // Typedef types have declarations, and therefore may have attributes. 00068 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) { 00069 if (TTy->getDecl()->hasAttr<MayAliasAttr>()) 00070 return true; 00071 // Also, their underlying types may have relevant attributes. 00072 return TypeHasMayAlias(TTy->desugar()); 00073 } 00074 00075 return false; 00076 } 00077 00078 llvm::MDNode * 00079 CodeGenTBAA::getTBAAInfo(QualType QTy) { 00080 // At -O0 TBAA is not emitted for regular types. 00081 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) 00082 return NULL; 00083 00084 // If the type has the may_alias attribute (even on a typedef), it is 00085 // effectively in the general char alias class. 00086 if (TypeHasMayAlias(QTy)) 00087 return getChar(); 00088 00089 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); 00090 00091 if (llvm::MDNode *N = MetadataCache[Ty]) 00092 return N; 00093 00094 // Handle builtin types. 00095 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { 00096 switch (BTy->getKind()) { 00097 // Character types are special and can alias anything. 00098 // In C++, this technically only includes "char" and "unsigned char", 00099 // and not "signed char". In C, it includes all three. For now, 00100 // the risk of exploiting this detail in C++ seems likely to outweigh 00101 // the benefit. 00102 case BuiltinType::Char_U: 00103 case BuiltinType::Char_S: 00104 case BuiltinType::UChar: 00105 case BuiltinType::SChar: 00106 return getChar(); 00107 00108 // Unsigned types can alias their corresponding signed types. 00109 case BuiltinType::UShort: 00110 return getTBAAInfo(Context.ShortTy); 00111 case BuiltinType::UInt: 00112 return getTBAAInfo(Context.IntTy); 00113 case BuiltinType::ULong: 00114 return getTBAAInfo(Context.LongTy); 00115 case BuiltinType::ULongLong: 00116 return getTBAAInfo(Context.LongLongTy); 00117 case BuiltinType::UInt128: 00118 return getTBAAInfo(Context.Int128Ty); 00119 00120 // Treat all other builtin types as distinct types. This includes 00121 // treating wchar_t, char16_t, and char32_t as distinct from their 00122 // "underlying types". 00123 default: 00124 return MetadataCache[Ty] = 00125 MDHelper.createTBAANode(BTy->getName(Features), getChar()); 00126 } 00127 } 00128 00129 // Handle pointers. 00130 // TODO: Implement C++'s type "similarity" and consider dis-"similar" 00131 // pointers distinct. 00132 if (Ty->isPointerType()) 00133 return MetadataCache[Ty] = MDHelper.createTBAANode("any pointer", 00134 getChar()); 00135 00136 // Enum types are distinct types. In C++ they have "underlying types", 00137 // however they aren't related for TBAA. 00138 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { 00139 // In C mode, two anonymous enums are compatible iff their members 00140 // are the same -- see C99 6.2.7p1. For now, be conservative. We could 00141 // theoretically implement this by combining information about all the 00142 // members into a single identifying MDNode. 00143 if (!Features.CPlusPlus && 00144 ETy->getDecl()->getTypedefNameForAnonDecl()) 00145 return MetadataCache[Ty] = getChar(); 00146 00147 // In C++ mode, types have linkage, so we can rely on the ODR and 00148 // on their mangled names, if they're external. 00149 // TODO: Is there a way to get a program-wide unique name for a 00150 // decl with local linkage or no linkage? 00151 if (Features.CPlusPlus && 00152 ETy->getDecl()->getLinkage() != ExternalLinkage) 00153 return MetadataCache[Ty] = getChar(); 00154 00155 // TODO: This is using the RTTI name. Is there a better way to get 00156 // a unique string for a type? 00157 SmallString<256> OutName; 00158 llvm::raw_svector_ostream Out(OutName); 00159 MContext.mangleCXXRTTIName(QualType(ETy, 0), Out); 00160 Out.flush(); 00161 return MetadataCache[Ty] = MDHelper.createTBAANode(OutName, getChar()); 00162 } 00163 00164 // For now, handle any other kind of type conservatively. 00165 return MetadataCache[Ty] = getChar(); 00166 } 00167 00168 llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() { 00169 return MDHelper.createTBAANode("vtable pointer", getRoot()); 00170 }