Skip to content

Commit b7098ff

Browse files
committed
Try caching the lookup results
1 parent 0e7470f commit b7098ff

File tree

8 files changed

+77
-79
lines changed

8 files changed

+77
-79
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
12181218
#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) CanQualType SingletonId;
12191219
#include "clang/Basic/HLSLIntangibleTypes.def"
12201220

1221+
// Cache size_t and ptrdiff_t typedefs
1222+
// (C99 7.17), defined in <stddef.h>.
1223+
mutable QualType PtrDiffTy;
1224+
mutable QualType UnsignedSizeTy;
1225+
12211226
// Types for deductions in C++0x [stmt.ranged]'s desugaring. Built on demand.
12221227
mutable QualType AutoDeductTy; // Deduction against 'auto'.
12231228
mutable QualType AutoRRefDeductTy; // Deduction against 'auto &&'.
@@ -1941,11 +1946,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
19411946
/// <stddef.h>.
19421947
///
19431948
/// The sizeof operator requires this (C99 6.5.3.4p4).
1944-
CanQualType getSizeType() const;
1949+
QualType getSizeType() const;
19451950

19461951
/// Return the unique signed counterpart of
19471952
/// the integer type corresponding to size_t.
1948-
CanQualType getSignedSizeType() const;
1953+
QualType getSignedSizeType() const;
19491954

19501955
/// Return the unique type for "intmax_t" (C99 7.18.1.5), defined in
19511956
/// <stdint.h>.
@@ -2442,10 +2447,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
24422447
QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error,
24432448
unsigned *IntegerConstantArgs = nullptr) const;
24442449

2445-
QualType getCGlobalCXXStdNSTypedef(const NamespaceDecl *StdNS,
2446-
StringRef DefName,
2447-
QualType FallBack = {}) const;
2448-
24492450
/// Types and expressions required to build C++2a three-way comparisons
24502451
/// using operator<=>, including the values return by builtin <=> operators.
24512452
ComparisonCategories CompCategories;

clang/lib/AST/ASTContext.cpp

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6666,16 +6666,55 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
66666666
return getTypeDeclType(const_cast<TagDecl*>(Decl));
66676667
}
66686668

