@@ -159,7 +159,11 @@ class ShrinkBorrowScope {
159
159
return 0 ;
160
160
}
161
161
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 ) {
163
167
if (users.contains (instruction)) {
164
168
if (auto apply = ApplySite::isa (instruction)) {
165
169
SmallVector<int , 2 > rewritableArgumentIndices;
@@ -173,35 +177,41 @@ class ShrinkBorrowScope {
173
177
if (rewritableArgumentIndices.size () != usesInApply (apply)) {
174
178
return false ;
175
179
}
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
+ }
188
194
}
189
195
}
190
196
return true ;
191
197
} else if (auto *bbi = dyn_cast<BeginBorrowInst>(instruction)) {
192
198
if (bbi->isLexical () &&
193
199
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
+ }
197
205
return true ;
198
206
}
199
207
} else if (auto *cvi = dyn_cast<CopyValueInst>(instruction)) {
200
208
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
+ }
205
215
return true ;
206
216
}
207
217
}
0 commit comments