|
28 | 28 | #include "swift/SILOptimizer/Analysis/ValueTracking.h"
|
29 | 29 | #include "swift/SILOptimizer/Utils/CFGOptUtils.h"
|
30 | 30 | #include "swift/SILOptimizer/Utils/Existential.h"
|
| 31 | +#include "swift/SILOptimizer/Utils/KeyPathProjector.h" |
31 | 32 | #include "swift/SILOptimizer/Utils/ValueLifetime.h"
|
32 | 33 | #include "llvm/ADT/DenseMap.h"
|
33 | 34 | #include "llvm/ADT/SmallPtrSet.h"
|
34 | 35 | #include "llvm/ADT/SmallVector.h"
|
35 | 36 | #include "llvm/ADT/Statistic.h"
|
| 37 | +#include <utility> |
36 | 38 |
|
37 | 39 | using namespace swift;
|
38 | 40 | using namespace swift::PatternMatch;
|
@@ -199,92 +201,6 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
|
199 | 201 | return NAI;
|
200 | 202 | }
|
201 | 203 |
|
202 |
| -/// Ends the begin_access "scope" if a begin_access was inserted for optimizing |
203 |
| -/// a keypath pattern. |
204 |
| -static void insertEndAccess(BeginAccessInst *&beginAccess, bool isModify, |
205 |
| - SILBuilder &builder) { |
206 |
| - if (beginAccess) { |
207 |
| - builder.createEndAccess(beginAccess->getLoc(), beginAccess, |
208 |
| - /*aborted*/ false); |
209 |
| - if (isModify) |
210 |
| - beginAccess->setAccessKind(SILAccessKind::Modify); |
211 |
| - beginAccess = nullptr; |
212 |
| - } |
213 |
| -} |
214 |
| - |
215 |
| -/// Creates the projection pattern for a keypath instruction. |
216 |
| -/// |
217 |
| -/// Currently only the StoredProperty pattern is handled. |
218 |
| -/// TODO: handle other patterns, like getters/setters, optional chaining, etc. |
219 |
| -/// |
220 |
| -/// Returns false if \p keyPath is not a keypath instruction or if there is any |
221 |
| -/// other reason why the optimization cannot be done. |
222 |
| -static SILValue createKeypathProjections(SILValue keyPath, SILValue root, |
223 |
| - SILLocation loc, |
224 |
| - BeginAccessInst *&beginAccess, |
225 |
| - SILBuilder &builder) { |
226 |
| - if (auto *upCast = dyn_cast<UpcastInst>(keyPath)) |
227 |
| - keyPath = upCast->getOperand(); |
228 |
| - |
229 |
| - // Is it a keypath instruction at all? |
230 |
| - auto *kpInst = dyn_cast<KeyPathInst>(keyPath); |
231 |
| - if (!kpInst || !kpInst->hasPattern()) |
232 |
| - return SILValue(); |
233 |
| - |
234 |
| - auto components = kpInst->getPattern()->getComponents(); |
235 |
| - |
236 |
| - // Check if the keypath only contains patterns which we support. |
237 |
| - for (const KeyPathPatternComponent &comp : components) { |
238 |
| - if (comp.getKind() != KeyPathPatternComponent::Kind::StoredProperty) |
239 |
| - return SILValue(); |
240 |
| - } |
241 |
| - |
242 |
| - SILValue addr = root; |
243 |
| - for (const KeyPathPatternComponent &comp : components) { |
244 |
| - assert(comp.getKind() == KeyPathPatternComponent::Kind::StoredProperty); |
245 |
| - VarDecl *storedProperty = comp.getStoredPropertyDecl(); |
246 |
| - SILValue elementAddr; |
247 |
| - if (addr->getType().getStructOrBoundGenericStruct()) { |
248 |
| - addr = builder.createStructElementAddr(loc, addr, storedProperty); |
249 |
| - } else if (addr->getType().getClassOrBoundGenericClass()) { |
250 |
| - SingleValueInstruction *Ref = builder.createLoad(loc, addr, |
251 |
| - LoadOwnershipQualifier::Unqualified); |
252 |
| - insertEndAccess(beginAccess, /*isModify*/ false, builder); |
253 |
| - |
254 |
| - // Handle the case where the storedProperty is in a super class. |
255 |
| - while (Ref->getType().getClassOrBoundGenericClass() != |
256 |
| - storedProperty->getDeclContext()) { |
257 |
| - SILType superCl = Ref->getType().getSuperclass(); |
258 |
| - if (!superCl) { |
259 |
| - // This should never happen, because the property should be in the |
260 |
| - // decl or in a superclass of it. Just handle this to be on the safe |
261 |
| - // side. |
262 |
| - return SILValue(); |
263 |
| - } |
264 |
| - Ref = builder.createUpcast(loc, Ref, superCl); |
265 |
| - } |
266 |
| - |
267 |
| - addr = builder.createRefElementAddr(loc, Ref, storedProperty); |
268 |
| - |
269 |
| - // Class members need access enforcement. |
270 |
| - if (builder.getModule().getOptions().EnforceExclusivityDynamic) { |
271 |
| - beginAccess = builder.createBeginAccess(loc, addr, SILAccessKind::Read, |
272 |
| - SILAccessEnforcement::Dynamic, |
273 |
| - /*noNestedConflict*/ false, |
274 |
| - /*fromBuiltin*/ false); |
275 |
| - addr = beginAccess; |
276 |
| - } |
277 |
| - } else { |
278 |
| - // This should never happen, as a stored-property pattern can only be |
279 |
| - // applied to classes and structs. But to be safe - and future prove - |
280 |
| - // let's handle this case and bail. |
281 |
| - insertEndAccess(beginAccess, /*isModify*/ false, builder); |
282 |
| - return SILValue(); |
283 |
| - } |
284 |
| - } |
285 |
| - return addr; |
286 |
| -} |
287 |
| - |
288 | 204 | /// Try to optimize a keypath application with an apply instruction.
|
289 | 205 | ///
|
290 | 206 | /// Replaces (simplified SIL):
|
@@ -317,22 +233,26 @@ bool SILCombiner::tryOptimizeKeypath(ApplyInst *AI) {
|
317 | 233 | } else {
|
318 | 234 | return false;
|
319 | 235 | }
|
320 |
| - |
321 |
| - BeginAccessInst *beginAccess = nullptr; |
322 |
| - SILValue projectedAddr = createKeypathProjections(keyPath, rootAddr, |
323 |
| - AI->getLoc(), beginAccess, |
324 |
| - Builder); |
325 |
| - if (!projectedAddr) |
| 236 | + |
| 237 | + auto projector = KeyPathProjector::create(keyPath, rootAddr, |
| 238 | + AI->getLoc(), Builder); |
| 239 | + if (!projector) |
326 | 240 | return false;
|
327 |
| - |
328 |
| - if (isModify) { |
329 |
| - Builder.createCopyAddr(AI->getLoc(), valueAddr, projectedAddr, |
330 |
| - IsTake, IsNotInitialization); |
331 |
| - } else { |
332 |
| - Builder.createCopyAddr(AI->getLoc(), projectedAddr, valueAddr, |
333 |
| - IsNotTake, IsInitialization); |
334 |
| - } |
335 |
| - insertEndAccess(beginAccess, isModify, Builder); |
| 241 | + |
| 242 | + KeyPathProjector::AccessType accessType; |
| 243 | + if (isModify) accessType = KeyPathProjector::AccessType::Set; |
| 244 | + else accessType = KeyPathProjector::AccessType::Get; |
| 245 | + |
| 246 | + projector->project(accessType, [&](SILValue projectedAddr) { |
| 247 | + if (isModify) { |
| 248 | + Builder.createCopyAddr(AI->getLoc(), valueAddr, projectedAddr, |
| 249 | + IsTake, IsNotInitialization); |
| 250 | + } else { |
| 251 | + Builder.createCopyAddr(AI->getLoc(), projectedAddr, valueAddr, |
| 252 | + IsNotTake, IsInitialization); |
| 253 | + } |
| 254 | + }); |
| 255 | + |
336 | 256 | eraseInstFromFunction(*AI);
|
337 | 257 | ++NumOptimizedKeypaths;
|
338 | 258 | return true;
|
@@ -377,19 +297,24 @@ bool SILCombiner::tryOptimizeInoutKeypath(BeginApplyInst *AI) {
|
377 | 297 | EndApplyInst *endApply = dyn_cast<EndApplyInst>(AIUse->getUser());
|
378 | 298 | if (!endApply)
|
379 | 299 | return false;
|
380 |
| - |
381 |
| - BeginAccessInst *beginAccess = nullptr; |
382 |
| - SILValue projectedAddr = createKeypathProjections(keyPath, rootAddr, |
383 |
| - AI->getLoc(), beginAccess, |
384 |
| - Builder); |
385 |
| - if (!projectedAddr) |
| 300 | + |
| 301 | + auto projector = KeyPathProjector::create(keyPath, rootAddr, |
| 302 | + AI->getLoc(), Builder); |
| 303 | + if (!projector) |
386 | 304 | return false;
|
| 305 | + |
| 306 | + KeyPathProjector::AccessType accessType; |
| 307 | + if (isModify) accessType = KeyPathProjector::AccessType::Modify; |
| 308 | + else accessType = KeyPathProjector::AccessType::Get; |
| 309 | + |
| 310 | + projector->project(accessType, [&](SILValue projectedAddr) { |
| 311 | + // Replace the projected address. |
| 312 | + valueAddr->replaceAllUsesWith(projectedAddr); |
| 313 | + |
| 314 | + // Skip to the end of the key path application before cleaning up. |
| 315 | + Builder.setInsertionPoint(endApply); |
| 316 | + }); |
387 | 317 |
|
388 |
| - // Replace the projected address. |
389 |
| - valueAddr->replaceAllUsesWith(projectedAddr); |
390 |
| - |
391 |
| - Builder.setInsertionPoint(endApply); |
392 |
| - insertEndAccess(beginAccess, isModify, Builder); |
393 | 318 | eraseInstFromFunction(*endApply);
|
394 | 319 | eraseInstFromFunction(*AI);
|
395 | 320 | ++NumOptimizedKeypaths;
|
|
0 commit comments