CodeGenTBAA.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "CodeGenTBAA.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "llvm/LLVMContext.h"
  16. #include "llvm/Metadata.h"
  17. using namespace clang;
  18. using namespace CodeGen;
  19. CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
  20. const LangOptions &Features)
  21. : Context(Ctx), VMContext(VMContext), Features(Features), Root(0), Char(0) {
  22. }
  23. CodeGenTBAA::~CodeGenTBAA() {
  24. }
  25. llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(const char *NameStr,
  26. llvm::MDNode *Parent) {
  27. llvm::Value *Ops[] = {
  28. llvm::MDString::get(VMContext, NameStr),
  29. Parent
  30. };
  31. return llvm::MDNode::get(VMContext, Ops, llvm::array_lengthof(Ops));
  32. }
  33. llvm::MDNode *
  34. CodeGenTBAA::getTBAAInfo(QualType QTy) {
  35. Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  36. if (llvm::MDNode *N = MetadataCache[Ty])
  37. return N;
  38. if (!Root) {
  39. Root = getTBAAInfoForNamedType("Experimental TBAA", 0);
  40. Char = getTBAAInfoForNamedType("omnipotent char", Root);
  41. }
  42. // For now, just emit a very minimal tree.
  43. if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
  44. switch (BTy->getKind()) {
  45. // Charactar types are special and can alias anything.
  46. case BuiltinType::Char_U:
  47. case BuiltinType::Char_S:
  48. case BuiltinType::UChar:
  49. case BuiltinType::SChar:
  50. return Char;
  51. // Unsigned types can alias their corresponding signed types.
  52. case BuiltinType::UShort:
  53. return getTBAAInfo(Context.ShortTy);
  54. case BuiltinType::UInt:
  55. return getTBAAInfo(Context.IntTy);
  56. case BuiltinType::ULong:
  57. return getTBAAInfo(Context.LongTy);
  58. case BuiltinType::ULongLong:
  59. return getTBAAInfo(Context.LongLongTy);
  60. case BuiltinType::UInt128:
  61. return getTBAAInfo(Context.Int128Ty);
  62. // Other builtin types.
  63. default:
  64. return MetadataCache[Ty] =
  65. getTBAAInfoForNamedType(BTy->getName(Features), Char);
  66. }
  67. }
  68. // For now, treat all pointers as equivalent to each other.
  69. if (Ty->isPointerType())
  70. return MetadataCache[Ty] = getTBAAInfoForNamedType("TBAA.pointer", Char);
  71. // For now, handle any other kind of type conservatively.
  72. return MetadataCache[Ty] = Char;
  73. }