Skip to content

[IRGen] Simplify constant occupied/spare bit interleaving #24978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 17 additions & 46 deletions lib/IRGen/GenEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3575,12 +3575,8 @@ namespace {
if (CommonSpareBits.empty())
return APInt();

APInt v = interleaveSpareBits(IGM, PayloadTagBits,
PayloadTagBits.size(),
tag, 0);
v |= interleaveSpareBits(IGM, CommonSpareBits,
CommonSpareBits.size(),
0, tagIndex);
APInt v = scatterBits(PayloadTagBits.asAPInt(), tag);
v |= scatterBits(~CommonSpareBits.asAPInt(), tagIndex);
return v;
}

Expand Down Expand Up @@ -4125,9 +4121,7 @@ namespace {
// If we have spare bits, pack tag bits into them.
unsigned numSpareBits = PayloadTagBits.count();
if (numSpareBits > 0) {
APInt tagMaskVal
= interleaveSpareBits(IGM, PayloadTagBits,
PayloadTagBits.size(), tag, 0);
APInt tagMaskVal = scatterBits(PayloadTagBits.asAPInt(), tag);
payload.emitApplyOrMask(IGF, tagMaskVal);
}

Expand Down Expand Up @@ -4801,9 +4795,7 @@ namespace {
// enum containing this enum as a payload. Single payload layout
// unfortunately assumes that tagging the payload case is a no-op.
auto spareBitMask = ~CommonSpareBits.asAPInt();
APInt tagBitMask
= interleaveSpareBits(IGM, PayloadTagBits, PayloadTagBits.size(),
spareTagBits, 0);
APInt tagBitMask = scatterBits(PayloadTagBits.asAPInt(), spareTagBits);

payload.emitApplyAndMask(IGF, spareBitMask);
payload.emitApplyOrMask(IGF, tagBitMask);
Expand Down Expand Up @@ -5315,8 +5307,8 @@ namespace {
auto payloadTagMask = payloadBitCount >= 32
? ~0u : (1 << payloadBitCount) - 1;
auto payloadPart = mask & payloadTagMask;
auto payloadBits = interleaveSpareBits(IGM, CommonSpareBits,
bits, payloadPart, 0);
auto payloadBits = scatterBits(CommonSpareBits.asAPInt().zextOrTrunc(bits),
payloadPart);
if (getExtraTagBitCountForExtraInhabitants() > 0) {
auto extraBits = APInt(bits,
(mask >> payloadBitCount) & extraTagMask)
Expand Down Expand Up @@ -6916,41 +6908,20 @@ llvm::Value *irgen::emitScatterBits(IRGenFunction &IGF,
return result;
}

/// Interleave the occupiedValue and spareValue bits, taking a bit from one
/// or the other at each position based on the spareBits mask.
APInt
irgen::interleaveSpareBits(IRGenModule &IGM, const SpareBitVector &spareBits,
unsigned bits,
unsigned spareValue, unsigned occupiedValue) {
// FIXME: endianness.
SmallVector<llvm::APInt::WordType, 2> valueParts;
valueParts.push_back(0);

llvm::APInt::WordType valueBit = 1;
auto advanceValueBit = [&]{
valueBit <<= 1;
if (valueBit == 0) {
valueParts.push_back(0);
valueBit = 1;
/// Unpack bits from the low bits of an integer value and
/// move them to the bit positions indicated by the mask.
llvm::APInt irgen::scatterBits(const llvm::APInt &mask, unsigned value) {
llvm::APInt result(mask.getBitWidth(), 0);
for (unsigned i = 0; i < mask.getBitWidth() && value != 0; ++i) {
if (!mask[i]) {
continue;
}
};

for (unsigned i = 0, e = spareBits.size();
(occupiedValue || spareValue) && i < e;
++i, advanceValueBit()) {
if (spareBits[i]) {
if (spareValue & 1)
valueParts.back() |= valueBit;
spareValue >>= 1;
} else {
if (occupiedValue & 1)
valueParts.back() |= valueBit;
occupiedValue >>= 1;
if (value & 1) {
result.setBit(i);
}
value >>= 1;
}

// Create the value.
return llvm::APInt(bits, valueParts);
return result;
}

/// A version of the above where the tag value is dynamic.
Expand Down
13 changes: 6 additions & 7 deletions lib/IRGen/GenEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,7 @@ void emitStoreEnumTagToAddress(IRGenFunction &IGF,
Address enumAddr,
EnumElementDecl *theCase);

/// Interleave the occupiedValue and spareValue bits, taking a bit from one
/// or the other at each position based on the spareBits mask.
APInt
interleaveSpareBits(IRGenModule &IGM, const SpareBitVector &spareBits,
unsigned bits, unsigned spareValue, unsigned occupiedValue);

/// A version of the above where the tag value is dynamic.
/// Unpack bits from value and scatter them into the masked bits.
EnumPayload interleaveSpareBits(IRGenFunction &IGF,
const EnumPayloadSchema &schema,
const SpareBitVector &spareBitVector,
Expand All @@ -111,6 +105,7 @@ llvm::Value *emitGatherBits(IRGenFunction &IGF,
llvm::Value *source,
unsigned resultLowBit,
unsigned resultBitWidth);

/// Unpack bits from the low bits of an integer value and
/// move them to the bit positions indicated by the mask.
/// Equivalent to a parallel bit deposit instruction (PDEP),
Expand All @@ -119,6 +114,10 @@ llvm::Value *emitScatterBits(IRGenFunction &IGF,
llvm::APInt mask,
llvm::Value *packedBits,
unsigned packedLowBit);

/// Unpack bits from the low bits of an integer value and
/// move them to the bit positions indicated by the mask.
llvm::APInt scatterBits(const llvm::APInt &mask, unsigned value);

/// An implementation strategy for an enum, which handles how the enum is
/// laid out and how to perform TypeInfo operations on values of the enum.
Expand Down
5 changes: 4 additions & 1 deletion lib/IRGen/GenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ FixedTypeInfo::getSpareBitFixedExtraInhabitantValue(IRGenModule &IGM,
spareIndex = (index >> occupiedBitCount) + 1;
}

return interleaveSpareBits(IGM, SpareBits, bits, spareIndex, occupiedIndex);
APInt mask = SpareBits.asAPInt().zextOrTrunc(bits);
APInt v = scatterBits(mask, spareIndex);
v |= scatterBits(~mask, occupiedIndex);
return v;
}

llvm::Value *
Expand Down