[PATCH] D125717: [InstCombine] Optimize and of icmps with power-of-2 and contiguous masks

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 25 17:38:39 PDT 2023


goldstein.w.n added inline comments.


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp:484
 
+/// Try to fold (icmp(A & B) != 0) & (icmp(A & D) == E), where B is a contiguous
+/// set of ones starting from the most significant bit (negative power of 2), D
----------------
Can you be explicit about what you are transforming this into (ult).

Also shouldn't it be `(icmp (A & B) == 0) & (icmp (A & D) != E)`?


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp:489
+static Value *foldLogOpOfMaskedICmpsAllZerosBMaskNeqMixedOrContiguous(
+    bool IsAnd, Value *A, Value *B, Value *C, Value *D, Value *E,
+    ICmpInst::Predicate PredL, ICmpInst::Predicate PredR,
----------------
`C` is unused AFAICT.


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp:494
+         "Expected equality predicates for masked type of icmps.");
+  if (!IsAnd || PredL != ICmpInst::ICMP_EQ || PredR != ICmpInst::ICMP_NE)
+    return nullptr;
----------------
As a TODO (or here if you want to) you could also do:
```
`(icmp (A & B) == B) & (icmp (A & D) == D)` -> `(icmp (A & (B | D) == (B | D)`
```
https://alive2.llvm.org/ce/z/sFcvJP


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp:543
+          match(EElt, m_APInt(ECst)) && *DCst == *ECst &&
+          (isa<UndefValue>(BElt) ||
+           (BCst->countLeadingOnes() == DCst->countLeadingZeros()))) {
----------------
Can you add some tests with `undef` elements.

Also if `undef` is allowed in the non-vec case as well, this check and the scalar one could be done with a lambda. 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D125717



More information about the llvm-commits mailing list