Skip to content

Commit 2ebff4d

Browse files
committed
[GenTuple] Extracted dynamic tuple offset load.
Refactored the preexisting function loadTupleOffsetFromMetadata--which took an unsigned index and constructed an llvm::Value from it--to instead take a llvm::Value. Preserved the old overload to just pass through to the refactored function.
1 parent 01dc2d1 commit 2ebff4d

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lib/IRGen/GenTuple.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,37 @@ namespace {
7373
/// Project a tuple offset from a tuple metadata structure.
7474
static llvm::Value *loadTupleOffsetFromMetadata(IRGenFunction &IGF,
7575
llvm::Value *metadata,
76-
unsigned index) {
76+
llvm::Value *index) {
7777
auto asTuple = IGF.Builder.CreateBitCast(metadata,
7878
IGF.IGM.TupleTypeMetadataPtrTy);
7979

8080
llvm::Value *indices[] = {
81-
IGF.IGM.getSize(Size(0)), // (*tupleType)
82-
llvm::ConstantInt::get(IGF.IGM.Int32Ty, 3), // .Elements
83-
IGF.IGM.getSize(Size(index)), // [index]
84-
llvm::ConstantInt::get(IGF.IGM.Int32Ty, 1) // .Offset
81+
IGF.IGM.getSize(Size(0)), // (*tupleType)
82+
llvm::ConstantInt::get(IGF.IGM.Int32Ty, 3), // .Elements
83+
index, // [index]
84+
llvm::ConstantInt::get(IGF.IGM.Int32Ty, 1) // .Offset
8585
};
8686
auto slot = IGF.Builder.CreateInBoundsGEP(IGF.IGM.TupleTypeMetadataTy,
8787
asTuple, indices);
8888

89-
return IGF.Builder.CreateLoad(
90-
slot, IGF.IGM.Int32Ty, IGF.IGM.getPointerAlignment(),
91-
metadata->getName() + "." + Twine(index) + ".offset");
89+
Twine name = [&]() -> Twine {
90+
if (auto *constantIndex = dyn_cast<llvm::ConstantInt>(index)) {
91+
return metadata->getName() + "." +
92+
Twine(constantIndex->getValue().getLimitedValue()) + ".offset";
93+
} else {
94+
return metadata->getName() + ".dynamic.offset";
95+
}
96+
}();
97+
98+
return IGF.Builder.CreateLoad(slot, IGF.IGM.Int32Ty,
99+
IGF.IGM.getPointerAlignment(), name);
100+
}
101+
102+
static llvm::Value *loadTupleOffsetFromMetadata(IRGenFunction &IGF,
103+
llvm::Value *metadata,
104+
unsigned index) {
105+
return loadTupleOffsetFromMetadata(IGF, metadata,
106+
IGF.IGM.getSize(Size(index)));
92107
}
93108

94109
/// Adapter for tuple types.

0 commit comments

Comments
 (0)