llvm / clangir

A new (MLIR based) high-level IR for clang.
https://clangir.org
Other
307 stars 84 forks source link

[CIR] Introduce CIR simplification #696

Open gitoleg opened 2 weeks ago

gitoleg commented 2 weeks ago

This PR introduce cir simplification pass. The idea is to have a pass for the redundant operations removal/update.

Right now two pattern implemented, both related to the redundant bool operations. First pattern removes redundant casts from bool to int and back that for some reasons appear in the code. Second pattern removes sequential unary not operations (!!) .

For example, the code from the test is expanded from the simple check that is common for C code:

#define CHECK_PTR(ptr)  \
  do {                   \
    if (__builtin_expect((!!((ptr) == 0)), 0))\
      return -42; \
  } while(0)

I mark this PR as a draft for the following reasons: 1) I have no idea if it's useful for the community 2) There is a test fail - unfortunately current pattern rewriter run DCE underneath the hood and looks like we can't workaround it. It's enough just to add an operation to the list - in this case UnaryOp - and call applyOpPatternsAndFold. I could rewrite a test a little in order to make everything non dead or implement a simple fix point algorithm for the particular task (I would do the former).

bcardosolopes commented 1 week ago

Sorry I didn't had the time to look just yet, but should do that soon, can you update post rebase?