|
| 1 | +//===-- GenericSpecializer.cpp - Specialization of generic functions ------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Specialize calls to generic functions by substituting static type |
| 14 | +// information. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#define DEBUG_TYPE "sil-generic-specialize" |
| 19 | + |
| 20 | +#include "swift/SIL/SILFunction.h" |
| 21 | +#include "swift/SIL/SILInstruction.h" |
| 22 | +#include "swift/SILOptimizer/Utils/Generics.h" |
| 23 | +#include "swift/SILOptimizer/Utils/Local.h" |
| 24 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 25 | +#include "llvm/ADT/SmallVector.h" |
| 26 | + |
| 27 | +using namespace swift; |
| 28 | + |
| 29 | +// STATISTIC(NumEscapingAllocas, "Number of aggregate allocas not chopped up " |
| 30 | +// "due to uses."); |
| 31 | +// STATISTIC(NumChoppedAllocas, "Number of chopped up aggregate allocas."); |
| 32 | +// STATISTIC(NumUnhandledAllocas, "Number of non struct, tuple allocas."); |
| 33 | + |
| 34 | +namespace {} // end anonymous namespace |
| 35 | + |
| 36 | +namespace { |
| 37 | + |
| 38 | +class GenericSpecializer : public SILFunctionTransform { |
| 39 | + |
| 40 | + bool specializeAppliesInFunction(SILFunction &F); |
| 41 | + |
| 42 | + /// The entry point to the transformation. |
| 43 | + void run() override { |
| 44 | + SILFunction &F = *getFunction(); |
| 45 | + DEBUG(llvm::dbgs() << "***** GenericSpecializer on function:" << F.getName() |
| 46 | + << " *****\n"); |
| 47 | + |
| 48 | + if (specializeAppliesInFunction(F)) |
| 49 | + invalidateAnalysis(SILAnalysis::InvalidationKind::FunctionBody); |
| 50 | + } |
| 51 | + |
| 52 | + StringRef getName() override { return "Generic Specializer"; } |
| 53 | +}; |
| 54 | + |
| 55 | +} // end anonymous namespace |
| 56 | + |
| 57 | +bool GenericSpecializer::specializeAppliesInFunction(SILFunction &F) { |
| 58 | + bool Changed = false; |
| 59 | + llvm::SmallVector<SILInstruction *, 8> DeadApplies; |
| 60 | + |
| 61 | + for (auto &BB : F) { |
| 62 | + for (auto It = BB.begin(), End = BB.end(); It != End;) { |
| 63 | + auto &I = *It++; |
| 64 | + |
| 65 | + // Skip non-apply instructions, apply instructions with no |
| 66 | + // substitutions, apply instructions where we do not statically |
| 67 | + // know the called function, and apply instructions where we do |
| 68 | + // not have the body of the called function. |
| 69 | + |
| 70 | + ApplySite Apply = ApplySite::isa(&I); |
| 71 | + if (!Apply || !Apply.hasSubstitutions()) |
| 72 | + continue; |
| 73 | + |
| 74 | + auto *Callee = Apply.getCalleeFunction(); |
| 75 | + if (!Callee || !Callee->isDefinition()) |
| 76 | + continue; |
| 77 | + |
| 78 | + // We have a call that can potentially be specialized, so |
| 79 | + // attempt to do so. |
| 80 | + |
| 81 | + // The specializer helper function currently expects a collector |
| 82 | + // argument, but we aren't going to make use of the results so |
| 83 | + // we'll have our filter always return false; |
| 84 | + auto Filter = [](SILInstruction *I) -> bool { return false; }; |
| 85 | + CloneCollector Collector(Filter); |
| 86 | + |
| 87 | + SILFunction *SpecializedFunction; |
| 88 | + |
| 89 | + auto Specialized = |
| 90 | + trySpecializeApplyOfGeneric(Apply, SpecializedFunction, Collector); |
| 91 | + |
| 92 | + if (Specialized) { |
| 93 | + Changed = true; |
| 94 | + |
| 95 | + // If calling the specialization utility resulted in a new |
| 96 | + // function (as opposed to returning a previous |
| 97 | + // specialization), we need to notify the pass manager so that |
| 98 | + // the new function gets optimized. |
| 99 | + if (SpecializedFunction) |
| 100 | + notifyPassManagerOfFunction(SpecializedFunction); |
| 101 | + |
| 102 | + auto *AI = Apply.getInstruction(); |
| 103 | + |
| 104 | + if (!isa<TryApplyInst>(AI)) |
| 105 | + AI->replaceAllUsesWith(Specialized.getInstruction()); |
| 106 | + |
| 107 | + DeadApplies.push_back(AI); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Remove all the now-dead applies. |
| 113 | + while (!DeadApplies.empty()) { |
| 114 | + auto *AI = DeadApplies.pop_back_val(); |
| 115 | + recursivelyDeleteTriviallyDeadInstructions(AI, true); |
| 116 | + } |
| 117 | + |
| 118 | + return Changed; |
| 119 | +} |
| 120 | + |
| 121 | +SILTransform *swift::createGenericSpecializer() { |
| 122 | + return new GenericSpecializer(); |
| 123 | +} |
0 commit comments