static bool constantFoldStringConcatenation(ApplyInst *AI, llvm::SetVector<SILInstruction *> &WorkList) { SILBuilder B(AI); // Try to apply the string literal concatenation optimization. auto *Concatenated = tryToConcatenateStrings(AI, B); // Bail if string literal concatenation could not be performed. if (!Concatenated) return false; // Replace all uses of the old instruction by a new instruction. AI->replaceAllUsesWith(Concatenated); auto RemoveCallback = [&](SILInstruction *DeadI) { WorkList.remove(DeadI); }; // Remove operands that are not used anymore. // Even if they are apply_inst, it is safe to // do so, because they can only be applies // of functions annotated as string.utf16 // or string.utf16. for (auto &Op : AI->getAllOperands()) { SILValue Val = Op.get(); Op.drop(); if (Val->use_empty()) { auto *DeadI = dyn_cast<SILInstruction>(Val); recursivelyDeleteTriviallyDeadInstructions(DeadI, /*force*/ true, RemoveCallback); WorkList.remove(DeadI); } } // Schedule users of the new instruction for constant folding. // We only need to schedule the string.concat invocations. for (auto AIUse : Concatenated->getUses()) { if (isApplyOfStringConcat(*AIUse->getUser())) { WorkList.insert(AIUse->getUser()); } } // Delete the old apply instruction. recursivelyDeleteTriviallyDeadInstructions(AI, /*force*/ true, RemoveCallback); return true; }