@@ -159,7 +159,11 @@ class ShrinkBorrowScope {
159159 return 0 ;
160160 }
161161
162- bool tryHoistOverInstruction (SILInstruction *instruction) {
162+ bool canHoistOverInstruction (SILInstruction *instruction) {
163+ return tryHoistOverInstruction (instruction, /* rewrite=*/ false );
164+ }
165+
166+ bool tryHoistOverInstruction (SILInstruction *instruction, bool rewrite = true ) {
163167 if (users.contains (instruction)) {
164168 if (auto apply = ApplySite::isa (instruction)) {
165169 SmallVector<int , 2 > rewritableArgumentIndices;
@@ -173,35 +177,41 @@ class ShrinkBorrowScope {
173177 if (rewritableArgumentIndices.size () != usesInApply (apply)) {
174178 return false ;
175179 }
176- // We can rewrite all the arguments which are transitive uses of the
177- // borrow.
178- for (auto index : rewritableArgumentIndices) {
179- auto argument = apply.getArgument (index);
180- auto borrowee = introducer->getOperand ();
181- if (auto *cvi = dyn_cast<CopyValueInst>(argument)) {
182- cvi->setOperand (borrowee);
183- modifiedCopyValueInsts.push_back (cvi);
184- madeChange = true ;
185- } else {
186- apply.setArgument (index, borrowee);
187- madeChange = true ;
180+ if (rewrite) {
181+ // We can rewrite all the arguments which are transitive uses of the
182+ // borrow.
183+ for (auto index : rewritableArgumentIndices) {
184+ auto argument = apply.getArgument (index);
185+ auto borrowee = introducer->getOperand ();
186+ if (auto *cvi = dyn_cast<CopyValueInst>(argument)) {
187+ cvi->setOperand (borrowee);
188+ modifiedCopyValueInsts.push_back (cvi);
189+ madeChange = true ;
190+ } else {
191+ apply.setArgument (index, borrowee);
192+ madeChange = true ;
193+ }
188194 }
189195 }
190196 return true ;
191197 } else if (auto *bbi = dyn_cast<BeginBorrowInst>(instruction)) {
192198 if (bbi->isLexical () &&
193199 canReplaceValueWithBorrowedValue (bbi->getOperand ())) {
194- auto borrowee = introducer->getOperand ();
195- bbi->setOperand (borrowee);
196- madeChange = true ;
200+ if (rewrite) {
201+ auto borrowee = introducer->getOperand ();
202+ bbi->setOperand (borrowee);
203+ madeChange = true ;
204+ }
197205 return true ;
198206 }
199207 } else if (auto *cvi = dyn_cast<CopyValueInst>(instruction)) {
200208 if (canReplaceValueWithBorrowedValue (cvi->getOperand ())) {
201- auto borrowee = introducer->getOperand ();
202- cvi->setOperand (borrowee);
203- madeChange = true ;
204- modifiedCopyValueInsts.push_back (cvi);
209+ if (rewrite) {
210+ auto borrowee = introducer->getOperand ();
211+ cvi->setOperand (borrowee);
212+ madeChange = true ;
213+ modifiedCopyValueInsts.push_back (cvi);
214+ }
205215 return true ;
206216 }
207217 }
0 commit comments