CodeGenTBAA.cpp revision dc491118b3ad2b35f047df611107b8a069176ce5
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.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenTBAA.h"
15#include "Mangle.h"
16#include "clang/AST/ASTContext.h"
17#include "llvm/LLVMContext.h"
18#include "llvm/Metadata.h"
19using namespace clang;
20using namespace CodeGen;
21
22CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
23                         const LangOptions &Features, MangleContext &MContext)
24  : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext),
25    Root(0), Char(0) {
26}
27
28CodeGenTBAA::~CodeGenTBAA() {
29}
30
31llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(llvm::StringRef NameStr,
32                                                   llvm::MDNode *Parent) {
33  llvm::Value *Ops[] = {
34    llvm::MDString::get(VMContext, NameStr),
35    Parent
36  };
37
38  return llvm::MDNode::get(VMContext, Ops, llvm::array_lengthof(Ops));
39}
40
41llvm::MDNode *
42CodeGenTBAA::getTBAAInfo(QualType QTy) {
43  Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
44
45  if (llvm::MDNode *N = MetadataCache[Ty])
46    return N;
47
48  if (!Root) {
49    Root = getTBAAInfoForNamedType("Experimental TBAA", 0);
50    Char = getTBAAInfoForNamedType("omnipotent char", Root);
51  }
52
53  // For now, just emit a very minimal tree.
54  if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
55    switch (BTy->getKind()) {
56    // Character types are special and can alias anything.
57    // In C++, this technically only includes "char" and "unsigned char",
58    // and not "signed char". In C, it includes all three. For now,
59    // the risk of exploiting this detail in C++ seems likely to outweigh
60    // the benefit.
61    case BuiltinType::Char_U:
62    case BuiltinType::Char_S:
63    case BuiltinType::UChar:
64    case BuiltinType::SChar:
65      return Char;
66
67    // Unsigned types can alias their corresponding signed types.
68    case BuiltinType::UShort:
69      return getTBAAInfo(Context.ShortTy);
70    case BuiltinType::UInt:
71      return getTBAAInfo(Context.IntTy);
72    case BuiltinType::ULong:
73      return getTBAAInfo(Context.LongTy);
74    case BuiltinType::ULongLong:
75      return getTBAAInfo(Context.LongLongTy);
76    case BuiltinType::UInt128:
77      return getTBAAInfo(Context.Int128Ty);
78
79    // Treat all other builtin types as distinct types. This includes
80    // treating wchar_t, char16_t, and char32_t as distinct from their
81    // "underlying types".
82    default:
83      return MetadataCache[Ty] =
84               getTBAAInfoForNamedType(BTy->getName(Features), Char);
85    }
86  }
87
88  // TODO: Implement C++'s type "similarity" and consider dis-"similar"
89  // pointers distinct.
90  if (Ty->isPointerType())
91    return MetadataCache[Ty] = getTBAAInfoForNamedType("any pointer", Char);
92
93  // Enum types are distinct types. In C++ they have "underlying types",
94  // however they aren't related for TBAA.
95  if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
96    // In C mode, two anonymous enums are compatible iff their members
97    // are the same -- see C99 6.2.7p1. For now, be conservative. We could
98    // theoretically implement this by combining information about all the
99    // members into a single identifying MDNode.
100    if (!Features.CPlusPlus &&
101        ETy->getDecl()->getTypedefForAnonDecl())
102      return MetadataCache[Ty] = Char;
103
104    // In C++ mode, types have linkage, so we can rely on the ODR and
105    // on their mangled names, if they're external.
106    // TODO: Is there a way to get a program-wide unique name for a
107    // decl with local linkage or no linkage?
108    if (Features.CPlusPlus &&
109        ETy->getDecl()->getLinkage() != ExternalLinkage)
110      return MetadataCache[Ty] = Char;
111
112    // TODO: This is using the RTTI name. Is there a better way to get
113    // a unique string for a type?
114    llvm::SmallString<256> OutName;
115    MContext.mangleCXXRTTIName(QualType(ETy, 0), OutName);
116    return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, Char);
117  }
118
119  // For now, handle any other kind of type conservatively.
120  return MetadataCache[Ty] = Char;
121}
122