[llvm] [ExpandMemCmp] Improve memcmp optimisation for boolean results (PR #71221)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 6 03:24:43 PST 2023


================
@@ -656,6 +658,40 @@ Value *MemCmpExpansion::getMemCmpOneBlock() {
 
   const LoadPair Loads = getLoadPair(LoadSizeType, BSwapSizeType, MaxLoadType,
                                      /*Offset*/ 0);
+
+  // If a user of memcmp cares only about two outcomes, for example:
+  //    bool result = memcmp(a, b, NBYTES) > 0;
+  // We can generate more optimal code with a smaller number of operations
+  if (auto *U = CI->getUniqueUndroppableUser()) {
+    auto *UI = cast<Instruction>(U);
+    ICmpInst::Predicate Pred = ICmpInst::Predicate::BAD_ICMP_PREDICATE;
+    uint64_t Shift;
+    bool NeedsZExt = false;
+    // This is a special case because instead of checking if the result is less
+    // than zero:
+    //    bool result = memcmp(a, b, NBYTES) < 0;
+    // Compiler is clever enough to generate the following code:
+    //    bool result = memcmp(a, b, NBYTES) >> 31;
+    if (match(UI, m_LShr(m_Value(), m_ConstantInt(Shift))) &&
+        Shift == CI->getType()->getIntegerBitWidth() - 1) {
----------------
david-arm wrote:

Can you add brackets around `CI->getType()->getIntegerBitWidth() - 1` please? I'm sure it's correct, but I think brackets are more readable.

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


More information about the llvm-commits mailing list