Skip to content

Commit 410bf5e

Browse files
committed
[InstCombine] Use disjoint flag in mul of or fold
Slightly more powerful if the information used to infer disjoint was lost.
1 parent 056367b commit 410bf5e

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,8 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
300300
// Canonicalize (X|C1)*MulC -> X*MulC+C1*MulC.
301301
Value *X;
302302
Constant *C1;
303-
if ((match(Op0, m_OneUse(m_Add(m_Value(X), m_ImmConstant(C1))))) ||
304-
(match(Op0, m_OneUse(m_Or(m_Value(X), m_ImmConstant(C1)))) &&
305-
haveNoCommonBitsSet(X, C1, SQ.getWithInstruction(&I)))) {
303+
if (match(Op0, m_OneUse(m_Add(m_Value(X), m_ImmConstant(C1)))) ||
304+
match(Op0, m_OneUse(m_DisjointOr(m_Value(X), m_ImmConstant(C1))))) {
306305
// C1*MulC simplifies to a tidier constant.
307306
Value *NewC = Builder.CreateMul(C1, MulC);
308307
auto *BOp0 = cast<BinaryOperator>(Op0);

llvm/test/Transforms/InstCombine/mul.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,28 @@ define i32 @PR57278_mul_assume(i32 %a) {
964964

965965
declare void @llvm.assume(i1)
966966

967+
define i32 @PR57278_or_disjoint_nuw(i32 %a) {
968+
; CHECK-LABEL: @PR57278_or_disjoint_nuw(
969+
; CHECK-NEXT: [[TMP1:%.*]] = mul nuw i32 [[A:%.*]], 3
970+
; CHECK-NEXT: [[MUL:%.*]] = add nuw i32 [[TMP1]], 9
971+
; CHECK-NEXT: ret i32 [[MUL]]
972+
;
973+
%add = or disjoint i32 %a, 3
974+
%mul = mul nuw i32 %add, 3
975+
ret i32 %mul
976+
}
977+
978+
define i32 @PR57278_or_disjoint_nsw(i32 %a) {
979+
; CHECK-LABEL: @PR57278_or_disjoint_nsw(
980+
; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[A:%.*]], 3
981+
; CHECK-NEXT: [[MUL:%.*]] = add i32 [[TMP1]], 9
982+
; CHECK-NEXT: ret i32 [[MUL]]
983+
;
984+
%add = or disjoint i32 %a, 3
985+
%mul = mul nsw i32 %add, 3
986+
ret i32 %mul
987+
}
988+
967989
; https://alive2.llvm.org/ce/z/XYpv9q
968990
define <2 x i32> @PR57278_shl_vec(<2 x i32> %v1) {
969991
; CHECK-LABEL: @PR57278_shl_vec(

0 commit comments

Comments
 (0)