mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
ArrayElementValuePropagation: explicitly reserve space for new elements when doing the array-content-of optimization
When Array.append(contentOf:) is replaced by individual Array.append(element) calls, an explicit reserveCapacityForAppend is inserted.
This commit is contained in:
@@ -299,15 +299,24 @@ public:
|
||||
DEBUG(llvm::dbgs() << "Array append contentsOf calls replaced in "
|
||||
<< Fn.getName() << " (" << Repls.size() << ")\n");
|
||||
|
||||
auto *AppendFnDecl = Ctx.getArrayAppendElementDecl();
|
||||
FuncDecl *AppendFnDecl = Ctx.getArrayAppendElementDecl();
|
||||
if (!AppendFnDecl)
|
||||
return false;
|
||||
|
||||
FuncDecl *ReserveFnDecl = Ctx.getArrayReserveCapacityDecl();
|
||||
if (!ReserveFnDecl)
|
||||
return false;
|
||||
|
||||
auto Mangled = SILDeclRef(AppendFnDecl, SILDeclRef::Kind::Func).mangle();
|
||||
auto *AppendFn = M.findFunction(Mangled, SILLinkage::PublicExternal);
|
||||
SILFunction *AppendFn = M.findFunction(Mangled, SILLinkage::PublicExternal);
|
||||
if (!AppendFn)
|
||||
return false;
|
||||
|
||||
Mangled = SILDeclRef(ReserveFnDecl, SILDeclRef::Kind::Func).mangle();
|
||||
SILFunction *ReserveFn = M.findFunction(Mangled, SILLinkage::PublicExternal);
|
||||
if (!ReserveFn)
|
||||
return false;
|
||||
|
||||
for (const ArrayAllocation::AppendContentOfReplacement &Repl : Repls) {
|
||||
ArraySemanticsCall AppendContentsOf(Repl.AppendContentOfCall);
|
||||
assert(AppendContentsOf && "Must be AppendContentsOf call");
|
||||
@@ -322,7 +331,7 @@ public:
|
||||
SmallVector<Substitution, 4> Subs;
|
||||
Sig->getSubstitutions(ArraySubMap, Subs);
|
||||
|
||||
AppendContentsOf.replaceByAppendingValues(M, AppendFn,
|
||||
AppendContentsOf.replaceByAppendingValues(M, AppendFn, ReserveFn,
|
||||
Repl.ReplacementValues, Subs);
|
||||
}
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user