Skip to content

Commit 4650b11

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 2080c3e commit 4650b11

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
@@ -6068,21 +6068,22 @@ bool llvm::canIgnoreSignBitOfNaN(const Use &U) {
60686068
}
60696069

60706070
Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
6071+
unsigned ByteWidth = DL.getByteWidth();
60716072

60726073
// All byte-wide stores are splatable, even of arbitrary variables.
6073-
if (V->getType()->isIntegerTy(8))
6074+
if (V->getType()->isIntegerTy(ByteWidth))
60746075
return V;
60756076

60766077
LLVMContext &Ctx = V->getContext();
60776078

60786079
// Undef don't care.
6079-
auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6080+
auto *UndefByte = UndefValue::get(Type::getIntNTy(Ctx, ByteWidth));
60806081
if (isa<UndefValue>(V))
6081-
return UndefInt8;
6082+
return UndefByte;
60826083

60836084
// Return poison for zero-sized type.
60846085
if (DL.getTypeStoreSize(V->getType()).isZero())
6085-
return PoisonValue::get(Type::getInt8Ty(Ctx));
6086+
return PoisonValue::get(Type::getIntNTy(Ctx, ByteWidth));
60866087

60876088
Constant *C = dyn_cast<Constant>(V);
60886089
if (!C) {
@@ -6097,7 +6098,7 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
60976098

60986099
// Handle 'null' ConstantArrayZero etc.
60996100
if (C->isNullValue())
6100-
return Constant::getNullValue(Type::getInt8Ty(Ctx));
6101+
return Constant::getNullValue(Type::getIntNTy(Ctx, ByteWidth));
61016102

61026103
// Constant floating-point values can be handled as integer values if the
61036104
// corresponding integer value is "byteable". An important case is 0.0.
@@ -6114,13 +6115,14 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
61146115
: nullptr;
61156116
}
61166117

6117-
// We can handle constant integers that are multiple of 8 bits.
6118+
// We can handle constant integers that are multiple of the byte width.
61186119
if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6119-
if (CI->getBitWidth() % 8 == 0) {
6120-
assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6121-
if (!CI->getValue().isSplat(8))
6120+
if (CI->getBitWidth() % ByteWidth == 0) {
6121+
assert(CI->getBitWidth() > ByteWidth &&
6122+
"single byte should be handled above!");
6123+
if (!CI->getValue().isSplat(ByteWidth))
61226124
return nullptr;
6123-
return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6125+
return ConstantInt::get(Ctx, CI->getValue().trunc(ByteWidth));
61246126
}
61256127
}
61266128

@@ -6140,23 +6142,23 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
61406142
return LHS;
61416143
if (!LHS || !RHS)
61426144
return nullptr;
6143-
if (LHS == UndefInt8)
6145+
if (LHS == UndefByte)
61446146
return RHS;
6145-
if (RHS == UndefInt8)
6147+
if (RHS == UndefByte)
61466148
return LHS;
61476149
return nullptr;
61486150
};
61496151

61506152
if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
6151-
Value *Val = UndefInt8;
6153+
Value *Val = UndefByte;
61526154
for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
61536155
if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
61546156
return nullptr;
61556157
return Val;
61566158
}
61576159

61586160
if (isa<ConstantAggregate>(C)) {
6159-
Value *Val = UndefInt8;
6161+
Value *Val = UndefByte;
61606162
for (Value *Op : C->operands())
61616163
if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
61626164
return nullptr;

0 commit comments

Comments
 (0)