[InstCombine] Fold overflow bit of [u|s]mul.with.overflow in a poison-safe way
authorJuneyoung Lee <aqjune@gmail.com>
Wed, 28 Apr 2021 02:34:45 +0000 (11:34 +0900)
committerJuneyoung Lee <aqjune@gmail.com>
Sun, 2 May 2021 02:54:12 +0000 (11:54 +0900)
commit1977c53b2ae425541a0ef329ca10cc8d5cacd0cd
tree8f5a1a4807b230b9539cca1cc638f896de325e8a
parent603ae6082bcb9e7b8ae9f288c007488f144d9d22
[InstCombine] Fold overflow bit of [u|s]mul.with.overflow in a poison-safe way

As discussed in 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

Differential Revision: https://reviews.llvm.org/D101423
llvm/include/llvm/Analysis/OverflowInstAnalysis.h [new file with mode: 0644]
llvm/lib/Analysis/CMakeLists.txt
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/lib/Analysis/OverflowInstAnalysis.cpp [new file with mode: 0644]
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
llvm/test/Transforms/InstCombine/div-by-0-guard-before-smul_ov-not.ll
llvm/test/Transforms/InstCombine/div-by-0-guard-before-smul_ov.ll
llvm/test/Transforms/InstCombine/div-by-0-guard-before-umul_ov-not.ll
llvm/test/Transforms/InstCombine/div-by-0-guard-before-umul_ov.ll