[PATCH] D101423: [InstCombine] Fold overflow bit of [u|s]mul.with.overflow in a poison-safe way

Juneyoung Lee via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 27 19:42:19 PDT 2021


aqjune created this revision.
aqjune added reviewers: spatel, nikic, lebedev.ri.
Herald added a subscriber: hiraditya.
aqjune requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

As discussed in D101191 <https://reviews.llvm.org/D101191>, this patch adds a poison-safe folding of overflow bit check:

    %Op0 = icmp ne i4 %X, 0
    %Agg = call { i4, i1 } @llvm.[us]mul.with.overflow.i4(i4 %X, i4 %Y)
    %Op1 = extractvalue { i4, i1 } %Agg, 1
    %ret = select i1 %Op0, i1 %Op1, i1 false
  =>
    %Y.fr = freeze %Y
    %Agg = call { i4, i1 } @llvm.[us]mul.with.overflow.i4(i4 %X, i4 %Y.fr)
    %Op1 = extractvalue { i4, i1 } %Agg, 1
    %ret = %Op1

https://alive2.llvm.org/ce/z/zgPUGT
https://alive2.llvm.org/ce/z/h2gZ_6

Note that there are cases where inserting freeze is not necessary: e.g. %Y is `noundef`.
In this case, LLVM is already good because `%ret` is already successfully folded into `and`,
triggering the pre-existing optimization in InstSimplify: https://godbolt.org/z/v6qena15K


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101423

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/PhaseOrdering/unsigned-multiply-overflow-check.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101423.341062.patch
Type: text/x-patch
Size: 5922 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210428/d94c140c/attachment.bin>


More information about the llvm-commits mailing list