diff options
| author | Nikita Popov <npopov@redhat.com> | 2024-06-10 09:19:55 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-10 09:19:55 +0200 |
| commit | 12d24e0c56a154c88247e55c7c352030e4d9073d (patch) | |
| tree | 3594166dc0fad81c86f7ac8d41b2346f225d88f1 /clang/lib/CodeGen/CGExprAgg.cpp | |
| parent | a6929dbea8b4525ee41c1269e8e4a2b7182d9ce2 (diff) | |
| download | llvm-12d24e0c56a154c88247e55c7c352030e4d9073d.zip llvm-12d24e0c56a154c88247e55c7c352030e4d9073d.tar.gz llvm-12d24e0c56a154c88247e55c7c352030e4d9073d.tar.bz2 | |
[CodeGen] Simplify codegen for array initialization (#93956)
This makes codegen for array initialization simpler in two ways:
1. Drop the zero-index GEP at the start, which is no longer needed with
opaque pointers.
2. Emit GEPs directly to the correct element, instead of having a long
chain of +1 GEPs. This is more canonical, and also avoids regressions in
unoptimized builds from #93823.
Diffstat (limited to 'clang/lib/CodeGen/CGExprAgg.cpp')
| -rw-r--r-- | clang/lib/CodeGen/CGExprAgg.cpp | 29 |
1 files changed, 9 insertions, 20 deletions
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 5b2039a..b2a5cee 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -513,15 +513,6 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, QualType elementType = CGF.getContext().getAsArrayType(ArrayQTy)->getElementType(); - - // DestPtr is an array*. Construct an elementType* by drilling - // down a level. - llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0); - llvm::Value *indices[] = { zero, zero }; - llvm::Value *begin = Builder.CreateInBoundsGEP(DestPtr.getElementType(), - DestPtr.emitRawPointer(CGF), - indices, "arrayinit.begin"); - CharUnits elementSize = CGF.getContext().getTypeSizeInChars(elementType); CharUnits elementAlign = DestPtr.getAlignment().alignmentOfArrayElement(elementSize); @@ -562,6 +553,7 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, Address endOfInit = Address::invalid(); CodeGenFunction::CleanupDeactivationScope deactivation(CGF); + llvm::Value *begin = DestPtr.emitRawPointer(CGF); if (dtorKind) { CodeGenFunction::AllocaTrackerRAII allocaTracker(CGF); // In principle we could tell the cleanup where we are more @@ -585,19 +577,13 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1); - // The 'current element to initialize'. The invariants on this - // variable are complicated. Essentially, after each iteration of - // the loop, it points to the last initialized element, except - // that it points to the beginning of the array before any - // elements have been initialized. - llvm::Value *element = begin; - // Emit the explicit initializers. for (uint64_t i = 0; i != NumInitElements; ++i) { - // Advance to the next element. + llvm::Value *element = begin; if (i > 0) { - element = Builder.CreateInBoundsGEP( - llvmElementType, element, one, "arrayinit.element"); + element = Builder.CreateInBoundsGEP(llvmElementType, begin, + llvm::ConstantInt::get(CGF.SizeTy, i), + "arrayinit.element"); // Tell the cleanup that it needs to destroy up to this // element. TODO: some of these stores can be trivially @@ -624,9 +610,12 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, // do { *array++ = filler; } while (array != end); // Advance to the start of the rest of the array. + llvm::Value *element = begin; if (NumInitElements) { element = Builder.CreateInBoundsGEP( - llvmElementType, element, one, "arrayinit.start"); + llvmElementType, element, + llvm::ConstantInt::get(CGF.SizeTy, NumInitElements), + "arrayinit.start"); if (endOfInit.isValid()) Builder.CreateStore(element, endOfInit); } |
