Skip to content

Commit c097cff

Browse files
committed
[clang][bytecode] Use ExtendingDecl mechanism for primitives as well
... when creating the temporary variables for a MaterializeTemporaryExpr.
1 parent 31abb20 commit c097cff

File tree

3 files changed

+319
-43
lines changed

3 files changed

+319
-43
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
567567
// Location for the SubExpr.
568568
// Since SubExpr is of complex type, visiting it results in a pointer
569569
// anyway, so we just create a temporary pointer variable.
570-
unsigned SubExprOffset = allocateLocalPrimitive(
571-
SubExpr, PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
570+
unsigned SubExprOffset =
571+
allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
572572
if (!this->visit(SubExpr))
573573
return false;
574574
if (!this->emitSetLocal(PT_Ptr, SubExprOffset, CE))
@@ -611,8 +611,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
611611

612612
const auto *VT = CE->getType()->getAs<VectorType>();
613613
PrimType ElemT = classifyPrim(SubExpr->getType());
614-
unsigned ElemOffset = allocateLocalPrimitive(
615-
SubExpr, ElemT, /*IsConst=*/true, /*IsExtended=*/false);
614+
unsigned ElemOffset =
615+
allocateLocalPrimitive(SubExpr, ElemT, /*IsConst=*/true);
616616

617617
// Prepare a local variable for the scalar value.
618618
if (!this->visit(SubExpr))
@@ -1104,7 +1104,7 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
11041104
PrimType ResultElemT = this->classifyComplexElementType(E->getType());
11051105
unsigned ResultOffset = ~0u;
11061106
if (!DiscardResult)
1107-
ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, true, false);
1107+
ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, /*IsConst=*/true);
11081108

11091109
// Save result pointer in ResultOffset
11101110
if (!this->DiscardResult) {
@@ -1178,14 +1178,14 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
11781178

11791179
// Evaluate LHS and save value to LHSOffset.
11801180
if (LHSType->isAnyComplexType()) {
1181-
LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, true, false);
1181+
LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
11821182
if (!this->visit(LHS))
11831183
return false;
11841184
if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
11851185
return false;
11861186
} else {
11871187
PrimType LHST = classifyPrim(LHSType);
1188-
LHSOffset = this->allocateLocalPrimitive(LHS, LHST, true, false);
1188+
LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
11891189
if (!this->visit(LHS))
11901190
return false;
11911191
if (!this->emitSetLocal(LHST, LHSOffset, E))
@@ -1195,14 +1195,14 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
11951195
// Same with RHS.
11961196
unsigned RHSOffset;
11971197
if (RHSType->isAnyComplexType()) {
1198-
RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, true, false);
1198+
RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
11991199
if (!this->visit(RHS))
12001200
return false;
12011201
if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
12021202
return false;
12031203
} else {
12041204
PrimType RHST = classifyPrim(RHSType);
1205-
RHSOffset = this->allocateLocalPrimitive(RHS, RHST, true, false);
1205+
RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
12061206
if (!this->visit(RHS))
12071207
return false;
12081208
if (!this->emitSetLocal(RHST, RHSOffset, E))
@@ -1342,14 +1342,16 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
13421342
PrimType ResultElemT = this->classifyVectorElementType(E->getType());
13431343

13441344
// Evaluate LHS and save value to LHSOffset.
1345-
unsigned LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, true, false);
1345+
unsigned LHSOffset =
1346+
this->allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
13461347
if (!this->visit(LHS))
13471348
return false;
13481349
if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
13491350
return false;
13501351

