Skip to content

Commit b563c4e

Browse files
committed
[Mem2Reg] Canonicalize new phis.
Mem2Reg introduces new OSSA values in the form of phis. Take responsibility for canonicalizing their lifetimes.
1 parent 52ce100 commit b563c4e

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,8 @@ class MemoryToRegisters {
19481948

19491949
/// Canonicalize the lifetimes of the specified owned and guaranteed values.
19501950
void canonicalizeValueLifetimes(StackList<SILValue> &owned,
1951-
StackList<SILValue> &guaranteed);
1951+
StackList<SILValue> &guaranteed,
1952+
BasicBlockSetVector &livePhiBlocks);
19521953

19531954
public:
19541955
/// C'tor
@@ -2163,10 +2164,29 @@ void MemoryToRegisters::collectStoredValues(AllocStackInst *asi,
21632164
}
21642165

21652166
void MemoryToRegisters::canonicalizeValueLifetimes(
2166-
StackList<SILValue> &owned, StackList<SILValue> &guaranteed) {
2167+
StackList<SILValue> &owned, StackList<SILValue> &guaranteed,
2168+
BasicBlockSetVector &livePhiBlocks) {
21672169
if (Mem2RegDisableLifetimeCanonicalization)
21682170
return;
21692171

2172+
for (auto *block : livePhiBlocks) {
2173+
// When a single alloc_stack is promoted, any block gains at most a single
2174+
// new phi, which appears at the end of its argument list. The collection
2175+
// \p livePhiBlocks consists of exactly those blocks which gained such a
2176+
// new phi.
2177+
SILPhiArgument *argument =
2178+
cast<SILPhiArgument>(block->getArgument(block->getNumArguments() - 1));
2179+
switch (argument->getOwnershipKind()) {
2180+
case OwnershipKind::Owned:
2181+
owned.push_back(argument);
2182+
break;
2183+
case OwnershipKind::Guaranteed:
2184+
guaranteed.push_back(argument);
2185+
break;
2186+
default:
2187+
break;
2188+
}
2189+
}
21702190
CanonicalizeOSSALifetime canonicalizer(
21712191
/*pruneDebug=*/true, /*maximizeLifetime=*/!f.shouldOptimize(), &f,
21722192
accessBlockAnalysis, domInfo, calleeAnalysis, deleter);
@@ -2278,7 +2298,8 @@ bool MemoryToRegisters::run() {
22782298
}
22792299
instructionsToDelete.clear();
22802300
++NumInstRemoved;
2281-
canonicalizeValueLifetimes(ownedValues, guaranteedValues);
2301+
canonicalizeValueLifetimes(ownedValues, guaranteedValues,
2302+
livePhiBlocks);
22822303
madeChange = true;
22832304
}
22842305
}

test/SILOptimizer/mem2reg_lifetime.sil

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,9 @@ bb2:
311311
// CHECK: bb3([[ARG:%.*]] : @owned $@callee_owned () -> Int):
312312
bb3:
313313
%13 = load [copy] %1 : $*@callee_owned () -> Int
314-
// CHECK: [[COPY:%.*]] = copy_value [[ARG]]
315-
// CHECK: [[RESULT:%.*]] = apply [[COPY]]
314+
// : [[COPY:%.*]] = copy_value [[ARG]]
315+
// The above copy is eliminated by owned value canonicalization.
316+
// CHECK: [[RESULT:%.*]] = apply [[ARG]]
316317
%15 = apply %13() : $@callee_owned () -> Int
317318
br bb4
318319

@@ -321,7 +322,8 @@ bb3:
321322
// block.
322323
// CHECK: bb4
323324
bb4:
324-
// CHECK: destroy_value [[ARG]]
325+
// : destroy_value [[ARG]]
326+
// The above destroy is eliminated by owned value canonicalization.
325327
destroy_addr %1 : $*@callee_owned () -> Int
326328
dealloc_stack %1 : $*@callee_owned () -> Int
327329
return %15 : $Int

test/SILOptimizer/mem2reg_ossa.sil

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,9 @@ bb2:
325325
bb3:
326326
// CHECK-NOT: load
327327
%13 = load [copy] %1 : $*@callee_owned () -> Int
328-
// CHECK: [[COPY:%.*]] = copy_value [[ARG]]
329-
// CHECK: [[RESULT:%.*]] = apply [[COPY]]
328+
// : [[COPY:%.*]] = copy_value [[ARG]]
329+
// The above copy is eliminated by owned value canonicalization.
330+
// CHECK: [[RESULT:%.*]] = apply [[ARG]]
330331
%15 = apply %13() : $@callee_owned () -> Int
331332
br bb4
332333

0 commit comments

Comments
 (0)