https://github.com/s-barannikov updated https://github.com/llvm/llvm-project/pull/106538
>From 3036dfc0ddfad4aeee1b5bd23627ad1949875489 Mon Sep 17 00:00:00 2001 From: Sergei Barannikov <baranniko...@gmail.com> Date: Fri, 2 Aug 2024 13:14:49 +0300 Subject: [PATCH] [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. --- llvm/lib/Analysis/ValueTracking.cpp | 30 +++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index cdf7f052943c8..2007a9599114c 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -6348,21 +6348,22 @@ std::optional<bool> llvm::computeKnownFPSignBit(const Value *V, unsigned Depth, } Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { + unsigned ByteWidth = DL.getByteWidth(); // All byte-wide stores are splatable, even of arbitrary variables. - if (V->getType()->isIntegerTy(8)) + if (V->getType()->isIntegerTy(ByteWidth)) return V; LLVMContext &Ctx = V->getContext(); // Undef don't care. - auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx)); + auto *UndefByte = UndefValue::get(Type::getIntNTy(Ctx, ByteWidth)); if (isa<UndefValue>(V)) - return UndefInt8; + return UndefByte; // Return poison for zero-sized type. if (DL.getTypeStoreSize(V->getType()).isZero()) - return PoisonValue::get(Type::getInt8Ty(Ctx)); + return PoisonValue::get(Type::getIntNTy(Ctx, ByteWidth)); Constant *C = dyn_cast<Constant>(V); if (!C) { @@ -6377,7 +6378,7 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { // Handle 'null' ConstantArrayZero etc. if (C->isNullValue()) - return Constant::getNullValue(Type::getInt8Ty(Ctx)); + return Constant::getNullValue(Type::getIntNTy(Ctx, ByteWidth)); // Constant floating-point values can be handled as integer values if the // corresponding integer value is "byteable". An important case is 0.0. @@ -6394,13 +6395,14 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { : nullptr; } - // We can handle constant integers that are multiple of 8 bits. + // We can handle constant integers that are multiple of the byte width. if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { - if (CI->getBitWidth() % 8 == 0) { - assert(CI->getBitWidth() > 8 && "8 bits should be handled above!"); - if (!CI->getValue().isSplat(8)) + if (CI->getBitWidth() % ByteWidth == 0) { + assert(CI->getBitWidth() > ByteWidth && + "single byte should be handled above!"); + if (!CI->getValue().isSplat(ByteWidth)) return nullptr; - return ConstantInt::get(Ctx, CI->getValue().trunc(8)); + return ConstantInt::get(Ctx, CI->getValue().trunc(ByteWidth)); } } @@ -6420,15 +6422,15 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { return LHS; if (!LHS || !RHS) return nullptr; - if (LHS == UndefInt8) + if (LHS == UndefByte) return RHS; - if (RHS == UndefInt8) + if (RHS == UndefByte) return LHS; return nullptr; }; if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) { - Value *Val = UndefInt8; + Value *Val = UndefByte; for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I) if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL)))) return nullptr; @@ -6436,7 +6438,7 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { } if (isa<ConstantAggregate>(C)) { - Value *Val = UndefInt8; + Value *Val = UndefByte; for (Value *Op : C->operands()) if (!(Val = Merge(Val, isBytewiseValue(Op, DL)))) return nullptr; _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits