Skip to content

Commit 3de2fa9

Browse files
authored
[clang][bytecode] Avoid classifying in visitArrayElemInit() (#139674)
We usually call this more than once, but the type of the initializer never changes. Let's classify only once and pass that to visitArrayElemInit().
1 parent fd3fecf commit 3de2fa9

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -570,11 +570,11 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
570570
return false;
571571
}
572572

573+
PrimType T = classifyPrim(SubExpr->getType());
573574
// Init the complex value to {SubExpr, 0}.
574-
if (!this->visitArrayElemInit(0, SubExpr))
575+
if (!this->visitArrayElemInit(0, SubExpr, T))
575576
return false;
576577
// Zero-init the second element.
577-
PrimType T = classifyPrim(SubExpr->getType());
578578
if (!this->visitZeroInitializer(T, SubExpr->getType(), SubExpr))
579579
return false;
580580
return this->emitInitElem(T, 1, SubExpr);
@@ -772,7 +772,7 @@ bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
772772
return false;
773773
if (!this->emitInitElem(SubExprT, 0, SubExpr))
774774
return false;
775-
return this->visitArrayElemInit(1, SubExpr);
775+
return this->visitArrayElemInit(1, SubExpr, SubExprT);
776776
}
777777

778778
template <class Emitter>
@@ -1886,6 +1886,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
18861886
if (!this->emitCheckArraySize(NumElems, E))
18871887
return false;
18881888

1889+
std::optional<PrimType> InitT = classify(CAT->getElementType());
18891890
unsigned ElementIndex = 0;
18901891
for (const Expr *Init : Inits) {
18911892
if (const auto *EmbedS =
@@ -1905,7 +1906,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
19051906
if (!EmbedS->doForEachDataElement(Eval, ElementIndex))
19061907
return false;
19071908
} else {
1908-
if (!this->visitArrayElemInit(ElementIndex, Init))
1909+
if (!this->visitArrayElemInit(ElementIndex, Init, InitT))
19091910
return false;
19101911
++ElementIndex;
19111912
}
@@ -1915,7 +1916,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
19151916
// FIXME: This should go away.
19161917
if (ArrayFiller) {
19171918
for (; ElementIndex != NumElems; ++ElementIndex) {
1918-
if (!this->visitArrayElemInit(ElementIndex, ArrayFiller))
1919+
if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
19191920
return false;
19201921
}
19211922
}
@@ -1998,13 +1999,13 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
19981999
/// Pointer to the array(not the element!) must be on the stack when calling
19992000
/// this.
20002001
template <class Emitter>
2001-
bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex,
2002-
const Expr *Init) {
2003-
if (std::optional<PrimType> T = classify(Init->getType())) {
2002+
bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
2003+
std::optional<PrimType> InitT) {
2004+
if (InitT) {
20042005
// Visit the primitive element like normal.
20052006
if (!this->visit(Init))
20062007
return false;
2007-
return this->emitInitElem(*T, ElemIndex, Init);
2008+
return this->emitInitElem(*InitT, ElemIndex, Init);
20082009
}
20092010

20102011
InitLinkScope<Emitter> ILS(this, InitLink::Elem(ElemIndex));
@@ -2298,6 +2299,7 @@ bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
22982299
// Investigate compiling this to a loop.
22992300
const Expr *SubExpr = E->getSubExpr();
23002301
size_t Size = E->getArraySize().getZExtValue();
2302+
std::optional<PrimType> SubExprT = classify(SubExpr);
23012303

23022304
// So, every iteration, we execute an assignment here
23032305
// where the LHS is on the stack (the target array)
@@ -2306,7 +2308,7 @@ bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
23062308
ArrayIndexScope<Emitter> IndexScope(this, I);
23072309
BlockScope<Emitter> BS(this);
23082310

2309-
if (!this->visitArrayElemInit(I, SubExpr))
2311+
if (!this->visitArrayElemInit(I, SubExpr, SubExprT))
23102312
return false;
23112313
if (!BS.destroyLocals())
23122314
return false;

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
302302

303303
bool visitInitList(ArrayRef<const Expr *> Inits, const Expr *ArrayFiller,
304304
const Expr *E);
305-
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init);
305+
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
306+
std::optional<PrimType> InitT);
306307

307308
/// Creates a local primitive value.
308309
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst,

0 commit comments

Comments
 (0)