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

David Green via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 3 02:05:49 PDT 2024


================
@@ -2029,6 +2031,115 @@ OptimizeFunctions(Module &M,
   return Changed;
 }
 
+static bool tryWidenGlobalArray(CallInst *CI, GlobalVariable *SourceVar,
+                                unsigned NumBytesToPad, unsigned NumBytesToCopy,
+                                ConstantInt *BytesToCopyOp,
+                                ConstantDataArray *SourceDataArray) {
+  auto *F = CI->getCalledFunction();
+  auto *Alloca = dyn_cast<AllocaInst>(CI->getArgOperand(0));
+  auto *IsVolatile = dyn_cast<ConstantInt>(CI->getArgOperand(3));
+
+  if (!Alloca || !IsVolatile || IsVolatile->isOne())
+    return false;
+
+  if (!SourceVar->hasInitializer() || !SourceVar->isConstant() ||
+      !SourceVar->hasLocalLinkage() || !SourceVar->hasGlobalUnnamedAddr())
+    return false;
+
+  if (!Alloca->isStaticAlloca())
+    return false;
+
+  uint64_t DZSize = Alloca->getAllocatedType()->getArrayNumElements();
+  uint64_t SZSize = SourceDataArray->getType()->getNumElements();
+  unsigned ElementByteWidth = SourceDataArray->getElementByteSize();
+  // Calculate the number of elements to copy while avoiding floored
+  // division of integers returning wrong values i.e. copying one byte
+  // from an array of i16 would yield 0 elements to copy as supposed to 1.
+  unsigned NumElementsToCopy =
+      (NumBytesToCopy + ElementByteWidth - 1) / ElementByteWidth;
----------------
davemgreen wrote:

Could this use divideCeil?

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


More information about the llvm-commits mailing list