|
19 | 19 | #ifndef SWIFT_SILOPTIMIZER_UTILS_OWNERSHIPOPTUTILS_H
|
20 | 20 | #define SWIFT_SILOPTIMIZER_UTILS_OWNERSHIPOPTUTILS_H
|
21 | 21 |
|
| 22 | +#include "swift/Basic/Defer.h" |
22 | 23 | #include "swift/SIL/BasicBlockUtils.h"
|
23 | 24 | #include "swift/SIL/OwnershipUtils.h"
|
24 | 25 | #include "swift/SIL/SILModule.h"
|
@@ -76,6 +77,17 @@ struct OwnershipFixupContext {
|
76 | 77 | extraAddressFixupInfo.intPtrOp = InteriorPointerOperand();
|
77 | 78 | }
|
78 | 79 |
|
| 80 | + /// Gets access to the joint post dominance computer and clears it after \p |
| 81 | + /// callback. |
| 82 | + template <typename ResultTy> |
| 83 | + ResultTy withJointPostDomComputer( |
| 84 | + function_ref<ResultTy(JointPostDominanceSetComputer &)> callback) { |
| 85 | + // Make sure we clear the joint post dom computer after callback. |
| 86 | + SWIFT_DEFER { jointPostDomSetComputer.clear(); }; |
| 87 | + // Then return callback passing in the computer. |
| 88 | + return callback(jointPostDomSetComputer); |
| 89 | + } |
| 90 | + |
79 | 91 | private:
|
80 | 92 | /// Helper method called to determine if we discovered we needed interior
|
81 | 93 | /// pointer fixups while simplifying.
|
@@ -129,6 +141,37 @@ class OwnershipRAUWHelper {
|
129 | 141 | SILValue newValue);
|
130 | 142 | };
|
131 | 143 |
|
| 144 | +/// A utility composed ontop of OwnershipFixupContext that knows how to replace |
| 145 | +/// a single use of a value with another value with a different ownership. We |
| 146 | +/// allow for the values to have different types. |
| 147 | +/// |
| 148 | +/// NOTE: When not in OSSA, this just performs a normal set use, so this code is |
| 149 | +/// safe to use with all code. |
| 150 | +class OwnershipReplaceSingleUseHelper { |
| 151 | + OwnershipFixupContext *ctx; |
| 152 | + Operand *use; |
| 153 | + SILValue newValue; |
| 154 | + |
| 155 | +public: |
| 156 | + OwnershipReplaceSingleUseHelper() |
| 157 | + : ctx(nullptr), use(nullptr), newValue(nullptr) {} |
| 158 | + |
| 159 | + /// Return an instance of this class if we support replacing \p use->get() |
| 160 | + /// with \p newValue. |
| 161 | + /// |
| 162 | + /// NOTE: For now we only support objects, not addresses so addresses will |
| 163 | + /// always yield an invalid helper. |
| 164 | + OwnershipReplaceSingleUseHelper(OwnershipFixupContext &ctx, Operand *use, |
| 165 | + SILValue newValue); |
| 166 | + |
| 167 | + /// Returns true if this helper was initialized into a valid state. |
| 168 | + operator bool() const { return isValid(); } |
| 169 | + bool isValid() const { return bool(ctx) && bool(use) && bool(newValue); } |
| 170 | + |
| 171 | + /// Perform the actual RAUW. |
| 172 | + SILBasicBlock::iterator perform(); |
| 173 | +}; |
| 174 | + |
132 | 175 | } // namespace swift
|
133 | 176 |
|
134 | 177 | #endif
|
0 commit comments