Skip to content

Commit

Permalink
[InstCombine] Use disjoint flag in mul of or fold
Browse files Browse the repository at this point in the history
Slightly more powerful if the information used to infer disjoint
was lost.
  • Loading branch information
nikic committed Dec 5, 2023
1 parent 056367b commit 410bf5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 2 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,8 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
// Canonicalize (X|C1)*MulC -> X*MulC+C1*MulC.
Value *X;
Constant *C1;
if ((match(Op0, m_OneUse(m_Add(m_Value(X), m_ImmConstant(C1))))) ||
(match(Op0, m_OneUse(m_Or(m_Value(X), m_ImmConstant(C1)))) &&
haveNoCommonBitsSet(X, C1, SQ.getWithInstruction(&I)))) {
if (match(Op0, m_OneUse(m_Add(m_Value(X), m_ImmConstant(C1)))) ||
match(Op0, m_OneUse(m_DisjointOr(m_Value(X), m_ImmConstant(C1))))) {
// C1*MulC simplifies to a tidier constant.
Value *NewC = Builder.CreateMul(C1, MulC);
auto *BOp0 = cast<BinaryOperator>(Op0);
Expand Down
22 changes: 22 additions & 0 deletions llvm/test/Transforms/InstCombine/mul.ll
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,28 @@ define i32 @PR57278_mul_assume(i32 %a) {

declare void @llvm.assume(i1)

define i32 @PR57278_or_disjoint_nuw(i32 %a) {
; CHECK-LABEL: @PR57278_or_disjoint_nuw(
; CHECK-NEXT: [[TMP1:%.*]] = mul nuw i32 [[A:%.*]], 3
; CHECK-NEXT: [[MUL:%.*]] = add nuw i32 [[TMP1]], 9
; CHECK-NEXT: ret i32 [[MUL]]
;
%add = or disjoint i32 %a, 3
%mul = mul nuw i32 %add, 3
ret i32 %mul
}

define i32 @PR57278_or_disjoint_nsw(i32 %a) {
; CHECK-LABEL: @PR57278_or_disjoint_nsw(
; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[A:%.*]], 3
; CHECK-NEXT: [[MUL:%.*]] = add i32 [[TMP1]], 9
; CHECK-NEXT: ret i32 [[MUL]]
;
%add = or disjoint i32 %a, 3
%mul = mul nsw i32 %add, 3
ret i32 %mul
}

; https://alive2.llvm.org/ce/z/XYpv9q
define <2 x i32> @PR57278_shl_vec(<2 x i32> %v1) {
; CHECK-LABEL: @PR57278_shl_vec(
Expand Down

0 comments on commit 410bf5e

Please sign in to comment.