[PATCH] D65765: [InstCombine] Non-canonical clamp-like pattern handling

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 5 11:34:26 PDT 2019


lebedev.ri created this revision.
lebedev.ri added reviewers: spatel, nikic, xbolva00, dmgreen.
lebedev.ri added a project: LLVM.
Herald added a subscriber: hiraditya.
lebedev.ri added a parent revision: D65530: [InstCombine] foldXorOfICmps(): don't give up on non-single-use ICmp's if all users are freely invertible.

Given a pattern like:

  %old_cmp1 = icmp slt i32 %x, C2
  %old_replacement = select i1 %old_cmp1, i32 %target_low, i32 %target_high
  %old_x_offseted = add i32 %x, C1
  %old_cmp0 = icmp ult i32 %old_x_offseted, C0
  %r = select i1 %old_cmp0, i32 %x, i32 %old_replacement

it can be rewritten as more canonical pattern:

  %new_cmp1 = icmp slt i32 %x, -C1
  %new_cmp2 = icmp sge i32 %x, C0-C1
  %new_clamped_low = select i1 %new_cmp1, i32 %target_low, i32 %x
  %r = select i1 %new_cmp2, i32 %target_high, i32 %new_clamped_low

Iff `-C1 s<= C2 s<= C0-C1`
Also, `ULT` predicate can also be `UGE`; or `UGT` iff `C0 != -1` (+invert result)
Also, `SLT` predicate can also be `SGE`; or `SGT` iff `C2 != INT_MAX` (+invert result)

If `C1 == 0`, then all 3 instructions must be one-use; else at most either `%old_cmp1` or `%old_x_offseted` can have extra uses.

NOTE: if we could reuse `%old_cmp1` as one of the comparisons we'll have to build, this could be less limiting.

So there are two icmp's, each one with 3 predicate variants, so there are 9 fold variants:

|     | ULT                            | UGE                             | UGT                             |
| SLT | https://rise4fun.com/Alive/yIJ | https://rise4fun.com/Alive/5BfN | https://rise4fun.com/Alive/INH  |
| SGE | https://rise4fun.com/Alive/hd8 | https://rise4fun.com/Alive/Abk  | https://rise4fun.com/Alive/PlzS |
| SGT | https://rise4fun.com/Alive/VYG | https://rise4fun.com/Alive/oMY  | https://rise4fun.com/Alive/KrzC |
|

F9730206: proof.opt <https://reviews.llvm.org/F9730206>

This fold was brought up in https://reviews.llvm.org/D65148#1603922 by @dmgreen, and is needed to unblock that patch.
This patch requires D65530 <https://reviews.llvm.org/D65530>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65765

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/InstCombine/canonicalize-clamp-like-pattern-between-negative-and-positive-thresholds.ll
  llvm/test/Transforms/InstCombine/canonicalize-clamp-like-pattern-between-zero-and-positive-threshold.ll
  llvm/test/Transforms/InstCombine/canonicalize-clamp-with-select-of-constant-threshold-pattern.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65765.213409.patch
Type: text/x-patch
Size: 37385 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190805/0ede6f0b/attachment.bin>


More information about the llvm-commits mailing list