Skip to content

Commit e3b3c91

Browse files
authored
[InstCombine] Fix missed opportunity to fold 'or' into 'mul' operand. (#74225)
We were able to fold or (mul X, Y), X --> mul X, (add Y, 1) (when the multiply has no common bits with X) This patch makes the transform work if the mul operands are commuted.
1 parent ecc080c commit e3b3c91

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3394,7 +3394,7 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
33943394
// If the operands have no common bits set:
33953395
// or (mul X, Y), X --> add (mul X, Y), X --> mul X, (Y + 1)
33963396
if (match(&I,
3397-
m_c_Or(m_OneUse(m_Mul(m_Value(X), m_Value(Y))), m_Deferred(X))) &&
3397+
m_c_Or(m_Value(X), m_OneUse(m_c_Mul(m_Deferred(X), m_Value(Y))))) &&
33983398
haveNoCommonBitsSet(Op0, Op1, DL)) {
33993399
Value *IncrementY = Builder.CreateAdd(Y, ConstantInt::get(Ty, 1));
34003400
return BinaryOperator::CreateMul(X, IncrementY);

llvm/test/Transforms/InstCombine/or.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,21 @@ define <2 x i12> @mul_no_common_bits_commute(<2 x i12> %p) {
15111511
ret <2 x i12> %r
15121512
}
15131513

1514+
define i32 @mul_no_common_bits_commute2(i32 %p1, i32 %p2) {
1515+
; CHECK-LABEL: @mul_no_common_bits_commute2(
1516+
; CHECK-NEXT: [[X:%.*]] = and i32 [[P1:%.*]], 7
1517+
; CHECK-NEXT: [[Y:%.*]] = shl i32 [[P2:%.*]], 3
1518+
; CHECK-NEXT: [[TMP1:%.*]] = or disjoint i32 [[Y]], 1
1519+
; CHECK-NEXT: [[R:%.*]] = mul i32 [[X]], [[TMP1]]
1520+
; CHECK-NEXT: ret i32 [[R]]
1521+
;
1522+
%x = and i32 %p1, 7
1523+
%y = shl i32 %p2, 3
1524+
%m = mul i32 %y, %x
1525+
%r = or i32 %m, %x
1526+
ret i32 %r
1527+
}
1528+
15141529
; negative test - extra use requires extra instructions
15151530

15161531
define i32 @mul_no_common_bits_uses(i32 %p1, i32 %p2) {

0 commit comments

Comments
 (0)