[PATCH] D75840: [ExpandMemCmp] Improve non-equality comparisons with zero.

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 11 10:10:37 PDT 2020


spatel added inline comments.


================
Comment at: llvm/lib/CodeGen/ExpandMemCmp.cpp:370-374
+  case CmpInst::ICMP_SLT:
+  case CmpInst::ICMP_SGT:
+  case CmpInst::ICMP_SLE:
+  case CmpInst::ICMP_SGE:
+    return getCompareLoadPairsForZeroRelational(BlockIndex, LoadIndex);
----------------
This is not correct. memcmp() compares as unsigned bytes:
http://www.cplusplus.com/reference/cstring/memcmp/

This program will show a failure compiled with -O1 vs -O0 (do we have some kind of correctness test like this in the test-suite?):


```
#include <string.h>
#include <stdio.h>

void wrapper(char *c, char *d) {
  if (memcmp(c, d, 4) > 0)
    printf(">0\n");
  else
    printf("<=0\n");
}

int main() {
  int A = 0x00000000;
  int B = 0x00000080;
  wrapper(&A, &B);
  return 0;
}

```


================
Comment at: llvm/test/CodeGen/X86/memcmp-more-load-pairs.ll:128
+; X86-NEXT:    cmpw %ax, %cx
+; X86-NEXT:    setl %al
 ; X86-NEXT:    retl
----------------
I think that's a miscompile...should be "setb"?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75840/new/

https://reviews.llvm.org/D75840





More information about the llvm-commits mailing list