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/Attr.h" 00021 #include "clang/AST/Mangle.h" 00022 #include "clang/AST/RecordLayout.h" 00023 #include "clang/Frontend/CodeGenOptions.h" 00024 #include "llvm/ADT/SmallSet.h" 00025 #include "llvm/IR/Constants.h" 00026 #include "llvm/IR/LLVMContext.h" 00027 #include "llvm/IR/Metadata.h" 00028 #include "llvm/IR/Type.h" 00029 using namespace clang; 00030 using namespace CodeGen; 00031 00032 CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, 00033 const CodeGenOptions &CGO, 00034 const LangOptions &Features, MangleContext &MContext) 00035 : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext), 00036 MDHelper(VMContext), Root(nullptr), Char(nullptr) { 00037 } 00038 00039 CodeGenTBAA::~CodeGenTBAA() { 00040 } 00041 00042 llvm::MDNode *CodeGenTBAA::getRoot() { 00043 // Define the root of the tree. This identifies the tree, so that 00044 // if our LLVM IR is linked with LLVM IR from a different front-end 00045 // (or a different version of this front-end), their TBAA trees will 00046 // remain distinct, and the optimizer will treat them conservatively. 00047 if (!Root) 00048 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA"); 00049 00050 return Root; 00051 } 00052 00053 // For both scalar TBAA and struct-path aware TBAA, the scalar type has the 00054 // same format: name, parent node, and offset. 00055 llvm::MDNode *CodeGenTBAA::createTBAAScalarType(StringRef Name, 00056 llvm::MDNode *Parent) { 00057 return MDHelper.createTBAAScalarTypeNode(Name, Parent); 00058 } 00059 00060 llvm::MDNode *CodeGenTBAA::getChar() { 00061 // Define the root of the tree for user-accessible memory. C and C++ 00062 // give special powers to char and certain similar types. However, 00063 // these special powers only cover user-accessible memory, and doesn't 00064 // include things like vtables. 00065 if (!Char) 00066 Char = createTBAAScalarType("omnipotent char", getRoot()); 00067 00068 return Char; 00069 } 00070 00071 static bool TypeHasMayAlias(QualType QTy) { 00072 // Tagged types have declarations, and therefore may have attributes. 00073 if (const TagType *TTy = dyn_cast<TagType>(QTy)) 00074 return TTy->getDecl()->hasAttr<MayAliasAttr>(); 00075 00076 // Typedef types have declarations, and therefore may have attributes. 00077 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) { 00078 if (TTy->getDecl()->hasAttr<MayAliasAttr>()) 00079 return true; 00080 // Also, their underlying types may have relevant attributes. 00081 return TypeHasMayAlias(TTy->desugar()); 00082 } 00083 00084 return false; 00085 } 00086 00087 llvm::MDNode * 00088 CodeGenTBAA::getTBAAInfo(QualType QTy) { 00089 // At -O0 or relaxed aliasing, TBAA is not emitted for regular types. 00090 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) 00091 return nullptr; 00092 00093 // If the type has the may_alias attribute (even on a typedef), it is 00094 // effectively in the general char alias class. 00095 if (TypeHasMayAlias(QTy)) 00096 return getChar(); 00097 00098 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); 00099 00100 if (llvm::MDNode *N = MetadataCache[Ty]) 00101 return N; 00102 00103 // Handle builtin types. 00104 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { 00105 switch (BTy->getKind()) { 00106 // Character types are special and can alias anything. 00107 // In C++, this technically only includes "char" and "unsigned char", 00108 // and not "signed char". In C, it includes all three. For now, 00109 // the risk of exploiting this detail in C++ seems likely to outweigh 00110 // the benefit. 00111 case BuiltinType::Char_U: 00112 case BuiltinType::Char_S: 00113 case BuiltinType::UChar: 00114 case BuiltinType::SChar: 00115 return getChar(); 00116 00117 // Unsigned types can alias their corresponding signed types. 00118 case BuiltinType::UShort: 00119 return getTBAAInfo(Context.ShortTy); 00120 case BuiltinType::UInt: 00121 return getTBAAInfo(Context.IntTy); 00122 case BuiltinType::ULong: 00123 return getTBAAInfo(Context.LongTy); 00124 case BuiltinType::ULongLong: 00125 return getTBAAInfo(Context.LongLongTy); 00126 case BuiltinType::UInt128: 00127 return getTBAAInfo(Context.Int128Ty); 00128 00129 // Treat all other builtin types as distinct types. This includes 00130 // treating wchar_t, char16_t, and char32_t as distinct from their 00131 // "underlying types". 00132 default: 00133 return MetadataCache[Ty] = 00134 createTBAAScalarType(BTy->getName(Features), getChar()); 00135 } 00136 } 00137 00138 // Handle pointers. 00139 // TODO: Implement C++'s type "similarity" and consider dis-"similar" 00140 // pointers distinct. 00141 if (Ty->isPointerType()) 00142 return MetadataCache[Ty] = createTBAAScalarType("any pointer", 00143 getChar()); 00144 00145 // Enum types are distinct types. In C++ they have "underlying types", 00146 // however they aren't related for TBAA. 00147 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { 00148 // In C++ mode, types have linkage, so we can rely on the ODR and 00149 // on their mangled names, if they're external. 00150 // TODO: Is there a way to get a program-wide unique name for a 00151 // decl with local linkage or no linkage? 00152 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible()) 00153 return MetadataCache[Ty] = getChar(); 00154 00155 SmallString<256> OutName; 00156 llvm::raw_svector_ostream Out(OutName); 00157 MContext.mangleTypeName(QualType(ETy, 0), Out); 00158 Out.flush(); 00159 return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar()); 00160 } 00161 00162 // For now, handle any other kind of type conservatively. 00163 return MetadataCache[Ty] = getChar(); 00164 } 00165 00166 llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() { 00167 return createTBAAScalarType("vtable pointer", getRoot()); 00168 } 00169 00170 bool 00171 CodeGenTBAA::CollectFields(uint64_t BaseOffset, 00172 QualType QTy, 00173 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> & 00174 Fields, 00175 bool MayAlias) { 00176 /* Things not handled yet include: C++ base classes, bitfields, */ 00177 00178 if (const RecordType *TTy = QTy->getAs<RecordType>()) { 00179 const RecordDecl *RD = TTy->getDecl()->getDefinition(); 00180 if (RD->hasFlexibleArrayMember()) 00181 return false; 00182 00183 // TODO: Handle C++ base classes. 00184 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD)) 00185 if (Decl->bases_begin() != Decl->bases_end()) 00186 return false; 00187 00188 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 00189 00190 unsigned idx = 0; 00191 for (RecordDecl::field_iterator i = RD->field_begin(), 00192 e = RD->field_end(); i != e; ++i, ++idx) { 00193 uint64_t Offset = BaseOffset + 00194 Layout.getFieldOffset(idx) / Context.getCharWidth(); 00195 QualType FieldQTy = i->getType(); 00196 if (!CollectFields(Offset, FieldQTy, Fields, 00197 MayAlias || TypeHasMayAlias(FieldQTy))) 00198 return false; 00199 } 00200 return true; 00201 } 00202 00203 /* Otherwise, treat whatever it is as a field. */ 00204 uint64_t Offset = BaseOffset; 00205 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity(); 00206 llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy); 00207 llvm::MDNode *TBAATag = getTBAAScalarTagInfo(TBAAInfo); 00208 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag)); 00209 return true; 00210 } 00211 00212 llvm::MDNode * 00213 CodeGenTBAA::getTBAAStructInfo(QualType QTy) { 00214 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); 00215 00216 if (llvm::MDNode *N = StructMetadataCache[Ty]) 00217 return N; 00218 00219 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields; 00220 if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy))) 00221 return MDHelper.createTBAAStructNode(Fields); 00222 00223 // For now, handle any other kind of type conservatively. 00224 return StructMetadataCache[Ty] = nullptr; 00225 } 00226 00227 /// Check if the given type can be handled by path-aware TBAA. 00228 static bool isTBAAPathStruct(QualType QTy) { 00229 if (const RecordType *TTy = QTy->getAs<RecordType>()) { 00230 const RecordDecl *RD = TTy->getDecl()->getDefinition(); 00231 if (RD->hasFlexibleArrayMember()) 00232 return false; 00233 // RD can be struct, union, class, interface or enum. 00234 // For now, we only handle struct and class. 00235 if (RD->isStruct() || RD->isClass()) 00236 return true; 00237 } 00238 return false; 00239 } 00240 00241 llvm::MDNode * 00242 CodeGenTBAA::getTBAAStructTypeInfo(QualType QTy) { 00243 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); 00244 assert(isTBAAPathStruct(QTy)); 00245 00246 if (llvm::MDNode *N = StructTypeMetadataCache[Ty]) 00247 return N; 00248 00249 if (const RecordType *TTy = QTy->getAs<RecordType>()) { 00250 const RecordDecl *RD = TTy->getDecl()->getDefinition(); 00251 00252 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 00253 SmallVector <std::pair<llvm::MDNode*, uint64_t>, 4> Fields; 00254 unsigned idx = 0; 00255 for (RecordDecl::field_iterator i = RD->field_begin(), 00256 e = RD->field_end(); i != e; ++i, ++idx) { 00257 QualType FieldQTy = i->getType(); 00258 llvm::MDNode *FieldNode; 00259 if (isTBAAPathStruct(FieldQTy)) 00260 FieldNode = getTBAAStructTypeInfo(FieldQTy); 00261 else 00262 FieldNode = getTBAAInfo(FieldQTy); 00263 if (!FieldNode) 00264 return StructTypeMetadataCache[Ty] = nullptr; 00265 Fields.push_back(std::make_pair( 00266 FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth())); 00267 } 00268 00269 SmallString<256> OutName; 00270 if (Features.CPlusPlus) { 00271 // Don't use the mangler for C code. 00272 llvm::raw_svector_ostream Out(OutName); 00273 MContext.mangleTypeName(QualType(Ty, 0), Out); 00274 Out.flush(); 00275 } else { 00276 OutName = RD->getName(); 00277 } 00278 // Create the struct type node with a vector of pairs (offset, type). 00279 return StructTypeMetadataCache[Ty] = 00280 MDHelper.createTBAAStructTypeNode(OutName, Fields); 00281 } 00282 00283 return StructMetadataCache[Ty] = nullptr; 00284 } 00285 00286 /// Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA. 00287 llvm::MDNode * 00288 CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode, 00289 uint64_t Offset) { 00290 if (!AccessNode) 00291 return nullptr; 00292 00293 if (!CodeGenOpts.StructPathTBAA) 00294 return getTBAAScalarTagInfo(AccessNode); 00295 00296 const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr(); 00297 TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset); 00298 if (llvm::MDNode *N = StructTagMetadataCache[PathTag]) 00299 return N; 00300 00301 llvm::MDNode *BNode = nullptr; 00302 if (isTBAAPathStruct(BaseQTy)) 00303 BNode = getTBAAStructTypeInfo(BaseQTy); 00304 if (!BNode) 00305 return StructTagMetadataCache[PathTag] = 00306 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0); 00307 00308 return StructTagMetadataCache[PathTag] = 00309 MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset); 00310 } 00311 00312 llvm::MDNode * 00313 CodeGenTBAA::getTBAAScalarTagInfo(llvm::MDNode *AccessNode) { 00314 if (!AccessNode) 00315 return nullptr; 00316 if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode]) 00317 return N; 00318 00319 return ScalarTagMetadataCache[AccessNode] = 00320 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0); 00321 }