[llvm] [Hexagon] Do not promote unaligned loop access to memmove. (PR #205358)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 24 06:08:31 PDT 2026


================
@@ -229,6 +233,41 @@ InstructionCost HexagonTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
                                 OpInfo, I);
 }
 
+/* Do not promote loop access to memmove if source or destination
+ * array cannot be aligned to 8 byte boundary. memmove is beneficial
+ * only if source and destination are 8 byte aligned or memmove
+ * supports unaligned accesses. For example
+ * char a[100] ;
+ * for(i=n; i> 0; i--)
+ *     a[i] = a[i-1];
+ * We can never align source and destination simultaneously at
+ * 8 byte boundary. If we convert this loop to memmove then
+ * memmove will do alignment checks and it fails so eventually
+ * do copy byte by byte. As a result, memmove will be relatively
+ * slow (due to call overhead and checks inside memmove)
+ * as compared to the loop access for unaligned accesses.*/
+
+bool HexagonTTIImpl::isMemmoveProfitable(Value *DestPtr, Value *SourcePtr,
+                                         ScalarEvolution *SE) const {
+  if (HexagonEnableMemmove)
+    return true;
+
+  // memmove is only efficient on Hexagon when src and dst can be simultaneously
+  // 8-byte aligned. Use SE to compute the constant byte difference between the
+  // two pointers. If it is not a multiple of 8, they can never be
+  // simultaneously aligned — keep the original loop instead.
+
+  std::optional<APInt> Diff = SE->computeConstantDifference(
----------------
anilkund wrote:

 Would it make more sense for the loopIdiomRecognizePass to compute the scev exp difference and then call the TTI hook to check the alignment? 

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


More information about the llvm-commits mailing list