Skip to content

Commit 555e531

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 97cce99 commit 555e531

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

llvm/lib/Analysis/ValueTracking.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -6319,21 +6319,22 @@ std::optional<bool> llvm::computeKnownFPSignBit(const Value *V, unsigned Depth,
63196319
}
63206320

63216321
Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
6322+
unsigned ByteWidth = DL.getByteWidth();
63226323

63236324
// All byte-wide stores are splatable, even of arbitrary variables.
6324-
if (V->getType()->isIntegerTy(8))
6325+
if (V->getType()->isIntegerTy(ByteWidth))
63256326
return V;
63266327

63276328
LLVMContext &Ctx = V->getContext();
63286329

63296330
// Undef don't care.
6330-
auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6331+
auto *UndefByte = UndefValue::get(Type::getIntNTy(Ctx, ByteWidth));
63316332
if (isa<UndefValue>(V))
6332-
return UndefInt8;
6333+
return UndefByte;
63336334

63346335
// Return poison for zero-sized type.
63356336
if (DL.getTypeStoreSize(V->getType()).isZero())
6336-
return PoisonValue::get(Type::getInt8Ty(Ctx));
6337+
return PoisonValue::get(Type::getIntNTy(Ctx, ByteWidth));
63376338

63386339
Constant *C = dyn_cast<Constant>(V);
63396340
if (!C) {
@@ -6348,7 +6349,7 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
63486349

63496350
// Handle 'null' ConstantArrayZero etc.
63506351
if (C->isNullValue())
6351-
return Constant::getNullValue(Type::getInt8Ty(Ctx));
6352+
return Constant::getNullValue(Type::getIntNTy(Ctx, ByteWidth));
63526353

63536354
// Constant floating-point values can be handled as integer values if the
63546355
// corresponding integer value is "byteable". An important case is 0.0.
@@ -6365,13 +6366,14 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
63656366
: nullptr;
63666367
}
63676368

6368-
// We can handle constant integers that are multiple of 8 bits.
6369+
// We can handle constant integers that are multiple of the byte width.
63696370
if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6370-
if (CI->getBitWidth() % 8 == 0) {
6371-
assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6372-
if (!CI->getValue().isSplat(8))
6371+
if (CI->getBitWidth() % ByteWidth == 0) {
6372+
assert(CI->getBitWidth() > ByteWidth &&
6373+
"single byte should be handled above!");
6374+
if (!CI->getValue().isSplat(ByteWidth))
63736375
return nullptr;
6374-
return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6376+
return ConstantInt::get(Ctx, CI->getValue().trunc(ByteWidth));
63756377
}
63766378
}
63776379

@@ -6391,23 +6393,23 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
63916393
return LHS;
63926394
if (!LHS || !RHS)
63936395
return nullptr;
6394-
if (LHS == UndefInt8)
6396+
if (LHS == UndefByte)
63956397
return RHS;
6396-
if (RHS == UndefInt8)
6398+
if (RHS == UndefByte)
63976399
return LHS;
63986400
return nullptr;
63996401
};
64006402

64016403
if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
6402-
Value *Val = UndefInt8;
6404+
Value *Val = UndefByte;
64036405
for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
64046406
if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
64056407
return nullptr;
64066408
return Val;
64076409
}
64086410

64096411
if (isa<ConstantAggregate>(C)) {
6410-
Value *Val = UndefInt8;
6412+
Value *Val = UndefByte;
64116413
for (Value *Op : C->operands())
64126414
if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
64136415
return nullptr;

0 commit comments

Comments
 (0)