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