13511352
// Evaluate RHS and save value to RHSOffset.
1352-
unsigned RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, true, false);
1353+
unsigned RHSOffset =
1354+
this->allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
13531355
if (!this->visit(RHS))
13541356
return false;
13551357
if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
@@ -2710,8 +2712,8 @@ bool Compiler<Emitter>::VisitMaterializeTemporaryExpr(
27102712
// For everyhing else, use local variables.
27112713
if (SubExprT) {
27122714
bool IsConst = SubExpr->getType().isConstQualified();
2713-
unsigned LocalIndex = allocateLocalPrimitive(E, *SubExprT, IsConst,
2714-
/*IsExtended=*/true);
2715+
unsigned LocalIndex =
2716+
allocateLocalPrimitive(E, *SubExprT, IsConst, E->getExtendingDecl());
27152717
if (!this->visit(SubExpr))
27162718
return false;
27172719
if (!this->emitSetLocal(*SubExprT, LocalIndex, E))
@@ -2781,7 +2783,7 @@ bool Compiler<Emitter>::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
27812783
unsigned LocalIndex;
27822784

27832785
if (T)
2784-
LocalIndex = this->allocateLocalPrimitive(Init, *T, false, false);
2786+
LocalIndex = this->allocateLocalPrimitive(Init, *T, /*IsConst=*/false);
27852787
else if (std::optional<unsigned> MaybeIndex = this->allocateLocal(Init))
27862788
LocalIndex = *MaybeIndex;
27872789
else
@@ -3337,8 +3339,8 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
33373339
PrimType SizeT = classifyPrim(Stripped->getType());
33383340

33393341
// Save evaluated array size to a variable.
3340-
unsigned ArrayLen = allocateLocalPrimitive(
3341-
Stripped, SizeT, /*IsConst=*/false, /*IsExtended=*/false);
3342+
unsigned ArrayLen =
3343+
allocateLocalPrimitive(Stripped, SizeT, /*IsConst=*/false);
33423344
if (!this->visit(Stripped))
33433345
return false;
33443346
if (!this->emitSetLocal(SizeT, ArrayLen, E))
@@ -3416,8 +3418,8 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
34163418
}
34173419

34183420
// Create loop variables.
3419-
unsigned Iter = allocateLocalPrimitive(
3420-
Stripped, SizeT, /*IsConst=*/false, /*IsExtended=*/false);
3421+
unsigned Iter =
3422+
allocateLocalPrimitive(Stripped, SizeT, /*IsConst=*/false);
34213423
if (!this->emitConst(StaticInitElems, SizeT, E))
34223424
return false;
34233425
if (!this->emitSetLocal(SizeT, Iter, E))
@@ -3668,8 +3670,8 @@ template <class Emitter>
36683670
bool Compiler<Emitter>::VisitAddrLabelExpr(const AddrLabelExpr *E) {
36693671
assert(E->getType()->isVoidPointerType());
36703672

3671-
unsigned Offset = allocateLocalPrimitive(
3672-
E->getLabel(), PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
3673+
unsigned Offset =
3674+
allocateLocalPrimitive(E->getLabel(), PT_Ptr, /*IsConst=*/true);
36733675

36743676
return this->emitGetLocal(PT_Ptr, Offset, E);
36753677
}
@@ -3684,7 +3686,8 @@ bool Compiler<Emitter>::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
36843686
QualType SrcType = Src->getType();
36853687
PrimType SrcElemT = classifyVectorElementType(SrcType);
36863688

3687-
unsigned SrcOffset = this->allocateLocalPrimitive(Src, PT_Ptr, true, false);
3689+
unsigned SrcOffset =
3690+
this->allocateLocalPrimitive(Src, PT_Ptr, /*IsConst=*/true);
36883691
if (!this->visit(Src))
36893692
return false;
36903693
if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
@@ -3727,8 +3730,8 @@ bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
37273730
// Save both input vectors to a local variable.
37283731
unsigned VectorOffsets[2];
37293732
for (unsigned I = 0; I != 2; ++I) {
3730-
VectorOffsets[I] = this->allocateLocalPrimitive(
3731-
Vecs[I], PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
3733+
VectorOffsets[I] =
3734+
this->allocateLocalPrimitive(Vecs[I], PT_Ptr, /*IsConst=*/true);
37323735
if (!this->visit(Vecs[I]))
37333736
return false;
37343737
if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E))
@@ -3780,8 +3783,7 @@ bool Compiler<Emitter>::VisitExtVectorElementExpr(
37803783
}
37813784

37823785
// Create a local variable for the base.
3783-
unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true,
3784-
/*IsExtended=*/false);
3786+
unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true);
37853787
if (!this->visit(Base))
37863788
return false;
37873789
if (!this->emitSetLocal(PT_Ptr, BaseOffset, E))
@@ -4193,9 +4195,8 @@ bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
41934195
}
41944196

