[llvm-dev] PatternMatch Commutatively?

Zhang via llvm-dev llvm-dev at lists.llvm.org
Thu Sep 23 02:49:19 PDT 2021


Hi:


I was trying to match the following pattern:


```
...
%8 = load i64, i64* %3, align 8
%9 = xor i64 %8, -1
...
%11 = or i64 %9, %10
...

```


which is roughly (~X | Y).


Given Or is commutative, I was under the assumption that trying to match (X | ~Y) would match (X | ~Y) and (~X | Y), but from testing with LLVM 11 it seems I had to match the pattern twice:


```
if (match(bo, m_Or(m_Value(X), m_Not(m_Value(Y))))) {
  // X | (~Y)
  ....
} else if (match(bo, m_Or(m_Not(m_Value(X)), m_Value(Y)))) {
  // (~X) | Y
  ....
}

```


and only the second code path is used.






Going through IR/PatternMatch.h , it seems that while the template BinaryOp_match has an optional argument Commutable, it defaults to false and none of m_Or, m_Xor or m_And seem to set it to true.


What am I misunderstanding here?




Zhang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20210923/db7b0c22/attachment.html>


More information about the llvm-dev mailing list