[llvm] [ExpandMemCmp] Check misaligned access per overlapping/tail load (PR #210707)

Pengcheng Wang via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 22 21:46:30 PDT 2026


================
@@ -127,31 +130,50 @@ class MemCmpExpansion {
   LoadPair getLoadPair(Type *LoadSizeType, Type *BSwapSizeType,
                        Type *CmpSizeType, unsigned OffsetBytes);
 
+  // Return true if a load of `LoadSize` bytes at `Offset` from the base
+  // pointers is accessible on the target: either it is naturally aligned given
+  // the known common base alignment, or the target allows a misaligned access
+  // of that width.
+  bool isAccessAllowed(unsigned LoadSize, uint64_t Offset) const;
+
   static LoadEntryVector
   computeGreedyLoadSequence(uint64_t Size, llvm::ArrayRef<unsigned> LoadSizes,
                             unsigned MaxNumLoads, unsigned &NumLoadsNonOneByte);
-  static LoadEntryVector
+  LoadEntryVector
   computeOverlappingLoadSequence(uint64_t Size, unsigned MaxLoadSize,
                                  unsigned MaxNumLoads,
-                                 unsigned &NumLoadsNonOneByte);
+                                 unsigned &NumLoadsNonOneByte) const;
 
-  static void optimiseLoadSequence(
+  void optimiseLoadSequence(
       LoadEntryVector &LoadSequence,
       const TargetTransformInfo::MemCmpExpansionOptions &Options,
-      bool IsUsedForZeroCmp);
+      bool IsUsedForZeroCmp) const;
 
 public:
   MemCmpExpansion(CallInst *CI, uint64_t Size,
                   const TargetTransformInfo::MemCmpExpansionOptions &Options,
                   const bool IsUsedForZeroCmp, const DataLayout &TheDataLayout,
-                  DomTreeUpdater *DTU);
+                  DomTreeUpdater *DTU, const TargetTransformInfo &TTI);
 
   unsigned getNumBlocks();
   uint64_t getNumLoads() const { return LoadSequence.size(); }
 
   Value *getMemCmpExpansion();
 };
 
+// We query whether the access is *allowed*, not whether it is *fast*, matching
+// the historical behavior of forming unaligned loads whenever the target
+// permits them.
+bool MemCmpExpansion::isAccessAllowed(unsigned LoadSize,
+                                      uint64_t Offset) const {
+  Align AccessAlign = commonAlignment(BaseAlign, Offset);
+  if (AccessAlign >= LoadSize)
+    return true;
----------------
wangpc-pp wrote:

I am wrong. When RVV exists, we insert non-power-of-two load sizes (9, 10, 11…) into `LoadSizes`, which reach `isAccessAllowed` via the `erase_if` filter. And then `Align(9)` asserts. So we can't use `isAligned` here. See https://github.com/llvm/llvm-project/pull/210707/commits/579a3e26607cd04e6685f47b8f84e13cf3a4207e.

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


More information about the llvm-commits mailing list