[llvm] [llvm][ARM]Add widen strings pass (PR #107120)
Nashe Mncube via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 2 06:20:41 PDT 2024
================
@@ -2029,6 +2031,109 @@ 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,
+ unsigned NumBytesToPad,
+ unsigned NumBytesToCopy,
+ ConstantInt *BytesToCopyOp) {
+ 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() || !IsCharArray(Alloca->getAllocatedType()))
+ return false;
+
+ ConstantDataArray *SourceDataArray =
+ dyn_cast<ConstantDataArray>(SourceVar->getInitializer());
+ if (!SourceDataArray || !IsCharArray(SourceDataArray->getType()))
+ return false;
+
+ uint64_t DZSize = Alloca->getAllocatedType()->getArrayNumElements();
+ uint64_t SZSize = SourceDataArray->getType()->getNumElements();
+
+ // For safety purposes lets add a constraint and only pad when
+ // num bytes to copy == destination array size == source string
+ // which is a constant
+ if (NumBytesToCopy != DZSize || DZSize != SZSize)
+ return false;
+
+ unsigned int TotalBytes = NumBytesToCopy + NumBytesToPad;
+
+ // Update destination char array to be word aligned (memcpy(X,...,...))
+ IRBuilder<> BuildAlloca(Alloca);
+ AllocaInst *NewAlloca = BuildAlloca.CreateAlloca(ArrayType::get(
+ Alloca->getAllocatedType()->getArrayElementType(), TotalBytes));
+ NewAlloca->takeName(Alloca);
+ NewAlloca->setAlignment(Alloca->getAlign());
+ Alloca->replaceAllUsesWith(NewAlloca);
----------------
nasherm wrote:
Done
https://github.com/llvm/llvm-project/pull/107120
More information about the llvm-commits
mailing list