@@ -1925,15 +1925,19 @@ swift::cloneFullApplySiteReplacingCallee(FullApplySite applySite,
1925
1925
llvm_unreachable (" Unhandled case?!" );
1926
1926
}
1927
1927
1928
- SILBasicBlock::iterator
1929
- swift::replaceAllUsesAndErase (SingleValueInstruction *svi, SILValue newValue,
1930
- InstModCallbacks &callbacks) {
1931
- assert (svi != newValue && " Cannot RAUW a value with itself" );
1932
- SILBasicBlock::iterator nextii = std::next (svi->getIterator ());
1933
-
1934
- // Only SingleValueInstructions are currently simplified.
1935
- while (!svi->use_empty ()) {
1936
- Operand *use = *svi->use_begin ();
1928
+ // FIXME: For any situation where this may be called on an unbounded number of
1929
+ // uses, it should perform a single callback invocation to notify the client
1930
+ // that newValue has new uses rather than a callback for every new use.
1931
+ //
1932
+ // FIXME: This should almost certainly replace end_lifetime uses rather than
1933
+ // deleting them.
1934
+ SILBasicBlock::iterator swift::replaceAllUses (SILValue oldValue,
1935
+ SILValue newValue,
1936
+ SILBasicBlock::iterator nextii,
1937
+ InstModCallbacks &callbacks) {
1938
+ assert (oldValue != newValue && " Cannot RAUW a value with itself" );
1939
+ while (!oldValue->use_empty ()) {
1940
+ Operand *use = *oldValue->use_begin ();
1937
1941
SILInstruction *user = use->getUser ();
1938
1942
// Erase the end of scope marker.
1939
1943
if (isEndOfScopeMarker (user)) {
@@ -1944,12 +1948,62 @@ swift::replaceAllUsesAndErase(SingleValueInstruction *svi, SILValue newValue,
1944
1948
}
1945
1949
callbacks.setUseValue (use, newValue);
1946
1950
}
1951
+ return nextii;
1952
+ }
1953
+
1954
+ SILBasicBlock::iterator
1955
+ swift::replaceAllUsesAndErase (SingleValueInstruction *svi, SILValue newValue,
1956
+ InstModCallbacks &callbacks) {
1957
+ SILBasicBlock::iterator nextii = replaceAllUses (
1958
+ svi, newValue, std::next (svi->getIterator ()), callbacks);
1947
1959
1948
1960
callbacks.deleteInst (svi);
1949
1961
1950
1962
return nextii;
1951
1963
}
1952
1964
1965
+ SILBasicBlock::iterator
1966
+ swift::replaceAllUsesAndErase (SILValue oldValue, SILValue newValue,
1967
+ InstModCallbacks &callbacks) {
1968
+ auto *blockArg = dyn_cast<SILPhiArgument>(oldValue);
1969
+ if (!blockArg) {
1970
+ // SingleValueInstruction SSA replacement.
1971
+ return replaceAllUsesAndErase (cast<SingleValueInstruction>(oldValue),
1972
+ newValue, callbacks);
1973
+ }
1974
+ llvm_unreachable (" Untested" );
1975
+ #if 0 // FIXME: to be enabled in a following commit
1976
+ TermInst *oldTerm = blockArg->getTerminatorForResult();
1977
+ assert(oldTerm && "can only replace and erase terminators, not phis");
1978
+
1979
+ // Before:
1980
+ // oldTerm bb1, bb2
1981
+ // bb1(%oldValue):
1982
+ // use %oldValue
1983
+ // bb2:
1984
+ //
1985
+ // After:
1986
+ // br bb1
1987
+ // bb1:
1988
+ // use %newValue
1989
+ // bb2:
1990
+
1991
+ auto nextii = replaceAllUses(blockArg, newValue,
1992
+ oldTerm->getParent()->end(), callbacks);
1993
+ // Now that oldValue is replaced, the terminator should have no uses
1994
+ // left. The caller should have removed uses from other results.
1995
+ for (auto *succBB : oldTerm->getSuccessorBlocks()) {
1996
+ assert(succBB->getNumArguments() == 1 && "expected terminator result");
1997
+ succBB->eraseArgument(0);
1998
+ }
1999
+ auto *newBr = SILBuilderWithScope(oldTerm).createBranch(
2000
+ oldTerm->getLoc(), blockArg->getParent());
2001
+ callbacks.createdNewInst(newBr);
2002
+ callbacks.deleteInst(oldTerm);
2003
+ return nextii;
2004
+ #endif
2005
+ }
2006
+
1953
2007
// / Given that we are going to replace use's underlying value, if the use is a
1954
2008
// / lifetime ending use, insert an end scope scope use for the underlying value
1955
2009
// / before we RAUW.
0 commit comments