[llvm-branch-commits] [llvm] [ValueTracking] Make isBytewiseValue byte width agnostic (PR #106538)

Sergei Barannikov via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Aug 29 04:51:14 PDT 2024


https://github.com/s-barannikov created https://github.com/llvm/llvm-project/pull/106538

This is a simple change to show how easy it can be to support unusual
byte widths in the middle end.

>From 3b86337919c0f518c133f09787ddc0518ede4821 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov <barannikov88 at 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 173faa32a3878d..fb25cfcd2004ee 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5947,21 +5947,22 @@ KnownFPClass llvm::computeKnownFPClass(const Value *V,
 }
 
 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 Undef for zero-sized type.
   if (DL.getTypeStoreSize(V->getType()).isZero())
-    return UndefInt8;
+    return UndefByte;
 
   Constant *C = dyn_cast<Constant>(V);
   if (!C) {
@@ -5976,7 +5977,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.
@@ -5993,13 +5994,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));
     }
   }
 
@@ -6019,15 +6021,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 (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
       if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
         return nullptr;
@@ -6035,7 +6037,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;



More information about the llvm-branch-commits mailing list