Skip to content

Commit dc33c5a

Browse files
committed
Try more optimizations
1 parent fb437b0 commit dc33c5a

File tree

4 files changed

+46
-75
lines changed

4 files changed

+46
-75
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,26 +2915,31 @@ class CallExpr : public Expr {
29152915
// instead of re-computing the offset each time the trailing objects are
29162916
// accessed.
29172917

2918+
protected:
2919+
static constexpr unsigned offsetToTrailingObjects = 32;
2920+
2921+
template <typename T>
2922+
static constexpr unsigned sizeToAllocateForCallExprSubclass(unsigned SizeOfTrailingObjects) {
2923+
static_assert(sizeof(T) <= CallExpr::offsetToTrailingObjects);
2924+
return SizeOfTrailingObjects + CallExpr::offsetToTrailingObjects;
2925+
}
2926+
private:
29182927
/// Return a pointer to the start of the trailing array of "Stmt *".
29192928
Stmt **getTrailingStmts() {
29202929
return reinterpret_cast<Stmt **>(reinterpret_cast<char *>(this) +
2921-
CallExprBits.OffsetToTrailingObjects);
2930+
offsetToTrailingObjects);
29222931
}
29232932
Stmt *const *getTrailingStmts() const {
29242933
return const_cast<CallExpr *>(this)->getTrailingStmts();
29252934
}
29262935

2927-
/// Map a statement class to the appropriate offset in bytes from the
2928-
/// this pointer to the trailing objects.
2929-
static unsigned offsetToTrailingObjects(StmtClass SC);
2930-
29312936
unsigned getSizeOfTrailingStmts() const {
29322937
return (1 + getNumPreArgs() + getNumArgs()) * sizeof(Stmt *);
29332938
}
29342939

29352940
size_t getOffsetOfTrailingFPFeatures() const {
29362941
assert(hasStoredFPFeatures());
2937-
return CallExprBits.OffsetToTrailingObjects + getSizeOfTrailingStmts();
2942+
return offsetToTrailingObjects + getSizeOfTrailingStmts();
29382943
}
29392944

29402945
public:
@@ -2981,14 +2986,14 @@ class CallExpr : public Expr {
29812986
FPOptionsOverride *getTrailingFPFeatures() {
29822987
assert(hasStoredFPFeatures());
29832988
return reinterpret_cast<FPOptionsOverride *>(
2984-
reinterpret_cast<char *>(this) + CallExprBits.OffsetToTrailingObjects +
2989+
reinterpret_cast<char *>(this) + offsetToTrailingObjects +
29852990
getSizeOfTrailingStmts());
29862991
}
29872992
const FPOptionsOverride *getTrailingFPFeatures() const {
29882993
assert(hasStoredFPFeatures());
29892994
return reinterpret_cast<const FPOptionsOverride *>(
29902995
reinterpret_cast<const char *>(this) +
2991-
CallExprBits.OffsetToTrailingObjects + getSizeOfTrailingStmts());
2996+
offsetToTrailingObjects + getSizeOfTrailingStmts());
29922997
}
29932998

29942999
public:
@@ -3196,7 +3201,26 @@ class CallExpr : public Expr {
31963201
SourceLocation getRParenLoc() const { return RParenLoc; }
31973202
void setRParenLoc(SourceLocation L) { RParenLoc = L; }
31983203

3199-
SourceLocation getBeginLoc() const LLVM_READONLY;
3204+
SourceLocation getBeginLoc() const {
3205+
//if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(this))
3206+
// return OCE->getBeginLoc();
3207+
3208+
// A non-dependent call to a member function with an explicit object parameter
3209+
// is modelled with the object expression being the first argument, e.g. in
3210+
// `o.f(x)`, the callee will be just `f`, and `o` will be the first argument.
3211+
// Since the first argument is written before the callee, the expression's
3212+
// begin location should come from the first argument.
3213+
// This does not apply to dependent calls, which are modelled with `o.f`
3214+
// being the callee.
3215+
// Because this check is expennsive, we cache the result.
3216+
if (usesMemberSyntax()) {
3217+
if (auto FirstArgLoc = getArg(0)->getBeginLoc(); FirstArgLoc.isValid()) {
3218+
return FirstArgLoc;
3219+
}
3220+
}
3221+
return getCallee()->getBeginLoc();
3222+
}
3223+
32003224
SourceLocation getEndLoc() const LLVM_READONLY;
32013225

32023226
/// Return true if this is a call to __assume() or __builtin_assume() with
@@ -3229,8 +3253,6 @@ class CallExpr : public Expr {
32293253
}
32303254
};
32313255

3232-
static_assert(sizeof(CallExpr) == 24);
3233-
32343256
/// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F.
32353257
///
32363258
class MemberExpr final

clang/include/clang/AST/Stmt.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,9 @@ class alignas(void *) Stmt {
568568

569569
LLVM_PREFERRED_TYPE(bool)
570570
unsigned ExplicitObjectMemFunUsingMemberSyntax : 1;
571-
572-
/// The offset in bytes from the this pointer to the start of the
573-
/// trailing objects belonging to CallExpr. Intentionally byte sized
574-
/// for faster access.
575-
unsigned OffsetToTrailingObjects : 8;
576571
};
577-
enum { NumCallExprBits = 32 };
572+
573+
enum { NumCallExprBits = 24 };
578574

579575
class MemberExprBitfields {
580576
friend class ASTStmtReader;

clang/lib/AST/Expr.cpp

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,11 +1460,6 @@ CallExpr::CallExpr(StmtClass SC, Expr *Fn, ArrayRef<Expr *> PreArgs,
14601460
CallExprBits.NumPreArgs = NumPreArgs;
14611461
assert((NumPreArgs == getNumPreArgs()) && "NumPreArgs overflow!");
14621462

1463-
unsigned OffsetToTrailingObjects = offsetToTrailingObjects(SC);
1464-
CallExprBits.OffsetToTrailingObjects = OffsetToTrailingObjects;
1465-
assert((CallExprBits.OffsetToTrailingObjects == OffsetToTrailingObjects) &&
1466-
"OffsetToTrailingObjects overflow!");
1467-
14681463
CallExprBits.UsesADL = static_cast<bool>(UsesADL);
14691464

14701465
setCallee(Fn);
@@ -1490,11 +1485,6 @@ CallExpr::CallExpr(StmtClass SC, unsigned NumPreArgs, unsigned NumArgs,
14901485
: Expr(SC, Empty), NumArgs(NumArgs) {
14911486
CallExprBits.NumPreArgs = NumPreArgs;
14921487
assert((NumPreArgs == getNumPreArgs()) && "NumPreArgs overflow!");
1493-
1494-
unsigned OffsetToTrailingObjects = offsetToTrailingObjects(SC);
1495-
CallExprBits.OffsetToTrailingObjects = OffsetToTrailingObjects;
1496-
assert((CallExprBits.OffsetToTrailingObjects == OffsetToTrailingObjects) &&
1497-
"OffsetToTrailingObjects overflow!");
14981488
CallExprBits.HasFPFeatures = HasFPFeatures;
14991489
CallExprBits.IsCoroElideSafe = false;
15001490
}
@@ -1508,7 +1498,8 @@ CallExpr *CallExpr::Create(const ASTContext &Ctx, Expr *Fn,
15081498
unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
15091499
/*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
15101500
void *Mem =
1511-
Ctx.Allocate(sizeof(CallExpr) + SizeOfTrailingObjects, alignof(CallExpr));
1501+
Ctx.Allocate(sizeToAllocateForCallExprSubclass<CallExpr>(SizeOfTrailingObjects),
1502+
alignof(CallExpr));
15121503
return new (Mem) CallExpr(CallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
15131504
RParenLoc, FPFeatures, MinNumArgs, UsesADL);
15141505
}
@@ -1518,28 +1509,11 @@ CallExpr *CallExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs,
15181509
unsigned SizeOfTrailingObjects =
15191510
CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures);
15201511
void *Mem =
1521-
Ctx.Allocate(sizeof(CallExpr) + SizeOfTrailingObjects, alignof(CallExpr));
1512+
Ctx.Allocate(sizeToAllocateForCallExprSubclass<CallExpr>(SizeOfTrailingObjects), alignof(CallExpr));
15221513
return new (Mem)
15231514
CallExpr(CallExprClass, /*NumPreArgs=*/0, NumArgs, HasFPFeatures, Empty);
15241515
}
15251516

1526-
unsigned CallExpr::offsetToTrailingObjects(StmtClass SC) {
1527-
switch (SC) {
1528-
case CallExprClass:
1529-
return sizeof(CallExpr);
1530-
case CXXOperatorCallExprClass:
1531-
return sizeof(CXXOperatorCallExpr);
1532-
case CXXMemberCallExprClass:
1533-
return sizeof(CXXMemberCallExpr);
1534-
case UserDefinedLiteralClass:
1535-
return sizeof(UserDefinedLiteral);
1536-
case CUDAKernelCallExprClass:
1537-
return sizeof(CUDAKernelCallExpr);
1538-
default:
1539-
llvm_unreachable("unexpected class deriving from CallExpr!");
1540-
}
1541-
}
1542-
15431517
Decl *Expr::getReferencedDeclOfCallee() {
15441518

15451519
// Optimize for the common case first
@@ -1647,27 +1621,6 @@ CallExpr::getUnusedResultAttr(const ASTContext &Ctx) const {
16471621
return {nullptr, nullptr};
16481622
}
16491623

1650-
SourceLocation CallExpr::getBeginLoc() const {
1651-
if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(this))
1652-
return OCE->getBeginLoc();
1653-
1654-
// A non-dependent call to a member function with an explicit object parameter
1655-
// is modelled with the object expression being the first argument, e.g. in
1656-
// `o.f(x)`, the callee will be just `f`, and `o` will be the first argument.
1657-
// Since the first argument is written before the callee, the expression's
1658-
// begin location should come from the first argument.
1659-
// This does not apply to dependent calls, which are modelled with `o.f`
1660-
// being the callee.
1661-
// Because this check is expennsive, we cache the result.
1662-
if (usesMemberSyntax()) {
1663-
if (auto FirstArgLoc = getArg(0)->getBeginLoc(); FirstArgLoc.isValid()) {
1664-
return FirstArgLoc;
1665-
}
1666-
}
1667-
1668-
return getCallee()->getBeginLoc();
1669-
}
1670-
16711624
SourceLocation CallExpr::getEndLoc() const {
16721625
if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(this))
16731626
return OCE->getEndLoc();

clang/lib/AST/ExprCXX.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ CXXOperatorCallExpr::Create(const ASTContext &Ctx,
619619
unsigned NumArgs = Args.size();
620620
unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
621621
/*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
622-
void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
622+
void *Mem = Ctx.Allocate(sizeToAllocateForCallExprSubclass<CXXOperatorCallExpr>(SizeOfTrailingObjects),
623623
alignof(CXXOperatorCallExpr));
624624
return new (Mem) CXXOperatorCallExpr(OpKind, Fn, Args, Ty, VK, OperatorLoc,
625625
FPFeatures, UsesADL);
@@ -632,7 +632,7 @@ CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx,
632632
// Allocate storage for the trailing objects of CallExpr.
633633
unsigned SizeOfTrailingObjects =
634634
CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures);
635-
void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
635+
void *Mem = Ctx.Allocate(sizeToAllocateForCallExprSubclass<CXXOperatorCallExpr>(SizeOfTrailingObjects),
636636
alignof(CXXOperatorCallExpr));
637637
return new (Mem) CXXOperatorCallExpr(NumArgs, HasFPFeatures, Empty);
638638
}
@@ -684,7 +684,7 @@ CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn,
684684
unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
685685
unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
686686
/*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
687-
void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
687+
void *Mem = Ctx.Allocate(sizeToAllocateForCallExprSubclass<CXXMemberCallExpr>(SizeOfTrailingObjects),
688688
alignof(CXXMemberCallExpr));
689689
return new (Mem)
690690
CXXMemberCallExpr(Fn, Args, Ty, VK, RP, FPFeatures, MinNumArgs);
@@ -697,7 +697,7 @@ CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx,
697697
// Allocate storage for the trailing objects of CallExpr.
698698
unsigned SizeOfTrailingObjects =
699699
CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures);
700-
void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
700+
void *Mem = Ctx.Allocate(sizeToAllocateForCallExprSubclass<CXXMemberCallExpr>(SizeOfTrailingObjects),
701701
alignof(CXXMemberCallExpr));
702702
return new (Mem) CXXMemberCallExpr(NumArgs, HasFPFeatures, Empty);
703703
}
@@ -958,7 +958,7 @@ UserDefinedLiteral *UserDefinedLiteral::Create(const ASTContext &Ctx, Expr *Fn,
958958
unsigned NumArgs = Args.size();
959959
unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
960960
/*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
961-
void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
961+
void *Mem = Ctx.Allocate(sizeToAllocateForCallExprSubclass<UserDefinedLiteral>(SizeOfTrailingObjects),
962962
alignof(UserDefinedLiteral));
963963
return new (Mem)
964964
UserDefinedLiteral(Fn, Args, Ty, VK, LitEndLoc, SuffixLoc, FPFeatures);
@@ -971,7 +971,7 @@ UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx,
971971
// Allocate storage for the trailing objects of CallExpr.
972972
unsigned SizeOfTrailingObjects =
973973
CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPOptions);
974-
void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
974+
void *Mem = Ctx.Allocate(sizeToAllocateForCallExprSubclass<UserDefinedLiteral>(SizeOfTrailingObjects),
975975
alignof(UserDefinedLiteral));
976976
return new (Mem) UserDefinedLiteral(NumArgs, HasFPOptions, Empty);
977977
}
@@ -1946,7 +1946,7 @@ CUDAKernelCallExpr::Create(const ASTContext &Ctx, Expr *Fn, CallExpr *Config,
19461946
unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
19471947
unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
19481948
/*NumPreArgs=*/END_PREARG, NumArgs, FPFeatures.requiresTrailingStorage());
1949-
void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1949+
void *Mem = Ctx.Allocate(sizeToAllocateForCallExprSubclass<CUDAKernelCallExpr>(SizeOfTrailingObjects),
19501950
alignof(CUDAKernelCallExpr));
19511951
return new (Mem)
19521952
CUDAKernelCallExpr(Fn, Config, Args, Ty, VK, RP, FPFeatures, MinNumArgs);
@@ -1959,7 +1959,7 @@ CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx,
19591959
// Allocate storage for the trailing objects of CallExpr.
19601960
unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
19611961
/*NumPreArgs=*/END_PREARG, NumArgs, HasFPFeatures);
1962-
void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1962+
void *Mem = Ctx.Allocate(sizeToAllocateForCallExprSubclass<CUDAKernelCallExpr>(SizeOfTrailingObjects),
19631963
alignof(CUDAKernelCallExpr));
19641964
return new (Mem) CUDAKernelCallExpr(NumArgs, HasFPFeatures, Empty);
19651965
}

0 commit comments

Comments
 (0)