[clang] [clang][bytecode] Avoid classifying in visitArrayElemInit() (PR #139674)
via cfe-commits
cfe-commits at lists.llvm.org
Mon May 12 23:30:21 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
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().
---
Full diff: https://github.com/llvm/llvm-project/pull/139674.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+12-10)
- (modified) clang/lib/AST/ByteCode/Compiler.h (+2-1)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 51bfa8e548307..07e8712d41b64 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -570,11 +570,11 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
return false;
}
+ PrimType T = classifyPrim(SubExpr->getType());
// Init the complex value to {SubExpr, 0}.
- if (!this->visitArrayElemInit(0, SubExpr))
+ if (!this->visitArrayElemInit(0, SubExpr, T))
return false;
// Zero-init the second element.
- PrimType T = classifyPrim(SubExpr->getType());
if (!this->visitZeroInitializer(T, SubExpr->getType(), SubExpr))
return false;
return this->emitInitElem(T, 1, SubExpr);
@@ -772,7 +772,7 @@ bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
return false;
if (!this->emitInitElem(SubExprT, 0, SubExpr))
return false;
- return this->visitArrayElemInit(1, SubExpr);
+ return this->visitArrayElemInit(1, SubExpr, SubExprT);
}
template <class Emitter>
@@ -1886,6 +1886,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
if (!this->emitCheckArraySize(NumElems, E))
return false;
+ std::optional<PrimType> InitT = classify(CAT->getElementType());
unsigned ElementIndex = 0;
for (const Expr *Init : Inits) {
if (const auto *EmbedS =
@@ -1905,7 +1906,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
if (!EmbedS->doForEachDataElement(Eval, ElementIndex))
return false;
} else {
- if (!this->visitArrayElemInit(ElementIndex, Init))
+ if (!this->visitArrayElemInit(ElementIndex, Init, InitT))
return false;
++ElementIndex;
}
@@ -1915,7 +1916,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
// FIXME: This should go away.
if (ArrayFiller) {
for (; ElementIndex != NumElems; ++ElementIndex) {
- if (!this->visitArrayElemInit(ElementIndex, ArrayFiller))
+ if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
return false;
}
}
@@ -1998,13 +1999,13 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
/// Pointer to the array(not the element!) must be on the stack when calling
/// this.
template <class Emitter>
-bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex,
- const Expr *Init) {
- if (std::optional<PrimType> T = classify(Init->getType())) {
+bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
+ std::optional<PrimType> InitT) {
+ if (InitT) {
// Visit the primitive element like normal.
if (!this->visit(Init))
return false;
- return this->emitInitElem(*T, ElemIndex, Init);
+ return this->emitInitElem(*InitT, ElemIndex, Init);
}
InitLinkScope<Emitter> ILS(this, InitLink::Elem(ElemIndex));
@@ -2298,6 +2299,7 @@ bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
// Investigate compiling this to a loop.
const Expr *SubExpr = E->getSubExpr();
size_t Size = E->getArraySize().getZExtValue();
+ std::optional<PrimType> SubExprT = classify(SubExpr);
// So, every iteration, we execute an assignment here
// where the LHS is on the stack (the target array)
@@ -2306,7 +2308,7 @@ bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
ArrayIndexScope<Emitter> IndexScope(this, I);
BlockScope<Emitter> BS(this);
- if (!this->visitArrayElemInit(I, SubExpr))
+ if (!this->visitArrayElemInit(I, SubExpr, SubExprT))
return false;
if (!BS.destroyLocals())
return false;
diff --git a/clang/lib/AST/ByteCode/Compiler.h b/clang/lib/AST/ByteCode/Compiler.h
index 0febbac9c2043..ec5bd637453c5 100644
--- a/clang/lib/AST/ByteCode/Compiler.h
+++ b/clang/lib/AST/ByteCode/Compiler.h
@@ -302,7 +302,8 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
bool visitInitList(ArrayRef<const Expr *> Inits, const Expr *ArrayFiller,
const Expr *E);
- bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init);
+ bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
+ std::optional<PrimType> InitT);
/// Creates a local primitive value.
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst,
``````````
</details>
https://github.com/llvm/llvm-project/pull/139674
More information about the cfe-commits
mailing list