clang API Documentation

CodeGenTBAA.h
Go to the documentation of this file.
00001 //===--- CodeGenTBAA.h - TBAA information for LLVM CodeGen ------*- C++ -*-===//
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.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
00016 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
00017 
00018 #include "clang/Basic/LLVM.h"
00019 #include "llvm/ADT/DenseMap.h"
00020 #include "llvm/IR/MDBuilder.h"
00021 
00022 namespace llvm {
00023   class LLVMContext;
00024   class MDNode;
00025 }
00026 
00027 namespace clang {
00028   class ASTContext;
00029   class CodeGenOptions;
00030   class LangOptions;
00031   class MangleContext;
00032   class QualType;
00033   class Type;
00034 
00035 namespace CodeGen {
00036   class CGRecordLayout;
00037 
00038   struct TBAAPathTag {
00039     TBAAPathTag(const Type *B, const llvm::MDNode *A, uint64_t O)
00040       : BaseT(B), AccessN(A), Offset(O) {}
00041     const Type *BaseT;
00042     const llvm::MDNode *AccessN;
00043     uint64_t Offset;
00044   };
00045 
00046 /// CodeGenTBAA - This class organizes the cross-module state that is used
00047 /// while lowering AST types to LLVM types.
00048 class CodeGenTBAA {
00049   ASTContext &Context;
00050   const CodeGenOptions &CodeGenOpts;
00051   const LangOptions &Features;
00052   MangleContext &MContext;
00053 
00054   // MDHelper - Helper for creating metadata.
00055   llvm::MDBuilder MDHelper;
00056 
00057   /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
00058   /// them.
00059   llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
00060   /// This maps clang::Types to a struct node in the type DAG.
00061   llvm::DenseMap<const Type *, llvm::MDNode *> StructTypeMetadataCache;
00062   /// This maps TBAAPathTags to a tag node.
00063   llvm::DenseMap<TBAAPathTag, llvm::MDNode *> StructTagMetadataCache;
00064   /// This maps a scalar type to a scalar tag node.
00065   llvm::DenseMap<const llvm::MDNode *, llvm::MDNode *> ScalarTagMetadataCache;
00066 
00067   /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
00068   /// them for struct assignments.
00069   llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
00070 
00071   llvm::MDNode *Root;
00072   llvm::MDNode *Char;
00073 
00074   /// getRoot - This is the mdnode for the root of the metadata type graph
00075   /// for this translation unit.
00076   llvm::MDNode *getRoot();
00077 
00078   /// getChar - This is the mdnode for "char", which is special, and any types
00079   /// considered to be equivalent to it.
00080   llvm::MDNode *getChar();
00081 
00082   /// CollectFields - Collect information about the fields of a type for
00083   /// !tbaa.struct metadata formation. Return false for an unsupported type.
00084   bool CollectFields(uint64_t BaseOffset,
00085                      QualType Ty,
00086                      SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
00087                      bool MayAlias);
00088 
00089   /// A wrapper function to create a scalar type. For struct-path aware TBAA,
00090   /// the scalar type has the same format as the struct type: name, offset,
00091   /// pointer to another node in the type DAG.
00092   llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
00093 
00094 public:
00095   CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
00096               const CodeGenOptions &CGO,
00097               const LangOptions &Features,
00098               MangleContext &MContext);
00099   ~CodeGenTBAA();
00100 
00101   /// getTBAAInfo - Get the TBAA MDNode to be used for a dereference
00102   /// of the given type.
00103   llvm::MDNode *getTBAAInfo(QualType QTy);
00104 
00105   /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a
00106   /// dereference of a vtable pointer.
00107   llvm::MDNode *getTBAAInfoForVTablePtr();
00108 
00109   /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
00110   /// the given type.
00111   llvm::MDNode *getTBAAStructInfo(QualType QTy);
00112 
00113   /// Get the MDNode in the type DAG for given struct type QType.
00114   llvm::MDNode *getTBAAStructTypeInfo(QualType QType);
00115   /// Get the tag MDNode for a given base type, the actual scalar access MDNode
00116   /// and offset into the base type.
00117   llvm::MDNode *getTBAAStructTagInfo(QualType BaseQType,
00118                                      llvm::MDNode *AccessNode, uint64_t Offset);
00119 
00120   /// Get the scalar tag MDNode for a given scalar type.
00121   llvm::MDNode *getTBAAScalarTagInfo(llvm::MDNode *AccessNode);
00122 };
00123 
00124 }  // end namespace CodeGen
00125 }  // end namespace clang
00126 
00127 namespace llvm {
00128 
00129 template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> {
00130   static clang::CodeGen::TBAAPathTag getEmptyKey() {
00131     return clang::CodeGen::TBAAPathTag(
00132       DenseMapInfo<const clang::Type *>::getEmptyKey(),
00133       DenseMapInfo<const MDNode *>::getEmptyKey(),
00134       DenseMapInfo<uint64_t>::getEmptyKey());
00135   }
00136 
00137   static clang::CodeGen::TBAAPathTag getTombstoneKey() {
00138     return clang::CodeGen::TBAAPathTag(
00139       DenseMapInfo<const clang::Type *>::getTombstoneKey(),
00140       DenseMapInfo<const MDNode *>::getTombstoneKey(),
00141       DenseMapInfo<uint64_t>::getTombstoneKey());
00142   }
00143 
00144   static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) {
00145     return DenseMapInfo<const clang::Type *>::getHashValue(Val.BaseT) ^
00146            DenseMapInfo<const MDNode *>::getHashValue(Val.AccessN) ^
00147            DenseMapInfo<uint64_t>::getHashValue(Val.Offset);
00148   }
00149 
00150   static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS,
00151                       const clang::CodeGen::TBAAPathTag &RHS) {
00152     return LHS.BaseT == RHS.BaseT &&
00153            LHS.AccessN == RHS.AccessN &&
00154            LHS.Offset == RHS.Offset;
00155   }
00156 };
00157 
00158 }  // end namespace llvm
00159 
00160 #endif