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:
Erik Eckstein
2017-04-17 10:57:42 -07:00
parent 9ac13ae606
commit 76eca003bb
5 changed files with 108 additions and 5 deletions

View File

@@ -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;