-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathGenOpaque.cpp
716 lines (640 loc) · 29.7 KB
/
GenOpaque.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//===--- GenOpaque.cpp - Swift IR-generation for opaque values ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements IR generation for opaque values and value
// witness operations.
//
// In the comments throughout this file, three type names are used:
// 'B' is the type of a fixed-size buffer
// 'T' is the type which implements a protocol
// 'W' is the type of a witness to the protocol
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/DerivedTypes.h"
#include "IRGenFunction.h"
#include "IRGenModule.h"
#include "ProtocolInfo.h"
#include "ValueWitness.h"
#include "GenOpaque.h"
using namespace swift;
using namespace irgen;
/// A fixed-size buffer is always 16 bytes and pointer-aligned.
/// If we align them more, we'll need to introduce padding to
/// make protocol types work.
Size irgen::getFixedBufferSize(IRGenModule &IGM) {
return 3 * IGM.getPointerSize();
}
Alignment irgen::getFixedBufferAlignment(IRGenModule &IGM) {
return IGM.getPointerAlignment();
}
/// Lazily create the standard fixed-buffer type.
llvm::Type *IRGenModule::getFixedBufferTy() {
if (FixedBufferTy) return FixedBufferTy;
auto size = getFixedBufferSize(*this).getValue();
FixedBufferTy = llvm::ArrayType::get(Int8Ty, size);
return FixedBufferTy;
}
static llvm::Type *createWitnessType(IRGenModule &IGM, ValueWitness index) {
switch (index) {
// void (*deallocateBuffer)(B *buffer, M *self);
// void (*destroyBuffer)(B *buffer, M *self);
case ValueWitness::DeallocateBuffer:
case ValueWitness::DestroyBuffer: {
llvm::Type *bufPtrTy = IGM.getFixedBufferTy()->getPointerTo(0);
llvm::Type *args[] = { bufPtrTy, IGM.TypeMetadataPtrTy };
return llvm::FunctionType::get(IGM.VoidTy, args, /*isVarArg*/ false)
->getPointerTo();
}
// void (*destroy)(T *object, witness_t *self);
case ValueWitness::Destroy: {
llvm::Type *args[] = { IGM.OpaquePtrTy, IGM.TypeMetadataPtrTy };
return llvm::FunctionType::get(IGM.VoidTy, args, /*isVarArg*/ false)
->getPointerTo();
}
// void (*destroyArray)(T *object, size_t n, witness_t *self);
case ValueWitness::DestroyArray: {
llvm::Type *args[] = { IGM.OpaquePtrTy, IGM.SizeTy, IGM.TypeMetadataPtrTy };
return llvm::FunctionType::get(IGM.VoidTy, args, /*isVarArg*/ false)
->getPointerTo();
}
// T *(*initializeBufferWithCopyOfBuffer)(B *dest, B *src, M *self);
// T *(*initializeBufferWithTakeOfBuffer)(B *dest, B *src, M *self);
case ValueWitness::InitializeBufferWithCopyOfBuffer:
case ValueWitness::InitializeBufferWithTakeOfBuffer: {
llvm::Type *bufPtrTy = IGM.getFixedBufferTy()->getPointerTo(0);
llvm::Type *args[] = { bufPtrTy, bufPtrTy, IGM.TypeMetadataPtrTy };
return llvm::FunctionType::get(IGM.OpaquePtrTy, args, /*isVarArg*/ false)
->getPointerTo();
}
// T *(*allocateBuffer)(B *buffer, M *self);
// T *(*projectBuffer)(B *buffer, M *self);
case ValueWitness::AllocateBuffer:
case ValueWitness::ProjectBuffer: {
llvm::Type *bufPtrTy = IGM.getFixedBufferTy()->getPointerTo(0);
llvm::Type *args[] = { bufPtrTy, IGM.TypeMetadataPtrTy };
return llvm::FunctionType::get(IGM.OpaquePtrTy, args, /*isVarArg*/ false)
->getPointerTo();
}
// T *(*initializeBufferWithCopy)(B *dest, T *src, M *self);
// T *(*initializeBufferWithTake)(B *dest, T *src, M *self);
case ValueWitness::InitializeBufferWithCopy:
case ValueWitness::InitializeBufferWithTake: {
llvm::Type *bufPtrTy = IGM.getFixedBufferTy()->getPointerTo(0);
llvm::Type *args[] = { bufPtrTy, IGM.OpaquePtrTy, IGM.TypeMetadataPtrTy };
return llvm::FunctionType::get(IGM.OpaquePtrTy, args, /*isVarArg*/ false)
->getPointerTo();
}
// T *(*assignWithCopy)(T *dest, T *src, M *self);
// T *(*assignWithTake)(T *dest, T *src, M *self);
// T *(*initializeWithCopy)(T *dest, T *src, M *self);
// T *(*initializeWithTake)(T *dest, T *src, M *self);
case ValueWitness::AssignWithCopy:
case ValueWitness::AssignWithTake:
case ValueWitness::InitializeWithCopy:
case ValueWitness::InitializeWithTake: {
llvm::Type *ptrTy = IGM.OpaquePtrTy;
llvm::Type *args[] = { ptrTy, ptrTy, IGM.TypeMetadataPtrTy };
return llvm::FunctionType::get(ptrTy, args, /*isVarArg*/ false)
->getPointerTo();
}
// T *(*initializeArrayWithCopy)(T *dest, T *src, size_t n, M *self);
// T *(*initializeArrayWithTakeFrontToBack)(T *dest, T *src, size_t n, M *self);
// T *(*initializeArrayWithTakeBackToFront)(T *dest, T *src, size_t n, M *self);
case ValueWitness::InitializeArrayWithCopy:
case ValueWitness::InitializeArrayWithTakeFrontToBack:
case ValueWitness::InitializeArrayWithTakeBackToFront: {
llvm::Type *ptrTy = IGM.OpaquePtrTy;
llvm::Type *args[] = { ptrTy, ptrTy, IGM.SizeTy, IGM.TypeMetadataPtrTy };
return llvm::FunctionType::get(ptrTy, args, /*isVarArg*/ false)
->getPointerTo();
}
/// void (*storeExtraInhabitant)(T *obj, unsigned index, M *self);
case ValueWitness::StoreExtraInhabitant: {
llvm::Type *ptrTy = IGM.OpaquePtrTy;
llvm::Type *indexTy = IGM.Int32Ty;
llvm::Type *metaTy = IGM.TypeMetadataPtrTy;
llvm::Type *voidTy = IGM.VoidTy;
llvm::Type *args[] = {ptrTy, indexTy, metaTy};
return llvm::FunctionType::get(voidTy, args, /*isVarArg*/ false)
->getPointerTo();
}
/// int (*getExtraInhabitantIndex)(T *obj, M *self);
case ValueWitness::GetExtraInhabitantIndex: {
llvm::Type *ptrTy = IGM.OpaquePtrTy;
llvm::Type *metaTy = IGM.TypeMetadataPtrTy;
llvm::Type *indexTy = IGM.Int32Ty;
llvm::Type *args[] = {ptrTy, metaTy};
return llvm::FunctionType::get(indexTy, args, /*isVarArg*/ false)
->getPointerTo();
}
/// unsigned (*getEnumTag)(T *obj, M *self);
case ValueWitness::GetEnumTag: {
llvm::Type *ptrTy = IGM.OpaquePtrTy;
llvm::Type *metaTy = IGM.TypeMetadataPtrTy;
llvm::Type *indexTy = IGM.Int32Ty;
llvm::Type *args[] = {ptrTy, metaTy};
return llvm::FunctionType::get(indexTy, args, /*isVarArg*/ false)
->getPointerTo();
}
/// U *(*inplaceProjectEnumData)(T *obj, unsigned tag, M *self);
case ValueWitness::InplaceProjectEnumData: {
llvm::Type *ptrTy = IGM.OpaquePtrTy;
llvm::Type *metaTy = IGM.TypeMetadataPtrTy;
llvm::Type *indexTy = IGM.Int32Ty;
llvm::Type *args[] = {ptrTy, indexTy, metaTy};
return llvm::FunctionType::get(ptrTy, args, /*isVarArg*/ false)
->getPointerTo();
}
case ValueWitness::Size:
case ValueWitness::Flags:
case ValueWitness::Stride:
case ValueWitness::ExtraInhabitantFlags:
// Non-function witnesses all have type size_t.
return IGM.SizeTy;
}
llvm_unreachable("bad value witness!");
}
/// Return the cached pointer-to-function type for the given value
/// witness index.
llvm::Type *IRGenModule::getValueWitnessTy(ValueWitness index) {
assert(unsigned(index) < MaxNumValueWitnesses);
auto &ty = ValueWitnessTys[unsigned(index)];
if (ty) return ty;
ty = createWitnessType(*this, index);
return ty;
}
static StringRef getValueWitnessLabel(ValueWitness index) {
switch (index) {
case ValueWitness::DeallocateBuffer:
return "deallocateBuffer";
case ValueWitness::DestroyBuffer:
return "destroyBuffer";
case ValueWitness::Destroy:
return "destroy";
case ValueWitness::InitializeBufferWithCopyOfBuffer:
return "initializeBufferWithCopyOfBuffer";
case ValueWitness::AllocateBuffer:
return "allocateBuffer";
case ValueWitness::ProjectBuffer:
return "projectBuffer";
case ValueWitness::InitializeBufferWithCopy:
return "initializeBufferWithCopy";
case ValueWitness::InitializeBufferWithTake:
return "initializeBufferWithTake";
case ValueWitness::AssignWithCopy:
return "assignWithCopy";
case ValueWitness::AssignWithTake:
return "assignWithTake";
case ValueWitness::InitializeWithCopy:
return "initializeWithCopy";
case ValueWitness::InitializeWithTake:
return "initializeWithTake";
case ValueWitness::InitializeBufferWithTakeOfBuffer:
return "initializeBufferWithTakeOfBuffer";
case ValueWitness::Size:
return "size";
case ValueWitness::Flags:
return "flags";
case ValueWitness::Stride:
return "stride";
case ValueWitness::DestroyArray:
return "destroyArray";
case ValueWitness::InitializeArrayWithCopy:
return "initializeArrayWithCopy";
case ValueWitness::InitializeArrayWithTakeFrontToBack:
return "initializeArrayWithTakeFrontToBack";
case ValueWitness::InitializeArrayWithTakeBackToFront:
return "initializeArrayWithTakeBackToFront";
case ValueWitness::StoreExtraInhabitant:
return "storeExtraInhabitant";
case ValueWitness::GetExtraInhabitantIndex:
return "getExtraInhabitantIndex";
case ValueWitness::ExtraInhabitantFlags:
return "extraInhabitantFlags";
case ValueWitness::GetEnumTag:
return "getEnumTag";
case ValueWitness::InplaceProjectEnumData:
return "inplaceProjectEnumData";
}
llvm_unreachable("bad value witness index");
}
/// Load a specific witness from a known table. The result is
/// always an i8*.
llvm::Value *irgen::emitInvariantLoadOfOpaqueWitness(IRGenFunction &IGF,
llvm::Value *table,
WitnessIndex index) {
assert(table->getType() == IGF.IGM.WitnessTablePtrTy);
// GEP to the appropriate index, avoiding spurious IR in the trivial case.
llvm::Value *slot = table;
if (index.getValue() != 0)
slot = IGF.Builder.CreateConstInBoundsGEP1_32(
/*Ty=*/nullptr, table, index.getValue());
auto witness =
IGF.Builder.CreateLoad(Address(slot, IGF.IGM.getPointerAlignment()));
IGF.setInvariantLoad(witness);
return witness;
}
/// Given a value witness table, load one of the value witnesses.
/// The result has the appropriate type for the witness.
static llvm::Value *emitLoadOfValueWitness(IRGenFunction &IGF,
llvm::Value *table,
ValueWitness index) {
llvm::Value *witness = emitInvariantLoadOfOpaqueWitness(IGF, table, index);
auto label = getValueWitnessLabel(index);
auto type = IGF.IGM.getValueWitnessTy(index);
if (isValueWitnessFunction(index)) {
return IGF.Builder.CreateBitCast(witness, type, label);
} else {
return IGF.Builder.CreatePtrToInt(witness, type, label);
}
}
/// Given a type metadata pointer, load one of the value witnesses from its
/// value witness table.
static llvm::Value *emitLoadOfValueWitnessFromMetadata(IRGenFunction &IGF,
llvm::Value *metadata,
ValueWitness index) {
llvm::Value *vwtable = IGF.emitValueWitnessTableRefForMetadata(metadata);
return emitLoadOfValueWitness(IGF, vwtable, index);
}
llvm::Value *IRGenFunction::emitValueWitness(CanType type, ValueWitness index) {
if (auto witness =
tryGetLocalTypeData(type, LocalTypeData::forValueWitness(index)))
return witness;
auto vwtable = emitValueWitnessTableRef(type);
auto witness = emitLoadOfValueWitness(*this, vwtable, index);
setScopedLocalTypeData(type, LocalTypeData::forValueWitness(index), witness);
return witness;
}
llvm::Value *IRGenFunction::emitValueWitnessForLayout(SILType type,
ValueWitness index) {
if (auto witness = tryGetLocalTypeDataForLayout(type,
LocalTypeData::forValueWitness(index)))
return witness;
auto vwtable = emitValueWitnessTableRefForLayout(type);
auto witness = emitLoadOfValueWitness(*this, vwtable, index);
setScopedLocalTypeDataForLayout(type,
LocalTypeData::forValueWitness(index), witness);
return witness;
}
/// Given a call to a helper function that produces a result
/// into its first argument, set attributes appropriately.
static void setHelperAttributesForAggResult(llvm::CallInst *call,
bool isFormalResult = true) {
// Set as nounwind.
auto attrs = llvm::AttributeSet::get(call->getContext(),
llvm::AttributeSet::FunctionIndex,
llvm::Attribute::NoUnwind);
attrs = attrs.addAttribute(call->getContext(), 1, llvm::Attribute::NoAlias);
// Only set 'sret' if this is also the formal result.
if (isFormalResult) {
attrs = attrs.addAttribute(call->getContext(), 1,
llvm::Attribute::StructRet);
}
call->setAttributes(attrs);
}
/// Given a call to a helper function, set attributes appropriately.
static void setHelperAttributes(llvm::CallInst *call) {
// Set as nounwind.
auto attrs = llvm::AttributeSet::get(call->getContext(),
llvm::AttributeSet::FunctionIndex,
llvm::Attribute::NoUnwind);
call->setAttributes(attrs);
}
/// Emit a call to do an 'initializeBufferWithCopyOfBuffer' operation.
llvm::Value *irgen::emitInitializeBufferWithCopyOfBufferCall(IRGenFunction &IGF,
llvm::Value *metadata,
Address destBuffer,
Address srcBuffer) {
llvm::Value *copyFn = emitLoadOfValueWitnessFromMetadata(IGF, metadata,
ValueWitness::InitializeBufferWithCopyOfBuffer);
llvm::CallInst *call =
IGF.Builder.CreateCall(
copyFn,
{destBuffer.getAddress(), srcBuffer.getAddress(), metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
setHelperAttributesForAggResult(call, false);
return call;
}
/// Emit a call to do an 'initializeBufferWithTakeOfBuffer' operation.
llvm::Value *irgen::emitInitializeBufferWithTakeOfBufferCall(IRGenFunction &IGF,
llvm::Value *metadata,
Address destBuffer,
Address srcBuffer) {
llvm::Value *copyFn = emitLoadOfValueWitnessFromMetadata(IGF, metadata,
ValueWitness::InitializeBufferWithTakeOfBuffer);
llvm::CallInst *call =
IGF.Builder.CreateCall(
copyFn,
{destBuffer.getAddress(), srcBuffer.getAddress(), metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
setHelperAttributesForAggResult(call, false);
return call;
}
/// Emit a call to do an 'allocateBuffer' operation.
llvm::Value *irgen::emitAllocateBufferCall(IRGenFunction &IGF,
SILType T,
Address buffer) {
llvm::Value *metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *allocateFn
= IGF.emitValueWitnessForLayout(T, ValueWitness::AllocateBuffer);
llvm::CallInst *result =
IGF.Builder.CreateCall(allocateFn, {buffer.getAddress(), metadata});
result->setCallingConv(IGF.IGM.RuntimeCC);
result->setDoesNotThrow();
return result;
}
/// Emit a call to do a 'projectBuffer' operation.
llvm::Value *irgen::emitProjectBufferCall(IRGenFunction &IGF,
llvm::Value *metadata,
Address buffer) {
llvm::Value *projectFn = emitLoadOfValueWitnessFromMetadata(IGF, metadata,
ValueWitness::ProjectBuffer);
llvm::CallInst *result =
IGF.Builder.CreateCall(projectFn, {buffer.getAddress(), metadata});
result->setCallingConv(IGF.IGM.RuntimeCC);
result->setDoesNotThrow();
return result;
}
/// Emit a call to do an 'initializeWithCopy' operation.
void irgen::emitInitializeWithCopyCall(IRGenFunction &IGF,
SILType T,
llvm::Value *destObject,
llvm::Value *srcObject) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *copyFn = IGF.emitValueWitnessForLayout(T,
ValueWitness::InitializeWithCopy);
llvm::CallInst *call =
IGF.Builder.CreateCall(copyFn, {destObject, srcObject, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
call->setDoesNotThrow();
}
llvm::Value *irgen::emitInitializeBufferWithTakeCall(IRGenFunction &IGF,
SILType T,
llvm::Value *destObject,
llvm::Value *srcObject) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *copyFn = IGF.emitValueWitnessForLayout(T,
ValueWitness::InitializeBufferWithTake);
llvm::CallInst *call =
IGF.Builder.CreateCall(copyFn, {destObject, srcObject, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
call->setDoesNotThrow();
return call;
}
llvm::Value *irgen::emitInitializeBufferWithCopyCall(IRGenFunction &IGF,
SILType T,
llvm::Value *destObject,
llvm::Value *srcObject) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *copyFn = IGF.emitValueWitnessForLayout(T,
ValueWitness::InitializeBufferWithCopy);
llvm::CallInst *call =
IGF.Builder.CreateCall(copyFn, {destObject, srcObject, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
call->setDoesNotThrow();
return call;
}
/// Emit a call to do an 'initializeArrayWithCopy' operation.
void irgen::emitInitializeArrayWithCopyCall(IRGenFunction &IGF,
SILType T,
llvm::Value *destObject,
llvm::Value *srcObject,
llvm::Value *count) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *copyFn = IGF.emitValueWitnessForLayout(T,
ValueWitness::InitializeArrayWithCopy);
llvm::CallInst *call =
IGF.Builder.CreateCall(copyFn, {destObject, srcObject, count, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
call->setDoesNotThrow();
}
/// Emit a call to do an 'initializeWithTake' operation.
void irgen::emitInitializeWithTakeCall(IRGenFunction &IGF,
SILType T,
llvm::Value *destObject,
llvm::Value *srcObject) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *copyFn = IGF.emitValueWitnessForLayout(T,
ValueWitness::InitializeWithTake);
llvm::CallInst *call =
IGF.Builder.CreateCall(copyFn, {destObject, srcObject, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
call->setDoesNotThrow();
}
/// Emit a call to do an 'initializeArrayWithTakeFrontToBack' operation.
void irgen::emitInitializeArrayWithTakeFrontToBackCall(IRGenFunction &IGF,
SILType T,
llvm::Value *destObject,
llvm::Value *srcObject,
llvm::Value *count) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *copyFn = IGF.emitValueWitnessForLayout(T,
ValueWitness::InitializeArrayWithTakeFrontToBack);
llvm::CallInst *call =
IGF.Builder.CreateCall(copyFn, {destObject, srcObject, count, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
call->setDoesNotThrow();
}
/// Emit a call to do an 'initializeArrayWithTakeBackToFront' operation.
void irgen::emitInitializeArrayWithTakeBackToFrontCall(IRGenFunction &IGF,
SILType T,
llvm::Value *destObject,
llvm::Value *srcObject,
llvm::Value *count) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *copyFn = IGF.emitValueWitnessForLayout(T,
ValueWitness::InitializeArrayWithTakeBackToFront);
llvm::CallInst *call =
IGF.Builder.CreateCall(copyFn, {destObject, srcObject, count, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
call->setDoesNotThrow();
}
/// Emit a call to do an 'assignWithCopy' operation.
void irgen::emitAssignWithCopyCall(IRGenFunction &IGF,
llvm::Value *metadata,
llvm::Value *destObject,
llvm::Value *srcObject) {
llvm::Value *copyFn = emitLoadOfValueWitnessFromMetadata(IGF, metadata,
ValueWitness::AssignWithCopy);
llvm::CallInst *call =
IGF.Builder.CreateCall(copyFn, {destObject, srcObject, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
call->setDoesNotThrow();
}
void irgen::emitAssignWithCopyCall(IRGenFunction &IGF,
SILType T,
llvm::Value *destObject,
llvm::Value *srcObject) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *copyFn = IGF.emitValueWitnessForLayout(T,
ValueWitness::AssignWithCopy);
llvm::CallInst *call =
IGF.Builder.CreateCall(copyFn, {destObject, srcObject, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
call->setDoesNotThrow();
}
/// Emit a call to do an 'assignWithTake' operation.
void irgen::emitAssignWithTakeCall(IRGenFunction &IGF,
SILType T,
llvm::Value *destObject,
llvm::Value *srcObject) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *copyFn = IGF.emitValueWitnessForLayout(T,
ValueWitness::AssignWithTake);
llvm::CallInst *call =
IGF.Builder.CreateCall(copyFn, {destObject, srcObject, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
call->setDoesNotThrow();
}
/// Emit a call to do a 'destroy' operation.
void irgen::emitDestroyCall(IRGenFunction &IGF,
SILType T,
llvm::Value *object) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *fn = IGF.emitValueWitnessForLayout(T,
ValueWitness::Destroy);
llvm::CallInst *call = IGF.Builder.CreateCall(fn, {object, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
setHelperAttributes(call);
}
/// Emit a call to do a 'destroyArray' operation.
void irgen::emitDestroyArrayCall(IRGenFunction &IGF,
SILType T,
llvm::Value *object,
llvm::Value *count) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *fn = IGF.emitValueWitnessForLayout(T,
ValueWitness::DestroyArray);
llvm::CallInst *call = IGF.Builder.CreateCall(fn, {object, count, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
setHelperAttributes(call);
}
/// Emit a call to do a 'destroyBuffer' operation.
void irgen::emitDestroyBufferCall(IRGenFunction &IGF,
llvm::Value *metadata,
Address buffer) {
auto fn = emitLoadOfValueWitnessFromMetadata(IGF, metadata,
ValueWitness::DestroyBuffer);
llvm::CallInst *call =
IGF.Builder.CreateCall(fn, {buffer.getAddress(), metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
setHelperAttributes(call);
}
/// Emit a call to do a 'deallocateBuffer' operation.
void irgen::emitDeallocateBufferCall(IRGenFunction &IGF,
llvm::Value *metadata,
Address buffer) {
auto fn = emitLoadOfValueWitnessFromMetadata(IGF, metadata,
ValueWitness::DeallocateBuffer);
llvm::CallInst *call =
IGF.Builder.CreateCall(fn, {buffer.getAddress(), metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
setHelperAttributes(call);
}
void irgen::emitDeallocateBufferCall(IRGenFunction &IGF,
SILType T,
Address buffer) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *fn = IGF.emitValueWitnessForLayout(T,
ValueWitness::DeallocateBuffer);
llvm::CallInst *call =
IGF.Builder.CreateCall(fn, {buffer.getAddress(), metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
setHelperAttributes(call);
}
/// Emit a call to the 'getExtraInhabitantIndex' operation.
/// The type must be dynamically known to have extra inhabitant witnesses.
llvm::Value *irgen::emitGetExtraInhabitantIndexCall(IRGenFunction &IGF,
SILType T,
llvm::Value *srcObject) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *fn = IGF.emitValueWitnessForLayout(T,
ValueWitness::GetExtraInhabitantIndex);
llvm::CallInst *call =
IGF.Builder.CreateCall(fn, {srcObject, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
setHelperAttributes(call);
return call;
}
/// Emit a call to the 'storeExtraInhabitant' operation.
/// The type must be dynamically known to have extra inhabitant witnesses.
llvm::Value *irgen::emitStoreExtraInhabitantCall(IRGenFunction &IGF,
SILType T,
llvm::Value *index,
llvm::Value *destObject) {
auto metadata = IGF.emitTypeMetadataRefForLayout(T);
llvm::Value *fn = IGF.emitValueWitnessForLayout(T,
ValueWitness::StoreExtraInhabitant);
llvm::CallInst *call =
IGF.Builder.CreateCall(fn, {destObject, index, metadata});
call->setCallingConv(IGF.IGM.RuntimeCC);
setHelperAttributes(call);
return call;
}
/// Load the 'size' value witness from the given table as a size_t.
llvm::Value *irgen::emitLoadOfSize(IRGenFunction &IGF, SILType T) {
return IGF.emitValueWitnessForLayout(T, ValueWitness::Size);
}
/// Load the 'alignmentMask' value witness from the given table as a size_t.
llvm::Value *irgen::emitLoadOfAlignmentMask(IRGenFunction &IGF, SILType T) {
auto flags = IGF.emitValueWitnessForLayout(T, ValueWitness::Flags);
auto mask = IGF.IGM.getSize(Size(ValueWitnessFlags::AlignmentMask));
return IGF.Builder.CreateAnd(flags, mask,
flags->getName() + ".alignmentMask");
}
/// Load the 'isPOD' valueWitness from the given table as an i1.
llvm::Value *irgen::emitLoadOfIsPOD(IRGenFunction &IGF, SILType T) {
auto flags = IGF.emitValueWitnessForLayout(T, ValueWitness::Flags);
auto mask = IGF.IGM.getSize(Size(ValueWitnessFlags::IsNonPOD));
auto masked = IGF.Builder.CreateAnd(flags, mask);
return IGF.Builder.CreateICmpEQ(masked, IGF.IGM.getSize(Size(0)),
flags->getName() + ".isPOD");
}
/// Load the 'isBitwiseTakable' valueWitness from the given table as an i1.
llvm::Value *irgen::emitLoadOfIsBitwiseTakable(IRGenFunction &IGF, SILType T) {
auto flags = IGF.emitValueWitnessForLayout(T, ValueWitness::Flags);
auto mask = IGF.IGM.getSize(Size(ValueWitnessFlags::IsNonBitwiseTakable));
auto masked = IGF.Builder.CreateAnd(flags, mask);
return IGF.Builder.CreateICmpEQ(masked, IGF.IGM.getSize(Size(0)),
flags->getName() + ".isBitwiseTakable");
}
/// Load the 'isInline' valueWitness from the given table as an i1.
llvm::Value *irgen::emitLoadOfIsInline(IRGenFunction &IGF, SILType T) {
auto flags = IGF.emitValueWitnessForLayout(T, ValueWitness::Flags);
auto mask = IGF.IGM.getSize(Size(ValueWitnessFlags::IsNonInline));
auto masked = IGF.Builder.CreateAnd(flags, mask);
return IGF.Builder.CreateICmpEQ(masked, IGF.IGM.getSize(Size(0)),
flags->getName() + ".isInline");
}
/// Load the 'hasExtraInhabitants' valueWitness from the given table as an i1.
llvm::Value *irgen::emitLoadOfHasExtraInhabitants(IRGenFunction &IGF, SILType T) {
auto flags = IGF.emitValueWitnessForLayout(T, ValueWitness::Flags);
auto mask = IGF.IGM.getSize(Size(ValueWitnessFlags::Enum_HasExtraInhabitants));
auto masked = IGF.Builder.CreateAnd(flags, mask);
return IGF.Builder.CreateICmpNE(masked, IGF.IGM.getSize(Size(0)),
flags->getName() + ".hasExtraInhabitants");
}
/// Load the 'stride' value witness from the given table as a size_t.
llvm::Value *irgen::emitLoadOfStride(IRGenFunction &IGF, SILType T) {
return IGF.emitValueWitnessForLayout(T, ValueWitness::Stride);
}
llvm::Value *irgen::emitLoadOfExtraInhabitantCount(IRGenFunction &IGF,
SILType T) {
auto xiFlags = IGF.emitValueWitnessForLayout(T,
ValueWitness::ExtraInhabitantFlags);
auto mask = IGF.IGM.getSize(
Size(ExtraInhabitantFlags::NumExtraInhabitantsMask));
return IGF.Builder.CreateAnd(xiFlags, mask,
xiFlags->getName() + ".extraInhabitantCount");
}