6669+
static QualType LookupCGlobalCXXStdNSTypedef(const ASTContext &Ctx,
6670+
StringRef DefName,
6671+
CanQualType const &CanType) {
6672+
DeclContextLookupResult Lookup;
6673+
if (Ctx.getLangOpts().C99) {
6674+
Lookup = Ctx.getTranslationUnitDecl()->lookup(&Ctx.Idents.get(DefName));
6675+
} else if (Ctx.getLangOpts().CPlusPlus) {
6676+
const NamespaceDecl *StdNS = nullptr;
6677+
auto LookupStdNS =
6678+
Ctx.getTranslationUnitDecl()->lookup(&Ctx.Idents.get("std"));
6679+
if (!LookupStdNS.empty()) {
6680+
StdNS = dyn_cast<NamespaceDecl>(LookupStdNS.front());
6681+
}
6682+
if (StdNS) {
6683+
Lookup = StdNS->lookup(&Ctx.Idents.get(DefName));
6684+
} else {
6685+
Lookup = Ctx.getTranslationUnitDecl()->lookup(&Ctx.Idents.get(DefName));
6686+
}
6687+
}
6688+
if (!Lookup.empty()) {
6689+
if (auto *TD = dyn_cast<TypedefNameDecl>(Lookup.front())) {
6690+
auto Result = Ctx.getTypeDeclType(TD);
6691+
if (!Result.isNull() && Ctx.hasSameType(Result, CanType) &&
6692+
Ctx.getTypeAlign(Result) == Ctx.getTypeAlign(CanType)) {
6693+
return Result;
6694+
}
6695+
}
6696+
}
6697+
return QualType();
6698+
}
6699+
66696700
/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
66706701
/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
66716702
/// needs to agree with the definition in <stddef.h>.
6672-
CanQualType ASTContext::getSizeType() const {
6673-
return getFromTargetType(Target->getSizeType());
6703+
QualType ASTContext::getSizeType() const {
6704+
if (UnsignedSizeTy.isNull()) {
6705+
auto CanType = getFromTargetType(Target->getSizeType());
6706+
if (auto TypeDef = LookupCGlobalCXXStdNSTypedef(*this, "size_t", CanType);
6707+
TypeDef.isNull())
6708+
return CanType;
6709+
else
6710+
UnsignedSizeTy = TypeDef;
6711+
}
6712+
return UnsignedSizeTy;
66746713
}
66756714

66766715
/// Return the unique signed counterpart of the integer type
66776716
/// corresponding to size_t.
6678-
CanQualType ASTContext::getSignedSizeType() const {
6717+
QualType ASTContext::getSignedSizeType() const {
66796718
return getFromTargetType(Target->getSignedSizeType());
66806719
}
66816720

@@ -6714,7 +6753,16 @@ QualType ASTContext::getUIntPtrType() const {
67146753
/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
67156754
/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
67166755
QualType ASTContext::getPointerDiffType() const {
6717-
return getFromTargetType(Target->getPtrDiffType(LangAS::Default));
6756+
if (PtrDiffTy.isNull()) {
6757+
auto CanType = getFromTargetType(Target->getPtrDiffType(LangAS::Default));
6758+
if (auto TypeDef =
6759+
LookupCGlobalCXXStdNSTypedef(*this, "ptrdiff_t", CanType);
6760+
TypeDef.isNull())
6761+
return CanType;
6762+
else
6763+
PtrDiffTy = TypeDef;
6764+
}
6765+
return PtrDiffTy;
67186766
}
67196767

67206768
/// Return the unique unsigned counterpart of "ptrdiff_t"
@@ -12556,35 +12604,6 @@ QualType ASTContext::GetBuiltinType(unsigned Id,
1255612604
return getFunctionType(ResType, ArgTypes, EPI);
1255712605
}
1255812606

12559-
QualType ASTContext::getCGlobalCXXStdNSTypedef(const NamespaceDecl *StdNS,
12560-
StringRef DefName,
12561-
QualType FallBack) const {
12562-
DeclContextLookupResult Lookup;
12563-
if (getLangOpts().C99) {
12564-
Lookup = getTranslationUnitDecl()->lookup(&Idents.get(DefName));
12565-
} else if (getLangOpts().CPlusPlus) {
12566-
if (StdNS == nullptr) {
12567-
auto LookupStdNS = getTranslationUnitDecl()->lookup(&Idents.get("std"));
12568-
if (!LookupStdNS.empty()) {
12569-
StdNS = dyn_cast<NamespaceDecl>(LookupStdNS.front());
12570-
}
12571-
}
12572-
if (StdNS) {
12573-
Lookup = StdNS->lookup(&Idents.get(DefName));
12574-
} else {
12575-
Lookup = getTranslationUnitDecl()->lookup(&Idents.get(DefName));
12576-
}
12577-
}
12578-
if (!Lookup.empty()) {
12579-
if (auto *TD = dyn_cast<TypedefNameDecl>(Lookup.front())) {
12580-
if (auto Result = getTypeDeclType(TD); !Result.isNull()) {
12581-
return Result;
12582-
}
12583-
}
12584-
}
12585-
return FallBack;
12586-
}
12587-
1258812607
static GVALinkage basicGVALinkageForFunction(const ASTContext &Context,
1258912608
const FunctionDecl *FD) {
1259012609
if (!FD->isExternallyVisible())

clang/lib/AST/ExprCXX.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,10 +1700,8 @@ SizeOfPackExpr *SizeOfPackExpr::Create(ASTContext &Context,
17001700
ArrayRef<TemplateArgument> PartialArgs) {
17011701
void *Storage =
17021702
Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
1703-
return new (Storage) SizeOfPackExpr(
1704-
Context.getCGlobalCXXStdNSTypedef(nullptr, "size_t",
1705-
Context.getSizeType()),
1706-
OperatorLoc, Pack, PackLoc, RParenLoc, Length, PartialArgs);
1703+
return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1704+
PackLoc, RParenLoc, Length, PartialArgs);
17071705
}
17081706

17091707
SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,

clang/lib/CodeGen/CGCall.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ static void appendParameterTypes(const CodeGenTypes &CGT,
191191
assert(ExtInfos.size() == FPT->getNumParams());
192192
for (unsigned I = 0, E = FPT->getNumParams(); I != E; ++I) {
193193
prefix.push_back(FPT->getParamType(I));
194-
if (ExtInfos[I].hasPassObjectSize())
195-
prefix.push_back(CGT.getContext().getSizeType());
194+
if (ExtInfos[I].hasPassObjectSize()) {
195+
auto &Ctx = CGT.getContext();
196+
prefix.push_back(Ctx.getCanonicalType(Ctx.getSizeType()));
197+
}
196198
}
197199

198200
addExtParameterInfosForCall(paramInfos, FPT.getTypePtr(), PrefixSize,

clang/lib/CodeGen/CGCoroutine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,14 +1002,14 @@ RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E,
10021002
}
10031003
case llvm::Intrinsic::coro_size: {
10041004
auto &Context = getContext();
1005-
CanQualType SizeTy = Context.getSizeType();
1005+
CanQualType SizeTy = Context.getCanonicalType(Context.getSizeType());
10061006
llvm::IntegerType *T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
10071007
llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::coro_size, T);
10081008
return RValue::get(Builder.CreateCall(F));
10091009
}
10101010
case llvm::Intrinsic::coro_align: {
10111011
auto &Context = getContext();
1012-
CanQualType SizeTy = Context.getSizeType();
1012+
CanQualType SizeTy = Context.getCanonicalType(Context.getSizeType());
10131013
llvm::IntegerType *T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
10141014
llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::coro_align, T);
10151015
return RValue::get(Builder.CreateCall(F));

clang/lib/CodeGen/CGObjCMac.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class ObjCCommonTypesHelper {
285285
SmallVector<CanQualType, 5> Params;
286286
Params.push_back(Ctx.VoidPtrTy);
287287
Params.push_back(Ctx.VoidPtrTy);
288-
Params.push_back(Ctx.getSizeType());
288+
Params.push_back(Ctx.getCanonicalType(Ctx.getSizeType()));
289289
Params.push_back(Ctx.BoolTy);
290290
Params.push_back(Ctx.BoolTy);
291291
llvm::FunctionType *FTy = Types.GetFunctionType(

clang/lib/Sema/SemaExpr.cpp

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4025,23 +4025,11 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) {
40254025

40264026
// Does it fit in size_t?
40274027
if (ResultVal.isIntN(SizeTSize)) {
4028-
// Does it fit in ssize_t?
4029-
if (!Literal.isUnsigned && ResultVal[SizeTSize - 1] == 0) {
4030-
auto SignedSize = Context.getSignedSizeType();
4031-
if (auto PtrDiff = Context.getCGlobalCXXStdNSTypedef(
4032-
getStdNamespace(), "ptrdiff_t");
4033-
!PtrDiff.isNull() && Context.hasSameType(PtrDiff, SignedSize))
4034-
Ty = PtrDiff;
4035-
else if (auto SSize = Context.getCGlobalCXXStdNSTypedef(
4036-
getStdNamespace(), "ssize_t");
4037-
!SSize.isNull() && Context.hasSameType(SSize, SignedSize))
4038-
Ty = SSize;
4039-
else
4040-
Ty = SignedSize;
4041-
} else if (AllowUnsigned) {
4042-
Ty = Context.getCGlobalCXXStdNSTypedef(getStdNamespace(), "size_t",
4043-
Context.getSizeType());
4044-
}
4028+
// Does it fit in ptrdiff_t?
4029+
if (!Literal.isUnsigned && ResultVal[SizeTSize - 1] == 0)
4030+
Ty = Context.getPointerDiffType();
4031+
else if (AllowUnsigned)
4032+
Ty = Context.getSizeType();
40454033
Width = SizeTSize;
40464034
}
40474035
}
@@ -4714,11 +4702,7 @@ ExprResult Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo,
47144702

47154703
// C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
47164704
return new (Context) UnaryExprOrTypeTraitExpr(
4717-
ExprKind, TInfo,
4718-
Context.getCGlobalCXXStdNSTypedef(
4719-
getLangOpts().CPlusPlus ? getStdNamespace() : nullptr, "size_t",
4720-
Context.getSizeType()),
4721-
OpLoc, R.getEnd());
4705+
ExprKind, TInfo, Context.getSizeType(), OpLoc, R.getEnd());
47224706
}
47234707

47244708
ExprResult
@@ -4761,11 +4745,7 @@ Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc,
47614745

47624746
// C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
47634747
return new (Context) UnaryExprOrTypeTraitExpr(
4764-
ExprKind, E,
4765-
Context.getCGlobalCXXStdNSTypedef(
4766-
getLangOpts().CPlusPlus ? getStdNamespace() : nullptr, "size_t",
4767-
Context.getSizeType()),
4768-
OpLoc, E->getSourceRange().getEnd());
4748+
ExprKind, E, Context.getSizeType(), OpLoc, E->getSourceRange().getEnd());
47694749
}
47704750

47714751
ExprResult
@@ -11375,9 +11355,7 @@ QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS,
1137511355

1137611356
if (CompLHSTy)
1137711357
*CompLHSTy = LHS.get()->getType();
11378-
return Context.getCGlobalCXXStdNSTypedef(
11379-
getLangOpts().CPlusPlus ? getStdNamespace() : nullptr, "ptrdiff_t",
11380-
Context.getPointerDiffType());
11358+
return Context.getPointerDiffType();
1138111359
}
1138211360
}
1138311361

clang/lib/StaticAnalyzer/Checkers/VLASizeChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ ProgramStateRef VLASizeChecker::checkVLA(CheckerContext &C,
9494

9595
ASTContext &Ctx = C.getASTContext();
9696
SValBuilder &SVB = C.getSValBuilder();
97-
CanQualType SizeTy = Ctx.getSizeType();
97+
CanQualType SizeTy = Ctx.getCanonicalType(Ctx.getSizeType());
9898
uint64_t SizeMax =
9999
SVB.getBasicValueFactory().getMaxValue(SizeTy)->getZExtValue();
100100

0 commit comments

Comments
 (0)