Skip to content

Commit 3b86337

Browse files
committed
[ValueTracking] Make isBytewiseValue byte width agnostic
This is a simple change to show how easy it can be to support unusual byte widths in the middle end.
1 parent f78b4f6 commit 3b86337

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5947,21 +5947,22 @@ KnownFPClass llvm::computeKnownFPClass(const Value *V,
59475947
}
59485948

59495949
Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
5950+
unsigned ByteWidth = DL.getByteWidth();
59505951

59515952
// All byte-wide stores are splatable, even of arbitrary variables.
5952-
if (V->getType()->isIntegerTy(8))
5953+
if (V->getType()->isIntegerTy(ByteWidth))
59535954
return V;
59545955

59555956
LLVMContext &Ctx = V->getContext();
59565957

59575958
// Undef don't care.
5958-
auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
5959+
auto *UndefByte = UndefValue::get(Type::getIntNTy(Ctx, ByteWidth));
59595960
if (isa<UndefValue>(V))
5960-
return UndefInt8;
5961+
return UndefByte;
59615962

59625963
// Return Undef for zero-sized type.
59635964
if (DL.getTypeStoreSize(V->getType()).isZero())
5964-
return UndefInt8;
5965+
return UndefByte;
59655966

59665967
Constant *C = dyn_cast<Constant>(V);
59675968
if (!C) {
@@ -5976,7 +5977,7 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
59765977

59775978
// Handle 'null' ConstantArrayZero etc.
59785979
if (C->isNullValue())
5979-
return Constant::getNullValue(Type::getInt8Ty(Ctx));
5980+
return Constant::getNullValue(Type::getIntNTy(Ctx, ByteWidth));
59805981

59815982
// Constant floating-point values can be handled as integer values if the
59825983
// corresponding integer value is "byteable". An important case is 0.0.
@@ -5993,13 +5994,14 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
59935994
: nullptr;
59945995
}
59955996

5996-
// We can handle constant integers that are multiple of 8 bits.
5997+
// We can handle constant integers that are multiple of the byte width.
59975998
if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
5998-
if (CI->getBitWidth() % 8 == 0) {
5999-
assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6000-
if (!CI->getValue().isSplat(8))
5999+
if (CI->getBitWidth() % ByteWidth == 0) {
6000+
assert(CI->getBitWidth() > ByteWidth &&
6001+
"single byte should be handled above!");
6002+
if (!CI->getValue().isSplat(ByteWidth))
60016003
return nullptr;
6002-
return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6004+
return ConstantInt::get(Ctx, CI->getValue().trunc(ByteWidth));
60036005
}
60046006
}
60056007

@@ -6019,23 +6021,23 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
60196021
return LHS;
60206022
if (!LHS || !RHS)
60216023
return nullptr;
6022-
if (LHS == UndefInt8)
6024+
if (LHS == UndefByte)
60236025
return RHS;
6024-
if (RHS == UndefInt8)
6026+
if (RHS == UndefByte)
60256027
return LHS;
60266028
return nullptr;
60276029
};
60286030

60296031
if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
6030-
Value *Val = UndefInt8;
6032+
Value *Val = UndefByte;
60316033
for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
60326034
if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
60336035
return nullptr;
60346036
return Val;
60356037
}
60366038

60376039
if (isa<ConstantAggregate>(C)) {
6038-
Value *Val = UndefInt8;
6040+
Value *Val = UndefByte;
60396041
for (Value *Op : C->operands())
60406042
if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
60416043
return nullptr;

0 commit comments

Comments
 (0)