Skip to content

Commit ae41794

Browse files
committed
IRGen: Small refactoring for emitInvariantLoadFromMetadataAtIndex()
NFC other than eliminating some unnecessary casting when accessing class field offsets from the field offset vector.
1 parent 055b326 commit ae41794

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

lib/IRGen/GenMeta.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,13 +3643,10 @@ void IRGenFunction::setDereferenceableLoad(llvm::LoadInst *load,
36433643
}
36443644

36453645
/// Emit a load from the given metadata at a constant index.
3646-
///
3647-
/// The load is marked invariant. This function should not be called
3648-
/// on metadata objects that are in the process of being initialized.
3649-
static llvm::LoadInst *emitInvariantLoadFromMetadataAtIndex(IRGenFunction &IGF,
3650-
llvm::Value *metadata,
3651-
int index,
3652-
llvm::PointerType *objectTy,
3646+
static llvm::LoadInst *emitLoadFromMetadataAtIndex(IRGenFunction &IGF,
3647+
llvm::Value *metadata,
3648+
int index,
3649+
llvm::Type *objectTy,
36533650
const llvm::Twine &suffix = ""){
36543651
// Require the metadata to be some type that we recognize as a
36553652
// metadata pointer.
@@ -3658,6 +3655,8 @@ static llvm::LoadInst *emitInvariantLoadFromMetadataAtIndex(IRGenFunction &IGF,
36583655
// We require objectType to be a pointer type so that the GEP will
36593656
// scale by the right amount. We could load an arbitrary type using
36603657
// some extra bitcasting.
3658+
assert(IGF.IGM.DataLayout.getTypeStoreSize(objectTy) ==
3659+
IGF.IGM.DataLayout.getTypeStoreSize(IGF.IGM.SizeTy));
36613660

36623661
// Cast to T*.
36633662
auto objectPtrTy = objectTy->getPointerTo();
@@ -3670,8 +3669,21 @@ static llvm::LoadInst *emitInvariantLoadFromMetadataAtIndex(IRGenFunction &IGF,
36703669
IGF.IGM.getPointerAlignment());
36713670

36723671
// Load.
3673-
auto result = IGF.Builder.CreateLoad(slot,
3674-
metadata->getName() + suffix);
3672+
return IGF.Builder.CreateLoad(slot, metadata->getName() + suffix);
3673+
}
3674+
3675+
/// Emit a load from the given metadata at a constant index.
3676+
///
3677+
/// The load is marked invariant. This function should not be called
3678+
/// on metadata objects that are in the process of being initialized.
3679+
static llvm::LoadInst *emitInvariantLoadFromMetadataAtIndex(IRGenFunction &IGF,
3680+
llvm::Value *metadata,
3681+
int index,
3682+
llvm::Type *objectTy,
3683+
const llvm::Twine &suffix = ""){
3684+
3685+
auto result = emitLoadFromMetadataAtIndex(IGF, metadata, index, objectTy,
3686+
suffix);
36753687
IGF.setInvariantLoad(result);
36763688
return result;
36773689
}
@@ -3696,8 +3708,8 @@ IRGenFunction::emitValueWitnessTableRef(CanType type) {
36963708
llvm::Value *
36973709
IRGenFunction::emitValueWitnessTableRefForMetadata(llvm::Value *metadata) {
36983710
auto witness = emitInvariantLoadFromMetadataAtIndex(*this, metadata, -1,
3699-
IGM.WitnessTablePtrTy,
3700-
".valueWitnesses");
3711+
IGM.WitnessTablePtrTy,
3712+
".valueWitnesses");
37013713
// A value witness table is dereferenceable to the number of value witness
37023714
// pointers.
37033715

@@ -3733,15 +3745,15 @@ static llvm::Value *emitLoadOfMetadataRefAtIndex(IRGenFunction &IGF,
37333745
llvm::Value *metadata,
37343746
int index) {
37353747
return emitInvariantLoadFromMetadataAtIndex(IGF, metadata, index,
3736-
IGF.IGM.TypeMetadataPtrTy);
3748+
IGF.IGM.TypeMetadataPtrTy);
37373749
}
37383750

37393751
/// Load the protocol witness table reference at the given index.
37403752
static llvm::Value *emitLoadOfWitnessTableRefAtIndex(IRGenFunction &IGF,
37413753
llvm::Value *metadata,
37423754
int index) {
37433755
return emitInvariantLoadFromMetadataAtIndex(IGF, metadata, index,
3744-
IGF.IGM.WitnessTablePtrTy);
3756+
IGF.IGM.WitnessTablePtrTy);
37453757
}
37463758

37473759
namespace {
@@ -3935,8 +3947,8 @@ llvm::Value *irgen::emitClassFieldOffset(IRGenFunction &IGF,
39353947
llvm::Value *metadata) {
39363948
irgen::Size offset = getClassFieldOffset(IGF.IGM, theClass, field);
39373949
int index = getOffsetInWords(IGF.IGM, offset);
3938-
llvm::Value *val = emitLoadOfWitnessTableRefAtIndex(IGF, metadata, index);
3939-
return IGF.Builder.CreatePtrToInt(val, IGF.IGM.SizeTy);
3950+
return emitInvariantLoadFromMetadataAtIndex(IGF, metadata, index,
3951+
IGF.IGM.SizeTy);
39403952
}
39413953

39423954
/// Given a reference to class metadata of the given type,
@@ -4200,7 +4212,8 @@ llvm::Value *irgen::emitClassHeapMetadataRefForMetatype(IRGenFunction &IGF,
42004212
// a select here instead, which might be profitable.
42014213
IGF.Builder.emitBlock(wrapBB);
42024214
auto classFromWrapper =
4203-
emitInvariantLoadFromMetadataAtIndex(IGF, metatype, 1, IGF.IGM.TypeMetadataPtrTy);
4215+
emitInvariantLoadFromMetadataAtIndex(IGF, metatype, 1,
4216+
IGF.IGM.TypeMetadataPtrTy);
42044217
IGF.Builder.CreateBr(contBB);
42054218

42064219
// Continuation block.

test/IRGen/class_resilience_objc.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ public class GenericObjCSubclass<T> : NSCoder {
6464
// CHECK-64-NEXT: [[ISA_VALUE:%.*]] = and [[INT]] [[ISA]], [[ISA_MASK]]
6565
// CHECK-64-NEXT: [[ISA:%.*]] = inttoptr [[INT]] [[ISA_VALUE]] to %swift.type*
6666

67-
// CHECK-NEXT: [[ISA_ADDR:%.*]] = bitcast %swift.type* [[ISA]] to i8***
67+
// CHECK-NEXT: [[ISA_ADDR:%.*]] = bitcast %swift.type* [[ISA]] to [[INT]]*
6868

69-
// CHECK-32-NEXT: [[FIELD_OFFSET_ADDR:%.*]] = getelementptr inbounds i8**, i8*** [[ISA_ADDR]], [[INT]] 16
69+
// CHECK-32-NEXT: [[FIELD_OFFSET_ADDR:%.*]] = getelementptr inbounds [[INT]], [[INT]]* [[ISA_ADDR]], [[INT]] 16
7070

71-
// CHECK-64-NEXT: [[FIELD_OFFSET_ADDR:%.*]] = getelementptr inbounds i8**, i8*** [[ISA_ADDR]], [[INT]] 13
71+
// CHECK-64-NEXT: [[FIELD_OFFSET_ADDR:%.*]] = getelementptr inbounds [[INT]], [[INT]]* [[ISA_ADDR]], [[INT]] 13
7272

73-
// CHECK-NEXT: [[FIELD_OFFSET_TMP:%.*]] = load i8**, i8*** [[FIELD_OFFSET_ADDR:%.*]]
74-
// CHECK-NEXT: [[FIELD_OFFSET:%.*]] = ptrtoint i8** [[FIELD_OFFSET_TMP]] to [[INT]]
73+
// CHECK-NEXT: [[FIELD_OFFSET:%.*]] = load [[INT]], [[INT]]* [[FIELD_OFFSET_ADDR:%.*]]
7574
// CHECK-NEXT: [[OBJECT:%.*]] = bitcast %C21class_resilience_objc19GenericObjCSubclass* %0 to i8*
7675
// CHECK-NEXT: [[ADDR:%.*]] = getelementptr inbounds i8, i8* [[OBJECT]], [[INT]] [[FIELD_OFFSET]]
7776
// CHECK-NEXT: [[FIELD_ADDR:%.*]] = bitcast i8* [[ADDR]] to %Vs5Int32*

test/IRGen/generic_classes.sil

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,9 @@ entry(%c : $RootGeneric<F>):
235235

236236
// RootGeneric.y has dependent layout; load the offset from the metadata
237237
// CHECK-LABEL: define void @RootGeneric_concrete_fragile_dependent_member_access_y
238-
// CHECK: [[TYPE_METADATA_ARRAY:%.*]] = bitcast %swift.type* {{%.*}} to i8***
239-
// CHECK: [[Y_OFFSET_ADDR:%.*]] = getelementptr inbounds i8**, i8*** [[TYPE_METADATA_ARRAY]], i64 16
240-
// CHECK: [[Y_OFFSET_PTR:%.*]] = load i8**, i8*** [[Y_OFFSET_ADDR]], align 8
241-
// CHECK: [[Y_OFFSET:%.*]] = ptrtoint i8** [[Y_OFFSET_PTR]] to i64
238+
// CHECK: [[TYPE_METADATA_ARRAY:%.*]] = bitcast %swift.type* {{%.*}} to i64*
239+
// CHECK: [[Y_OFFSET_ADDR:%.*]] = getelementptr inbounds i64, i64* [[TYPE_METADATA_ARRAY]], i64 16
240+
// CHECK: [[Y_OFFSET:%.*]] = load i64, i64* [[Y_OFFSET_ADDR]], align 8
242241
// CHECK: [[CLASS_BYTE_ARRAY:%.*]] = bitcast [[ROOTGENERIC]]* {{%.*}} to i8*
243242
// CHECK: [[Y_ADDR:%.*]] = getelementptr inbounds i8, i8* [[CLASS_BYTE_ARRAY]], i64 [[Y_OFFSET]]
244243
// CHECK: bitcast i8* [[Y_ADDR]] to %swift.opaque*
@@ -251,10 +250,9 @@ entry(%z : $*F, %c : $RootGeneric<F>):
251250
}
252251

253252
// CHECK-LABEL: define void @RootGeneric_subst_concrete_fragile_dependent_member_access_y
254-
// CHECK: [[TYPE_METADATA_ARRAY:%.*]] = bitcast %swift.type* {{%.*}} to i8***
255-
// CHECK: [[Y_OFFSET_ADDR:%.*]] = getelementptr inbounds i8**, i8*** [[TYPE_METADATA_ARRAY]], i64 16
256-
// CHECK: [[Y_OFFSET_PTR:%.*]] = load i8**, i8*** [[Y_OFFSET_ADDR]], align 8
257-
// CHECK: [[Y_OFFSET:%.*]] = ptrtoint i8** [[Y_OFFSET_PTR]] to i64
253+
// CHECK: [[TYPE_METADATA_ARRAY:%.*]] = bitcast %swift.type* {{%.*}} to i64*
254+
// CHECK: [[Y_OFFSET_ADDR:%.*]] = getelementptr inbounds i64, i64* [[TYPE_METADATA_ARRAY]], i64 16
255+
// CHECK: [[Y_OFFSET:%.*]] = load i64, i64* [[Y_OFFSET_ADDR]], align 8
258256
// CHECK: [[CLASS_BYTE_ARRAY:%.*]] = bitcast [[ROOTGENERIC]]* {{%.*}} to i8*
259257
// CHECK: [[Y_ADDR:%.*]] = getelementptr inbounds i8, i8* [[CLASS_BYTE_ARRAY]], i64 [[Y_OFFSET]]
260258
// CHECK: bitcast i8* [[Y_ADDR]] to %Si*
@@ -268,10 +266,9 @@ entry(%z : $*Int, %c : $RootGeneric<Int>):
268266

269267
// RootGeneric.z has dependent layout; load the offset from the metadata
270268
// CHECK-LABEL: define i8 @RootGeneric_concrete_fragile_dependent_member_access_z
271-
// CHECK: [[TYPE_METADATA_ARRAY:%.*]] = bitcast %swift.type* {{%.*}} to i8***
272-
// CHECK: [[Z_OFFSET_ADDR:%.*]] = getelementptr inbounds i8**, i8*** [[TYPE_METADATA_ARRAY]], i64 17
273-
// CHECK: [[Z_OFFSET_PTR:%.*]] = load i8**, i8*** [[Z_OFFSET_ADDR]], align 8
274-
// CHECK: [[Z_OFFSET:%.*]] = ptrtoint i8** [[Z_OFFSET_PTR]] to i64
269+
// CHECK: [[TYPE_METADATA_ARRAY:%.*]] = bitcast %swift.type* {{%.*}} to i64*
270+
// CHECK: [[Z_OFFSET_ADDR:%.*]] = getelementptr inbounds i64, i64* [[TYPE_METADATA_ARRAY]], i64 17
271+
// CHECK: [[Z_OFFSET:%.*]] = load i64, i64* [[Z_OFFSET_ADDR]], align 8
275272
// CHECK: [[CLASS_BYTE_ARRAY:%.*]] = bitcast [[ROOTGENERIC]]* {{%.*}} to i8*
276273
// CHECK: [[Z_ADDR:%.*]] = getelementptr inbounds i8, i8* [[CLASS_BYTE_ARRAY]], i64 [[Z_OFFSET]]
277274
// CHECK: bitcast i8* [[Z_ADDR]] to %Vs5UInt8*
@@ -283,10 +280,9 @@ entry(%c : $RootGeneric<F>):
283280
}
284281

285282
// CHECK-LABEL: define i8 @RootGeneric_subst_concrete_fragile_dependent_member_access_z
286-
// CHECK: [[TYPE_METADATA_ARRAY:%.*]] = bitcast %swift.type* {{%.*}} to i8***
287-
// CHECK: [[Z_OFFSET_ADDR:%.*]] = getelementptr inbounds i8**, i8*** [[TYPE_METADATA_ARRAY]], i64 17
288-
// CHECK: [[Z_OFFSET_PTR:%.*]] = load i8**, i8*** [[Z_OFFSET_ADDR]], align 8
289-
// CHECK: [[Z_OFFSET:%.*]] = ptrtoint i8** [[Z_OFFSET_PTR]] to i64
283+
// CHECK: [[TYPE_METADATA_ARRAY:%.*]] = bitcast %swift.type* {{%.*}} to i64*
284+
// CHECK: [[Z_OFFSET_ADDR:%.*]] = getelementptr inbounds i64, i64* [[TYPE_METADATA_ARRAY]], i64 17
285+
// CHECK: [[Z_OFFSET:%.*]] = load i64, i64* [[Z_OFFSET_ADDR]], align 8
290286
// CHECK: [[CLASS_BYTE_ARRAY:%.*]] = bitcast [[ROOTGENERIC]]* {{%.*}} to i8*
291287
// CHECK: [[Z_ADDR:%.*]] = getelementptr inbounds i8, i8* [[CLASS_BYTE_ARRAY]], i64 [[Z_OFFSET]]
292288
// CHECK: bitcast i8* [[Z_ADDR]] to %Vs5UInt8*

0 commit comments

Comments
 (0)