Skip to content

Commit 1fa35f0

Browse files
authored
[clang] Avoid recalculating TBAA base type info (#73264)
As nullptr is a legitimate value, change the BaseTypeMetadataCache hash lookup/insertion to use find and insert rather than the subscript operator. Also adjust getBaseTypeInfoHelper to do no insertion, but let getBaseTypeInfo do that.
1 parent d06e175 commit 1fa35f0

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
342342
// field. Virtual bases are more complex and omitted, but avoid an
343343
// incomplete view for NewStructPathTBAA.
344344
if (CodeGenOpts.NewStructPathTBAA && CXXRD->getNumVBases() != 0)
345-
return BaseTypeMetadataCache[Ty] = nullptr;
345+
return nullptr;
346346
for (const CXXBaseSpecifier &B : CXXRD->bases()) {
347347
if (B.isVirtual())
348348
continue;
@@ -354,7 +354,7 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
354354
? getBaseTypeInfo(BaseQTy)
355355
: getTypeInfo(BaseQTy);
356356
if (!TypeNode)
357-
return BaseTypeMetadataCache[Ty] = nullptr;
357+
return nullptr;
358358
uint64_t Offset = Layout.getBaseClassOffset(BaseRD).getQuantity();
359359
uint64_t Size =
360360
Context.getASTRecordLayout(BaseRD).getDataSize().getQuantity();
@@ -378,7 +378,7 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
378378
llvm::MDNode *TypeNode = isValidBaseType(FieldQTy) ?
379379
getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy);
380380
if (!TypeNode)
381-
return BaseTypeMetadataCache[Ty] = nullptr;
381+
return nullptr;
382382

383383
uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex());
384384
uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity();
@@ -418,14 +418,20 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) {
418418
return nullptr;
419419

420420
const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
421-
if (llvm::MDNode *N = BaseTypeMetadataCache[Ty])
422-
return N;
423421

424-
// Note that the following helper call is allowed to add new nodes to the
425-
// cache, which invalidates all its previously obtained iterators. So we
426-
// first generate the node for the type and then add that node to the cache.
422+
// nullptr is a valid value in the cache, so use find rather than []
423+
auto I = BaseTypeMetadataCache.find(Ty);
424+
if (I != BaseTypeMetadataCache.end())
425+
return I->second;
426+
427+
// First calculate the metadata, before recomputing the insertion point, as
428+
// the helper can recursively call us.
427429
llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty);
428-
return BaseTypeMetadataCache[Ty] = TypeNode;
430+
LLVM_ATTRIBUTE_UNUSED auto inserted =
431+
BaseTypeMetadataCache.insert({Ty, TypeNode});
432+
assert(inserted.second && "BaseType metadata was already inserted");
433+
434+
return TypeNode;
429435
}
430436

431437
llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) {

0 commit comments

Comments
 (0)