[llvm] [LoopIdiom] Don't generate `wcslen` when the pointer passed is not known to be aligned (PR #229074)

Henry Jiang via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 7 00:16:27 PDT 2026


=?utf-8?q?Ömer_Sinan_Ağacan?= <omer at osa1.net>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/229074 at github.com>


================
@@ -2114,10 +2114,18 @@ class StrlenVerifier {
       return false;
     if (StepSize != OpWidth / 8)
       return false;
-    if (OpWidth >= 16)
+    if (OpWidth >= 16) {
       if (OpWidth != WcharSize * 8)
         return false;
 
+      // Don't call `wcslen` if the base pointer isn't known to be aligned.
+      const DataLayout &DL = LoopLoad->getDataLayout();
+      Align WcharAlign = DL.getABITypeAlign(OperandType);
+      if (LoopLoad->getAlign() < WcharAlign &&
+          SE->getMinTrailingZeros(LoadBaseEv) < Log2(WcharAlign))
+        return false;
----------------
mustartt wrote:

> Could you explain why this is guarded within integer-size-of-LoopLoad != 8? 

`i8` already has a minimum alignment of 1, so introducing a `strlen` for char would be legal. This is only needed for `wchar_t` that might be underaligned to the ABI requirement.

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


More information about the llvm-commits mailing list