@@ -200,7 +200,86 @@ bool isOwnershipForwardingInst(SILInstruction *i);
200
200
201
201
bool isGuaranteedForwardingInst (SILInstruction *i);
202
202
203
- struct BorrowScopeIntroducerKind {
203
+ struct BorrowScopeOperandKind {
204
+ using UnderlyingKindTy = std::underlying_type<SILInstructionKind>::type;
205
+
206
+ enum Kind : UnderlyingKindTy {
207
+ BeginBorrow = UnderlyingKindTy (SILInstructionKind::BeginBorrowInst),
208
+ BeginApply = UnderlyingKindTy (SILInstructionKind::BeginApplyInst),
209
+ };
210
+
211
+ Kind value;
212
+
213
+ BorrowScopeOperandKind (Kind newValue) : value(newValue) {}
214
+ BorrowScopeOperandKind (const BorrowScopeOperandKind &other)
215
+ : value(other.value) {}
216
+ operator Kind () const { return value; }
217
+
218
+ static Optional<BorrowScopeOperandKind> get (SILInstructionKind kind) {
219
+ switch (kind) {
220
+ default :
221
+ return None;
222
+ case SILInstructionKind::BeginBorrowInst:
223
+ return BorrowScopeOperandKind (BeginBorrow);
224
+ case SILInstructionKind::BeginApplyInst:
225
+ return BorrowScopeOperandKind (BeginApply);
226
+ }
227
+ }
228
+
229
+ void print (llvm::raw_ostream &os) const ;
230
+ LLVM_ATTRIBUTE_DEPRECATED (void dump () const , "only for use in the debugger");
231
+ };
232
+
233
+ // / An operand whose user instruction introduces a new borrow scope for the
234
+ // / operand's value. The value of the operand must be considered as implicitly
235
+ // / borrowed until the user's corresponding end scope instruction.
236
+ struct BorrowScopeOperand {
237
+ BorrowScopeOperandKind kind;
238
+ Operand *op;
239
+
240
+ BorrowScopeOperand (Operand *op)
241
+ : kind(*BorrowScopeOperandKind::get (op->getUser ()->getKind())), op(op) {}
242
+
243
+ // / If value is a borrow introducer return it after doing some checks.
244
+ static Optional<BorrowScopeOperand> get (Operand *op) {
245
+ auto *user = op->getUser ();
246
+ auto kind = BorrowScopeOperandKind::get (user->getKind ());
247
+ if (!kind)
248
+ return None;
249
+ return BorrowScopeOperand (*kind, op);
250
+ }
251
+
252
+ void visitEndScopeInstructions (function_ref<void (Operand *)> func) const {
253
+ switch (kind) {
254
+ case BorrowScopeOperandKind::BeginBorrow:
255
+ for (auto *use : cast<BeginBorrowInst>(op->getUser ())->getUses ()) {
256
+ if (isa<EndBorrowInst>(use->getUser ())) {
257
+ func (use);
258
+ }
259
+ }
260
+ return ;
261
+ case BorrowScopeOperandKind::BeginApply: {
262
+ auto *user = cast<BeginApplyInst>(op->getUser ());
263
+ for (auto *use : user->getTokenResult ()->getUses ()) {
264
+ func (use);
265
+ }
266
+ return ;
267
+ }
268
+ }
269
+ llvm_unreachable (" Covered switch isn't covered" );
270
+ }
271
+
272
+ private:
273
+ // / Internal constructor for failable static constructor. Please do not expand
274
+ // / its usage since it assumes the code passed in is well formed.
275
+ BorrowScopeOperand (BorrowScopeOperandKind kind, Operand *op)
276
+ : kind(kind), op(op) {}
277
+ };
278
+
279
+ llvm::raw_ostream &operator <<(llvm::raw_ostream &os,
280
+ BorrowScopeOperandKind kind);
281
+
282
+ struct BorrowScopeIntroducingValueKind {
204
283
using UnderlyingKindTy = std::underlying_type<ValueKind>::type;
205
284
206
285
// / Enum we use for exhaustive pattern matching over borrow scope introducers.
@@ -210,23 +289,23 @@ struct BorrowScopeIntroducerKind {
210
289
SILFunctionArgument = UnderlyingKindTy (ValueKind::SILFunctionArgument),
211
290
};
212
291
213
- static Optional<BorrowScopeIntroducerKind > get (ValueKind kind) {
292
+ static Optional<BorrowScopeIntroducingValueKind > get (ValueKind kind) {
214
293
switch (kind) {
215
294
default :
216
295
return None;
217
296
case ValueKind::LoadBorrowInst:
218
- return BorrowScopeIntroducerKind (LoadBorrow);
297
+ return BorrowScopeIntroducingValueKind (LoadBorrow);
219
298
case ValueKind::BeginBorrowInst:
220
- return BorrowScopeIntroducerKind (BeginBorrow);
299
+ return BorrowScopeIntroducingValueKind (BeginBorrow);
221
300
case ValueKind::SILFunctionArgument:
222
- return BorrowScopeIntroducerKind (SILFunctionArgument);
301
+ return BorrowScopeIntroducingValueKind (SILFunctionArgument);
223
302
}
224
303
}
225
304
226
305
Kind value;
227
306
228
- BorrowScopeIntroducerKind (Kind newValue) : value(newValue) {}
229
- BorrowScopeIntroducerKind (const BorrowScopeIntroducerKind &other)
307
+ BorrowScopeIntroducingValueKind (Kind newValue) : value(newValue) {}
308
+ BorrowScopeIntroducingValueKind (const BorrowScopeIntroducingValueKind &other)
230
309
: value(other.value) {}
231
310
operator Kind () const { return value; }
232
311
@@ -238,10 +317,10 @@ struct BorrowScopeIntroducerKind {
238
317
// / of the scope.
239
318
bool isLocalScope () const {
240
319
switch (value) {
241
- case BorrowScopeIntroducerKind ::BeginBorrow:
242
- case BorrowScopeIntroducerKind ::LoadBorrow:
320
+ case BorrowScopeIntroducingValueKind ::BeginBorrow:
321
+ case BorrowScopeIntroducingValueKind ::LoadBorrow:
243
322
return true ;
244
- case BorrowScopeIntroducerKind ::SILFunctionArgument:
323
+ case BorrowScopeIntroducingValueKind ::SILFunctionArgument:
245
324
return false ;
246
325
}
247
326
llvm_unreachable (" Covered switch isnt covered?!" );
@@ -252,7 +331,7 @@ struct BorrowScopeIntroducerKind {
252
331
};
253
332
254
333
llvm::raw_ostream &operator <<(llvm::raw_ostream &os,
255
- BorrowScopeIntroducerKind kind);
334
+ BorrowScopeIntroducingValueKind kind);
256
335
257
336
// / A higher level construct for working with values that represent the
258
337
// / introduction of a new borrow scope.
@@ -271,26 +350,26 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
271
350
// / borrow introducers can not have guaranteed results that are not creating a
272
351
// / new borrow scope. No such instructions exist today.
273
352
struct BorrowScopeIntroducingValue {
274
- BorrowScopeIntroducerKind kind;
353
+ BorrowScopeIntroducingValueKind kind;
275
354
SILValue value;
276
355
277
356
BorrowScopeIntroducingValue (LoadBorrowInst *lbi)
278
- : kind(BorrowScopeIntroducerKind ::LoadBorrow), value(lbi) {}
357
+ : kind(BorrowScopeIntroducingValueKind ::LoadBorrow), value(lbi) {}
279
358
BorrowScopeIntroducingValue (BeginBorrowInst *bbi)
280
- : kind(BorrowScopeIntroducerKind ::BeginBorrow), value(bbi) {}
359
+ : kind(BorrowScopeIntroducingValueKind ::BeginBorrow), value(bbi) {}
281
360
BorrowScopeIntroducingValue (SILFunctionArgument *arg)
282
- : kind(BorrowScopeIntroducerKind ::SILFunctionArgument), value(arg) {
361
+ : kind(BorrowScopeIntroducingValueKind ::SILFunctionArgument), value(arg) {
283
362
assert (arg->getOwnershipKind () == ValueOwnershipKind::Guaranteed);
284
363
}
285
364
286
365
BorrowScopeIntroducingValue (SILValue v)
287
- : kind(*BorrowScopeIntroducerKind ::get (v->getKind ())), value(v) {
366
+ : kind(*BorrowScopeIntroducingValueKind ::get (v->getKind ())), value(v) {
288
367
assert (v.getOwnershipKind () == ValueOwnershipKind::Guaranteed);
289
368
}
290
369
291
370
// / If value is a borrow introducer return it after doing some checks.
292
371
static Optional<BorrowScopeIntroducingValue> get (SILValue value) {
293
- auto kind = BorrowScopeIntroducerKind ::get (value->getKind ());
372
+ auto kind = BorrowScopeIntroducingValueKind ::get (value->getKind ());
294
373
if (!kind || value.getOwnershipKind () != ValueOwnershipKind::Guaranteed)
295
374
return None;
296
375
return BorrowScopeIntroducingValue (*kind, value);
@@ -334,7 +413,8 @@ struct BorrowScopeIntroducingValue {
334
413
private:
335
414
// / Internal constructor for failable static constructor. Please do not expand
336
415
// / its usage since it assumes the code passed in is well formed.
337
- BorrowScopeIntroducingValue (BorrowScopeIntroducerKind kind, SILValue value)
416
+ BorrowScopeIntroducingValue (BorrowScopeIntroducingValueKind kind,
417
+ SILValue value)
338
418
: kind(kind), value(value) {}
339
419
};
340
420
0 commit comments