return BinaryOperator::CreateXor(Or, ConstantInt::get(Ty, *CV));
}
+ // If the operands have no common bits set:
+ // or (mul X, Y), X --> add (mul X, Y), X --> mul X, (Y + 1)
+ if (match(&I,
+ m_c_Or(m_OneUse(m_Mul(m_Value(X), m_Value(Y))), m_Deferred(X))) &&
+ haveNoCommonBitsSet(Op0, Op1, DL)) {
+ Value *IncrementY = Builder.CreateAdd(Y, ConstantInt::get(Ty, 1));
+ return BinaryOperator::CreateMul(X, IncrementY);
+ }
+
// (A & C) | (B & D)
Value *A, *B, *C, *D;
if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
; CHECK-LABEL: @mul_no_common_bits(
; CHECK-NEXT: [[X:%.*]] = and i32 [[P1:%.*]], 7
; CHECK-NEXT: [[Y:%.*]] = shl i32 [[P2:%.*]], 3
-; CHECK-NEXT: [[M:%.*]] = mul i32 [[X]], [[Y]]
-; CHECK-NEXT: [[R:%.*]] = or i32 [[M]], [[X]]
+; CHECK-NEXT: [[TMP1:%.*]] = or i32 [[Y]], 1
+; CHECK-NEXT: [[R:%.*]] = mul i32 [[X]], [[TMP1]]
; CHECK-NEXT: ret i32 [[R]]
;
%x = and i32 %p1, 7
define i32 @mul_no_common_bits_const_op(i32 %p) {
; CHECK-LABEL: @mul_no_common_bits_const_op(
; CHECK-NEXT: [[X:%.*]] = and i32 [[P:%.*]], 7
-; CHECK-NEXT: [[M:%.*]] = mul nuw nsw i32 [[X]], 24
-; CHECK-NEXT: [[R:%.*]] = or i32 [[M]], [[X]]
+; CHECK-NEXT: [[R:%.*]] = mul nuw nsw i32 [[X]], 25
; CHECK-NEXT: ret i32 [[R]]
;
%x = and i32 %p, 7
define <2 x i12> @mul_no_common_bits_commute(<2 x i12> %p) {
; CHECK-LABEL: @mul_no_common_bits_commute(
; CHECK-NEXT: [[X:%.*]] = and <2 x i12> [[P:%.*]], <i12 1, i12 1>
-; CHECK-NEXT: [[M:%.*]] = mul nuw nsw <2 x i12> [[X]], <i12 14, i12 16>
-; CHECK-NEXT: [[R:%.*]] = or <2 x i12> [[X]], [[M]]
+; CHECK-NEXT: [[R:%.*]] = mul nuw nsw <2 x i12> [[X]], <i12 15, i12 17>
; CHECK-NEXT: ret <2 x i12> [[R]]
;
%x = and <2 x i12> %p, <i12 1, i12 1>
ret <2 x i12> %r
}
-define i32 @mul_no_common_bits_uses(i32 %p) {
+; negative test - extra use requires extra instructions
+
+define i32 @mul_no_common_bits_uses(i32 %p1, i32 %p2) {
; CHECK-LABEL: @mul_no_common_bits_uses(
+; CHECK-NEXT: [[X:%.*]] = and i32 [[P1:%.*]], 7
+; CHECK-NEXT: [[Y:%.*]] = shl i32 [[P2:%.*]], 3
+; CHECK-NEXT: [[M:%.*]] = mul i32 [[X]], [[Y]]
+; CHECK-NEXT: call void @use(i32 [[M]])
+; CHECK-NEXT: [[R:%.*]] = or i32 [[M]], [[X]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %x = and i32 %p1, 7
+ %y = shl i32 %p2, 3
+ %m = mul i32 %x, %y
+ call void @use(i32 %m)
+ %r = or i32 %m, %x
+ ret i32 %r
+}
+
+; negative test - probably not good to create an extra mul
+
+define i32 @mul_no_common_bits_const_op_uses(i32 %p) {
+; CHECK-LABEL: @mul_no_common_bits_const_op_uses(
; CHECK-NEXT: [[X:%.*]] = and i32 [[P:%.*]], 7
; CHECK-NEXT: [[M:%.*]] = mul nuw nsw i32 [[X]], 24
; CHECK-NEXT: call void @use(i32 [[M]])
ret i32 %r
}
+; negative test - %x and %m may have set 3rd bit
+
define i32 @mul_common_bits(i32 %p) {
; CHECK-LABEL: @mul_common_bits(
; CHECK-NEXT: [[X:%.*]] = and i32 [[P:%.*]], 7