[PATCH] D39953: [CodeGen] Generate TBAA type descriptors in a more reliable manner

2017-11-21 Thread Ivan Kosarev via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL318752: [CodeGen] Generate TBAA type descriptors in a more 
reliable manner (authored by kosarev).

Changed prior to commit:
  https://reviews.llvm.org/D39953?vs=123563=123754#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D39953

Files:
  cfe/trunk/lib/CodeGen/CodeGenTBAA.cpp
  cfe/trunk/lib/CodeGen/CodeGenTBAA.h

Index: cfe/trunk/lib/CodeGen/CodeGenTBAA.h
===
--- cfe/trunk/lib/CodeGen/CodeGenTBAA.h
+++ cfe/trunk/lib/CodeGen/CodeGenTBAA.h
@@ -143,6 +143,14 @@
   /// pointer to another node in the type DAG.
   llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
 
+  /// getTypeInfoHelper - An internal helper function to generate metadata used
+  /// to describe accesses to objects of the given type.
+  llvm::MDNode *getTypeInfoHelper(const Type *Ty);
+
+  /// getBaseTypeInfoHelper - An internal helper function to generate metadata
+  /// used to describe accesses to objects of the given base type.
+  llvm::MDNode *getBaseTypeInfoHelper(const Type *Ty);
+
 public:
   CodeGenTBAA(ASTContext , llvm::LLVMContext ,
   const CodeGenOptions ,
Index: cfe/trunk/lib/CodeGen/CodeGenTBAA.cpp
===
--- cfe/trunk/lib/CodeGen/CodeGenTBAA.cpp
+++ cfe/trunk/lib/CodeGen/CodeGenTBAA.cpp
@@ -107,29 +107,7 @@
   return false;
 }
 
-llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
-  // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
-  if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
-return nullptr;
-
-  // If the type has the may_alias attribute (even on a typedef), it is
-  // effectively in the general char alias class.
-  if (TypeHasMayAlias(QTy))
-return getChar();
-
-  // We need this function to not fall back to returning the "omnipotent char"
-  // type node for aggregate and union types. Otherwise, any dereference of an
-  // aggregate will result into the may-alias access descriptor, meaning all
-  // subsequent accesses to direct and indirect members of that aggregate will
-  // be considered may-alias too.
-  // TODO: Combine getTypeInfo() and getBaseTypeInfo() into a single function.
-  if (isValidBaseType(QTy))
-return getBaseTypeInfo(QTy);
-
-  const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
-  if (llvm::MDNode *N = MetadataCache[Ty])
-return N;
-
+llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
   // Handle builtin types.
   if (const BuiltinType *BTy = dyn_cast(Ty)) {
 switch (BTy->getKind()) {
@@ -160,23 +138,21 @@
 // treating wchar_t, char16_t, and char32_t as distinct from their
 // "underlying types".
 default:
-  return MetadataCache[Ty] =
-createTBAAScalarType(BTy->getName(Features), getChar());
+  return createTBAAScalarType(BTy->getName(Features), getChar());
 }
   }
 
   // C++1z [basic.lval]p10: "If a program attempts to access the stored value of
   // an object through a glvalue of other than one of the following types the
   // behavior is undefined: [...] a char, unsigned char, or std::byte type."
   if (Ty->isStdByteType())
-return MetadataCache[Ty] = getChar();
+return getChar();
 
   // Handle pointers and references.
   // TODO: Implement C++'s type "similarity" and consider dis-"similar"
   // pointers distinct.
   if (Ty->isPointerType() || Ty->isReferenceType())
-return MetadataCache[Ty] = createTBAAScalarType("any pointer",
-getChar());
+return createTBAAScalarType("any pointer", getChar());
 
   // Enum types are distinct types. In C++ they have "underlying types",
   // however they aren't related for TBAA.
@@ -186,16 +162,46 @@
 // TODO: Is there a way to get a program-wide unique name for a
 // decl with local linkage or no linkage?
 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
-  return MetadataCache[Ty] = getChar();
+  return getChar();
 
 SmallString<256> OutName;
 llvm::raw_svector_ostream Out(OutName);
 MContext.mangleTypeName(QualType(ETy, 0), Out);
-return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
+return createTBAAScalarType(OutName, getChar());
   }
 
   // For now, handle any other kind of type conservatively.
-  return MetadataCache[Ty] = getChar();
+  return getChar();
+}
+
+llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
+  // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
+  if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
+return nullptr;
+
+  // If the type has the may_alias attribute (even on a typedef), it is
+  // effectively in the general char alias class.
+  if (TypeHasMayAlias(QTy))
+return getChar();
+
+  // We need this function to not fall back to returning the "omnipotent 

[PATCH] D39953: [CodeGen] Generate TBAA type descriptors in a more reliable manner

2017-11-20 Thread John McCall via Phabricator via cfe-commits
rjmccall accepted this revision.
rjmccall added a comment.
This revision is now accepted and ready to land.

Okay, looks good.


https://reviews.llvm.org/D39953



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D39953: [CodeGen] Generate TBAA type descriptors in a more reliable manner

2017-11-20 Thread Ivan Kosarev via Phabricator via cfe-commits
kosarev added a comment.