41954197
template <class Emitter>
4196-
unsigned Compiler<Emitter>::allocateLocalPrimitive(DeclTy &&Src, PrimType Ty,
4197-
bool IsConst,
4198-
bool IsExtended) {
4198+
unsigned Compiler<Emitter>::allocateLocalPrimitive(
4199+
DeclTy &&Src, PrimType Ty, bool IsConst, const ValueDecl *ExtendingDecl) {
41994200
// Make sure we don't accidentally register the same decl twice.
42004201
if (const auto *VD =
42014202
dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
@@ -4212,7 +4213,10 @@ unsigned Compiler<Emitter>::allocateLocalPrimitive(DeclTy &&Src, PrimType Ty,
42124213
Scope::Local Local = this->createLocal(D);
42134214
if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
42144215
Locals.insert({VD, Local});
4215-
VarScope->add(Local, IsExtended);
4216+
if (ExtendingDecl)
4217+
VarScope->addExtended(Local, ExtendingDecl);
4218+
else
4219+
VarScope->add(Local, false);
42164220
return Local.Offset;
42174221
}
42184222

@@ -4780,7 +4784,7 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
47804784
// decl as the function pointer.
47814785
const Expr *Callee = E->getCallee();
47824786
CalleeOffset =
4783-
this->allocateLocalPrimitive(Callee, PT_MemberPtr, true, false);
4787+
this->allocateLocalPrimitive(Callee, PT_MemberPtr, /*IsConst=*/true);
47844788
if (!this->visit(Callee))
47854789
return false;
47864790
if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
@@ -4802,7 +4806,8 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
48024806
return this->emitKill(E);
48034807
} else if (!FuncDecl) {
48044808
const Expr *Callee = E->getCallee();
4805-
CalleeOffset = this->allocateLocalPrimitive(Callee, PT_FnPtr, true, false);
4809+
CalleeOffset =
4810+
this->allocateLocalPrimitive(Callee, PT_FnPtr, /*IsConst=*/true);
48064811
if (!this->visit(Callee))
48074812
return false;
48084813
if (!this->emitSetLocal(PT_FnPtr, *CalleeOffset, E))
@@ -5404,7 +5409,8 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
54045409

54055410
LabelTy EndLabel = this->getLabel();
54065411
OptLabelTy DefaultLabel = std::nullopt;
5407-
unsigned CondVar = this->allocateLocalPrimitive(Cond, CondT, true, false);
5412+
unsigned CondVar =
5413+
this->allocateLocalPrimitive(Cond, CondT, /*IsConst=*/true);
54085414

54095415
if (const auto *CondInit = S->getInit())
54105416
if (!visitStmt(CondInit))
@@ -6067,7 +6073,8 @@ bool Compiler<Emitter>::VisitComplexUnaryOperator(const UnaryOperator *E) {
60676073
// The offset of the temporary, if we created one.
60686074
unsigned SubExprOffset = ~0u;
60696075
auto createTemp = [=, &SubExprOffset]() -> bool {
6070-
SubExprOffset = this->allocateLocalPrimitive(SubExpr, PT_Ptr, true, false);
6076+
SubExprOffset =
6077+
this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
60716078
if (!this->visit(SubExpr))
60726079
return false;
60736080
return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
@@ -6181,7 +6188,7 @@ bool Compiler<Emitter>::VisitVectorUnaryOperator(const UnaryOperator *E) {
61816188

61826189
// The offset of the temporary, if we created one.
61836190
unsigned SubExprOffset =
6184-
this->allocateLocalPrimitive(SubExpr, PT_Ptr, true, false);
6191+
this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
61856192
if (!this->visit(SubExpr))
61866193
return false;
61876194
if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
@@ -6554,16 +6561,15 @@ bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
65546561
if (LHS->getType()->isAnyComplexType()) {
65556562
LHSIsComplex = true;
65566563
ElemT = classifyComplexElementType(LHS->getType());
6557-
LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true,
6558-
/*IsExtended=*/false);
6564+
LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
65596565
if (!this->visit(LHS))
65606566
return false;
65616567
if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
65626568
return false;
65636569
} else {
65646570
LHSIsComplex = false;
65656571
PrimType LHST = classifyPrim(LHS->getType());
6566-
LHSOffset = this->allocateLocalPrimitive(LHS, LHST, true, false);
6572+
LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
65676573
if (!this->visit(LHS))
65686574
return false;
65696575
if (!this->emitSetLocal(LHST, LHSOffset, E))
@@ -6575,16 +6581,15 @@ bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
65756581
if (RHS->getType()->isAnyComplexType()) {
65766582
RHSIsComplex = true;
65776583
ElemT = classifyComplexElementType(RHS->getType());
6578-
RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true,
6579-
/*IsExtended=*/false);
6584+
RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
65806585
if (!this->visit(RHS))
65816586
return false;
65826587
if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
65836588
return false;
65846589
} else {
65856590
RHSIsComplex = false;
65866591
PrimType RHST = classifyPrim(RHS->getType());
6587-
RHSOffset = this->allocateLocalPrimitive(RHS, RHST, true, false);
6592+
RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
65886593
if (!this->visit(RHS))
65896594
return false;
65906595
if (!this->emitSetLocal(RHST, RHSOffset, E))
@@ -6761,8 +6766,8 @@ bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
67616766
if (!this->visit(SubExpr))
67626767
return false;
67636768
} else if (std::optional<PrimType> FromT = classify(SubExpr)) {
6764-
unsigned TempOffset = allocateLocalPrimitive(
6765-
SubExpr, *FromT, /*IsConst=*/true, /*IsExtended=*/false);
6769+
unsigned TempOffset =
6770+
allocateLocalPrimitive(SubExpr, *FromT, /*IsConst=*/true);
67666771
if (!this->visit(SubExpr))
67676772
return false;
67686773
if (!this->emitSetLocal(*FromT, TempOffset, E))

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
303303

304304
/// Creates a local primitive value.
305305
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst,
306-
bool IsExtended = false);
306+
const ValueDecl *ExtendingDecl = nullptr);
307307

308308
/// Allocates a space storing a local given its type.
309309
std::optional<unsigned>

0 commit comments

Comments
 (0)