|
| 1 | +//===--- ArgumentExplosionTransform.cpp -----------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#define DEBUG_TYPE "fso-argument-explosion-transform" |
| 14 | +#include "FunctionSignatureOpts.h" |
| 15 | +#include "llvm/Support/CommandLine.h" |
| 16 | + |
| 17 | +using namespace swift; |
| 18 | + |
| 19 | +static llvm::cl::opt<bool> FSODisableArgExplosion( |
| 20 | + "sil-fso-disable-arg-explosion", |
| 21 | + llvm::cl::desc("Do not perform argument explosion during FSO. Intended " |
| 22 | + "only for testing purposes")); |
| 23 | + |
| 24 | +bool FunctionSignatureTransform::ArgumentExplosionAnalyzeParameters() { |
| 25 | + // If we are not supposed to perform argument explosion, bail. |
| 26 | + if (FSODisableArgExplosion) |
| 27 | + return false; |
| 28 | + |
| 29 | + SILFunction *F = TransformDescriptor.OriginalFunction; |
| 30 | + // Did we decide we should optimize any parameter? |
| 31 | + bool SignatureOptimize = false; |
| 32 | + auto Args = F->begin()->getFunctionArguments(); |
| 33 | + ConsumedArgToEpilogueReleaseMatcher ArgToReturnReleaseMap( |
| 34 | + RCIA->get(F), F, {SILArgumentConvention::Direct_Owned}); |
| 35 | + |
| 36 | + // Analyze the argument information. |
| 37 | + for (unsigned i : indices(Args)) { |
| 38 | + ArgumentDescriptor &A = TransformDescriptor.ArgumentDescList[i]; |
| 39 | + // If the argument is dead, there is no point in trying to explode it. The |
| 40 | + // dead argument pass will get it. |
| 41 | + if (A.IsEntirelyDead) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + // Do not optimize argument. |
| 46 | + if (!A.canOptimizeLiveArg()) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + // Explosion of generic parameters is not supported yet. |
| 51 | + if (A.Arg->getType().hasArchetype()) |
| 52 | + continue; |
| 53 | + |
| 54 | + A.ProjTree.computeUsesAndLiveness(A.Arg); |
| 55 | + A.Explode = A.shouldExplode(ArgToReturnReleaseMap); |
| 56 | + |
| 57 | + // Modified self argument. |
| 58 | + if (A.Explode && Args[i]->isSelf()) { |
| 59 | + TransformDescriptor.shouldModifySelfArgument = true; |
| 60 | + } |
| 61 | + |
| 62 | + SignatureOptimize |= A.Explode; |
| 63 | + } |
| 64 | + return SignatureOptimize; |
| 65 | +} |
| 66 | + |
| 67 | +void FunctionSignatureTransform::ArgumentExplosionFinalizeOptimizedFunction() { |
| 68 | + SILFunction *NewF = TransformDescriptor.OptimizedFunction.get(); |
| 69 | + SILBasicBlock *BB = &*NewF->begin(); |
| 70 | + SILBuilder Builder(BB->begin()); |
| 71 | + Builder.setCurrentDebugScope(BB->getParent()->getDebugScope()); |
| 72 | + unsigned TotalArgIndex = 0; |
| 73 | + for (ArgumentDescriptor &AD : TransformDescriptor.ArgumentDescList) { |
| 74 | + // If this argument descriptor was dead and we removed it, just skip it. Do |
| 75 | + // not increment the argument index. |
| 76 | + if (AD.WasErased) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + // Simply continue if do not explode. |
| 81 | + if (!AD.Explode) { |
| 82 | + TransformDescriptor.AIM[TotalArgIndex] = AD.Index; |
| 83 | + ++TotalArgIndex; |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + assert(!AD.IsEntirelyDead && |
| 88 | + "Should never see completely dead values here"); |
| 89 | + |
| 90 | + // OK, we need to explode this argument. |
| 91 | + unsigned ArgOffset = ++TotalArgIndex; |
| 92 | + unsigned OldArgIndex = ArgOffset - 1; |
| 93 | + llvm::SmallVector<SILValue, 8> LeafValues; |
| 94 | + |
| 95 | + // We do this in the same order as leaf types since ProjTree expects that |
| 96 | + // the order of leaf values matches the order of leaf types. |
| 97 | + llvm::SmallVector<const ProjectionTreeNode *, 8> LeafNodes; |
| 98 | + AD.ProjTree.getLiveLeafNodes(LeafNodes); |
| 99 | + |
| 100 | + for (auto *Node : LeafNodes) { |
| 101 | + auto OwnershipKind = *AD.getTransformedOwnershipKind(Node->getType()); |
| 102 | + LeafValues.push_back( |
| 103 | + BB->insertFunctionArgument(ArgOffset, Node->getType(), OwnershipKind, |
| 104 | + BB->getArgument(OldArgIndex)->getDecl())); |
| 105 | + TransformDescriptor.AIM[TotalArgIndex - 1] = AD.Index; |
| 106 | + ++ArgOffset; |
| 107 | + ++TotalArgIndex; |
| 108 | + } |
| 109 | + |
| 110 | + // Then go through the projection tree constructing aggregates and replacing |
| 111 | + // uses. |
| 112 | + AD.ProjTree.replaceValueUsesWithLeafUses( |
| 113 | + Builder, BB->getParent()->getLocation(), LeafValues); |
| 114 | + |
| 115 | + // We ignored debugvalue uses when we constructed the new arguments, in |
| 116 | + // order to preserve as much information as possible, we construct a new |
| 117 | + // value for OrigArg from the leaf values and use that in place of the |
| 118 | + // OrigArg. |
| 119 | + SILValue NewOrigArgValue = AD.ProjTree.computeExplodedArgumentValue( |
| 120 | + Builder, BB->getParent()->getLocation(), LeafValues); |
| 121 | + |
| 122 | + // Replace all uses of the original arg with the new value. |
| 123 | + SILArgument *OrigArg = BB->getArgument(OldArgIndex); |
| 124 | + OrigArg->replaceAllUsesWith(NewOrigArgValue); |
| 125 | + |
| 126 | + // Now erase the old argument since it does not have any uses. We also |
| 127 | + // decrement ArgOffset since we have one less argument now. |
| 128 | + BB->eraseArgument(OldArgIndex); |
| 129 | + --TotalArgIndex; |
| 130 | + } |
| 131 | +} |
0 commit comments