In https://reviews.llvm.org/D39953#929144, @rjmccall wrote:

> I think there might be some cases that intentionally don't cache their normal 
> result, though, so it might be harder than you think.


My understanding is that conceptually every canonical type has a single 
corresponding type node; we shall never return different nodes for the same 
type. With the updated patch we cache nodes for all types, including different 
versions of char. This is supposed to be an improvement saving us some 
execution time.


https://reviews.llvm.org/D39953



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D39953: [CodeGen] Generate TBAA type descriptors in a more reliable manner

2017-11-20 Thread Ivan Kosarev via Phabricator via cfe-commits
kosarev updated this revision to Diff 123563.
kosarev retitled this revision from "[CodeGen] Do not lookup for cached TBAA 
metadata nodes twice" to "[CodeGen] Generate TBAA type descriptors in a more 
reliable manner".
kosarev edited the summary of this revision.
kosarev added a comment.

Reworked to use helper functions to separate producing metadata nodes from 
other code.


https://reviews.llvm.org/D39953

Files:
  lib/CodeGen/CodeGenTBAA.cpp
  lib/CodeGen/CodeGenTBAA.h

Index: lib/CodeGen/CodeGenTBAA.h
===
--- lib/CodeGen/CodeGenTBAA.h
+++ lib/CodeGen/CodeGenTBAA.h
@@ -143,6 +143,14 @@
   /// pointer to another node in the type DAG.
   llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
 
+  /// getTypeInfoHelper - An internal helper function to generate metadata used
+  /// to describe accesses to objects of the given type.
+  llvm::MDNode *getTypeInfoHelper(const Type *Ty);
+
+  /// getBaseTypeInfoHelper - An internal helper function to generate metadata
+  /// used to describe accesses to objects of the given base type.
+  llvm::MDNode *getBaseTypeInfoHelper(const Type *Ty);
+
 public:
   CodeGenTBAA(ASTContext , llvm::LLVMContext ,
   const CodeGenOptions ,
Index: lib/CodeGen/CodeGenTBAA.cpp
===
--- lib/CodeGen/CodeGenTBAA.cpp
+++ lib/CodeGen/CodeGenTBAA.cpp
@@ -107,29 +107,7 @@
   return false;
 }
 
-llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
-  // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
-  if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
-return nullptr;
-
-  // If the type has the may_alias attribute (even on a typedef), it is
-  // effectively in the general char alias class.
-  if (TypeHasMayAlias(QTy))
-return getChar();
-
-  // We need this function to not fall back to returning the "omnipotent char"
-  // type node for aggregate and union types. Otherwise, any dereference of an
-  // aggregate will result into the may-alias access descriptor, meaning all
-  // subsequent accesses to direct and indirect members of that aggregate will
-  // be considered may-alias too.
-  // TODO: Combine getTypeInfo() and getBaseTypeInfo() into a single function.
-  if (isValidBaseType(QTy))
-return getBaseTypeInfo(QTy);
-
-  const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
-  if (llvm::MDNode *N = MetadataCache[Ty])
-return N;
-
+llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
   // Handle builtin types.
   if (const BuiltinType *BTy = dyn_cast(Ty)) {
 switch (BTy->getKind()) {
@@ -160,23 +138,21 @@
 // treating wchar_t, char16_t, and char32_t as distinct from their
 // "underlying types".
 default:
-  return MetadataCache[Ty] =
-createTBAAScalarType(BTy->getName(Features), getChar());
+  return createTBAAScalarType(BTy->getName(Features), getChar());
 }
   }
 
   // C++1z [basic.lval]p10: "If a program attempts to access the stored value of
   // an object through a glvalue of other than one of the following types the
   // behavior is undefined: [...] a char, unsigned char, or std::byte type."
   if (Ty->isStdByteType())
-return MetadataCache[Ty] = getChar();
+return getChar();
 
   // Handle pointers and references.
   // TODO: Implement C++'s type "similarity" and consider dis-"similar"
   // pointers distinct.
   if (Ty->isPointerType() || Ty->isReferenceType())
-return MetadataCache[Ty] = createTBAAScalarType("any pointer",
-getChar());
+return createTBAAScalarType("any pointer", getChar());
 
   // Enum types are distinct types. In C++ they have "underlying types",
   // however they aren't related for TBAA.
@@ -186,16 +162,46 @@
 // TODO: Is there a way to get a program-wide unique name for a
 // decl with local linkage or no linkage?
 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
-  return MetadataCache[Ty] = getChar();
+  return getChar();
 
 SmallString<256> OutName;
 llvm::raw_svector_ostream Out(OutName);
 MContext.mangleTypeName(QualType(ETy, 0), Out);
-return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
+return createTBAAScalarType(OutName, getChar());
   }
 
   // For now, handle any other kind of type conservatively.
-  return MetadataCache[Ty] = getChar();
+  return getChar();
+}
+
+llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
+  // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
+  if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
+return nullptr;
+
+  // If the type has the may_alias attribute (even on a typedef), it is
+  // effectively in the general char alias class.
+  if (TypeHasMayAlias(QTy))
+return getChar();
+
+  // We need this function to not fall back to returning the