[llvm] [llvm][ARM]Add widen strings pass (PR #107120)

David Green via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 23 09:36:54 PDT 2024


================
@@ -2029,6 +2031,129 @@ OptimizeFunctions(Module &M,
   return Changed;
 }
 
+static bool IsCharArray(Type *T) {
+  const unsigned int CHAR_BIT_SIZE = 8;
+  return T && T->isArrayTy() && T->getArrayElementType()->isIntegerTy() &&
+         T->getArrayElementType()->getIntegerBitWidth() == CHAR_BIT_SIZE;
+}
+
+static bool
+tryWidenGlobalString(CallInst *CI, GlobalVariable *SourceVar,
+                     function_ref<TargetTransformInfo &(Function &)> GetTTI,
+                     function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
+
+  auto *F = CI->getCalledFunction();
+  auto *Alloca = dyn_cast<AllocaInst>(CI->getArgOperand(0));
+  auto *BytesToCopy = dyn_cast<ConstantInt>(CI->getArgOperand(2));
+  auto *IsVolatile = dyn_cast<ConstantInt>(CI->getArgOperand(3));
+
+  if (!BytesToCopy)
+    return false;
+
+  uint64_t NumBytesToCopy = BytesToCopy->getZExtValue();
+
+  if (!Alloca)
+    return false;
+
+  if (!IsVolatile || IsVolatile->isOne())
+    return false;
+
+  if (NumBytesToCopy % 4 == 0)
+    return false;
+
+  if (!SourceVar->hasInitializer() || !SourceVar->isConstant() ||
+      !SourceVar->hasLocalLinkage() || !SourceVar->hasGlobalUnnamedAddr())
+    return false;
+
+  ConstantDataArray *SourceDataArray =
+      dyn_cast<ConstantDataArray>(SourceVar->getInitializer());
+  if (!SourceDataArray || !IsCharArray(SourceDataArray->getType()))
+    return false;
+
+  if (!Alloca->isStaticAlloca())
+    return false;
+
+  // Make sure destination is definitley a char array.
+  if (!IsCharArray(Alloca->getAllocatedType()))
+    return false;
+
+  uint64_t DZSize = Alloca->getAllocatedType()->getArrayNumElements();
+  uint64_t SZSize = SourceDataArray->getType()->getNumElements();
+
+  // For safety purposes lets add a constraint and only padd when
----------------
davemgreen wrote:

padd -> pad

https://github.com/llvm/llvm-project/pull/107120


More information about the llvm-commits mailing list