Skip to content

[InstCombine] Reducing rust i128::midpoint instructions #99614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,47 @@ static Instruction *foldBoxMultiply(BinaryOperator &I) {
return nullptr;
}

// Relating to the i128::midpoint in rust nightly
// we're finding this expression: ((Y ^ X) >> 1 + (Y & X))
// with the i128 data type and reducing the amount of asm instructions
// generated.
static Instruction *foldMidpointExpression(BinaryOperator &I) {
Value *X, *Y;

if (!I.getType()->isIntegerTy(128))
return nullptr;

if (!match(&I, m_Add(m_LShr(m_Xor(m_Value(X), m_Value(Y)), m_ConstantInt()),
m_c_And(m_Deferred(X), m_Deferred(Y)))))
return nullptr;

IRBuilder<> Builder(&I);
Module *Mod = I.getModule();

// Create the call llvm.uadd.with.overflow.i128
Function *UAddWithOverflow = Intrinsic::getDeclaration(Mod, Intrinsic::uadd_with_overflow,
Type::getInt128Ty(Mod->getContext()));
CallInst *UAddCall = Builder.CreateCall(UAddWithOverflow, {Y, X});
UAddCall->setTailCall(); // Mark the call as a tail call

// Extract the sum and the
Value *Sum = Builder.CreateExtractValue(UAddCall, 0);
Value *Overflow = Builder.CreateExtractValue(UAddCall, 1);

// Create the right shift for the sum element of
// overflow
Value *LShrVal = Builder.CreateLShr(Sum, ConstantInt::get(Type::getInt128Ty(I.getContext()), 1));

// Create the select instruction
Value *SelectVal = Builder.CreateSelect(Overflow,
ConstantInt::get(Type::getInt128Ty(I.getContext()), APInt(128, 1).shl(127)),
ConstantInt::get(Type::getInt128Ty(I.getContext()), 0));

Instruction *OrVal = BinaryOperator::CreateOr(LShrVal, SelectVal);

return OrVal;
}

Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
if (Value *V = simplifyAddInst(I.getOperand(0), I.getOperand(1),
I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
Expand All @@ -1505,6 +1546,9 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
if (Instruction *R = foldBoxMultiply(I))
return R;

if (Instruction *R = foldMidpointExpression(I))
return R;

if (Instruction *R = factorizeMathWithShlOps(I, Builder))
return R;

Expand Down
143 changes: 143 additions & 0 deletions llvm/test/Transforms/InstCombine/xor_lshr_and_i128.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

define i128 @xor_lshr_and(i128 %x, i128 %y) {
; CHECK-LABEL: define i128 @xor_lshr_and(
; CHECK-SAME: i128 [[X:%.*]], i128 [[Y:%.*]]) {
; CHECK-NEXT: [[START:.*:]]
; CHECK-NEXT: [[TMP0:%.*]] = tail call { i128, i1 } @llvm.uadd.with.overflow.i128(i128 [[X]], i128 [[Y]])
; CHECK-NEXT: [[XOR:%.*]] = extractvalue { i128, i1 } [[TMP0]], 0
; CHECK-NEXT: [[TMP2:%.*]] = extractvalue { i128, i1 } [[TMP0]], 1
; CHECK-NEXT: [[LSHR:%.*]] = lshr i128 [[XOR]], 1
; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP2]], i128 -170141183460469231731687303715884105728, i128 0
; CHECK-NEXT: [[ADD:%.*]] = or disjoint i128 [[LSHR]], [[TMP4]]
; CHECK-NEXT: ret i128 [[ADD]]
;
start:
%xor = xor i128 %y, %x
%lshr = lshr i128 %xor, 1
%and = and i128 %y, %x
%add = add i128 %lshr, %and
ret i128 %add
}

define i128 @xor_lshr_and_commuted1(i128 %x, i128 %y) {
; CHECK-LABEL: define i128 @xor_lshr_and_commuted1(
; CHECK-SAME: i128 [[X:%.*]], i128 [[Y:%.*]]) {
; CHECK-NEXT: [[START:.*:]]
; CHECK-NEXT: [[TMP0:%.*]] = tail call { i128, i1 } @llvm.uadd.with.overflow.i128(i128 [[X]], i128 [[Y]])
; CHECK-NEXT: [[XOR:%.*]] = extractvalue { i128, i1 } [[TMP0]], 0
; CHECK-NEXT: [[TMP2:%.*]] = extractvalue { i128, i1 } [[TMP0]], 1
; CHECK-NEXT: [[LSHR:%.*]] = lshr i128 [[XOR]], 1
; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP2]], i128 -170141183460469231731687303715884105728, i128 0
; CHECK-NEXT: [[ADD:%.*]] = or disjoint i128 [[LSHR]], [[TMP4]]
; CHECK-NEXT: ret i128 [[ADD]]
;
start:
%and = and i128 %y, %x
%xor = xor i128 %y, %x
%lshr = lshr i128 %xor, 1
%add = add i128 %lshr, %and
ret i128 %add
}

define i128 @xor_lshr_and_commuted2(i128 %x, i128 %y) {
; CHECK-LABEL: define i128 @xor_lshr_and_commuted2(
; CHECK-SAME: i128 [[X:%.*]], i128 [[Y:%.*]]) {
; CHECK-NEXT: [[START:.*:]]
; CHECK-NEXT: [[TMP0:%.*]] = tail call { i128, i1 } @llvm.uadd.with.overflow.i128(i128 [[X]], i128 [[Y]])
; CHECK-NEXT: [[XOR:%.*]] = extractvalue { i128, i1 } [[TMP0]], 0
; CHECK-NEXT: [[TMP2:%.*]] = extractvalue { i128, i1 } [[TMP0]], 1
; CHECK-NEXT: [[LSHR:%.*]] = lshr i128 [[XOR]], 1
; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP2]], i128 -170141183460469231731687303715884105728, i128 0
; CHECK-NEXT: [[ADD:%.*]] = or disjoint i128 [[LSHR]], [[TMP4]]
; CHECK-NEXT: ret i128 [[ADD]]
;
start:
%and = and i128 %y, %x
%xor = xor i128 %y, %x
%lshr = lshr i128 %xor, 1
%add = add i128 %lshr, %and
ret i128 %add
}

declare void @use(i8)

define i128 @xor_lshr_and_multi_use(i128 %x, i128 %y) {
; CHECK-LABEL: define i128 @xor_lshr_and_multi_use(
; CHECK-SAME: i128 [[X:%.*]], i128 [[Y:%.*]]) {
; CHECK-NEXT: [[START:.*:]]
; CHECK-NEXT: [[XOR:%.*]] = xor i128 [[Y]], [[X]]
; CHECK-NEXT: call void @use(i128 [[XOR]])
; CHECK-NEXT: [[LSHR:%.*]] = lshr i128 [[XOR]], 1
; CHECK-NEXT: call void @use(i128 [[LSHR]])
; CHECK-NEXT: [[TMP0:%.*]] = tail call { i128, i1 } @llvm.uadd.with.overflow.i128(i128 [[X]], i128 [[Y]])
; CHECK-NEXT: [[TMP1:%.*]] = extractvalue { i128, i1 } [[TMP0]], 0
; CHECK-NEXT: [[TMP2:%.*]] = extractvalue { i128, i1 } [[TMP0]], 1
; CHECK-NEXT: [[TMP3:%.*]] = lshr i128 [[TMP1]], 1
; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP2]], i128 -170141183460469231731687303715884105728, i128 0
; CHECK-NEXT: [[ADD:%.*]] = or disjoint i128 [[TMP3]], [[TMP4]]
; CHECK-NEXT: ret i128 [[ADD]]
;
start:
%xor = xor i128 %y, %x
call void @use(i128 %xor)
%lshr = lshr i128 %xor, 1
call void @use(i128 %lshr)
%and = and i128 %y, %x
%add = add i128 %lshr, %and
ret i128 %add
}

define i128 @xor_lshr_and_negative(i128 %x, i128 %y) {
; CHECK-LABEL: define i128 @xor_lshr_and_negative(
; CHECK-SAME: i128 [[X:%.*]], i128 [[Y:%.*]]) {
; CHECK-NEXT: [[START:.*:]]
; CHECK-NEXT: [[XOR:%.*]] = xor i128 [[X]], -1
; CHECK-NEXT: [[AND:%.*]] = and i128 [[Y]], [[X]]
; CHECK-NEXT: [[ADD:%.*]] = add i128 [[AND]], [[XOR]]
; CHECK-NEXT: ret i128 [[ADD]]
;
start:
%xor = xor i128 %x, -1
%and = and i128 %y, %x
%add = add i128 %xor, %and
ret i128 %add
}

define i32 @xor_lshr_and_negative2(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @xor_lshr_and_negative2(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[START:.*:]]
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[Y]], [[X]]
; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[XOR]], 1
; CHECK-NEXT: [[AND:%.*]] = and i32 [[Y]], [[X]]
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[LSHR]], [[AND]]
; CHECK-NEXT: ret i32 [[ADD]]
;
start:
%xor = xor i32 %y, %x
%lshr = lshr i32 %xor, 1
%and = and i32 %y, %x
%add = add i32 %lshr, %and
ret i32 %add
}

define <2 x i128> @xor_lshr_and_vec(<2 x i128> %x, <2 x i128> %y) {
; CHECK-LABEL: define <2 x i128> @xor_lshr_and_vec(
; CHECK-SAME: <2 x i128> [[X:%.*]], <2 x i128> [[Y:%.*]]) {
; CHECK-NEXT: [[START:.*:]]
; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i128> [[Y]], [[X]]
; CHECK-NEXT: [[LSHR:%.*]] = lshr <2 x i128> [[XOR]], <i128 1, i128 1>
; CHECK-NEXT: [[AND:%.*]] = and <2 x i128> [[Y]], [[X]]
; CHECK-NEXT: [[ADD:%.*]] = add <2 x i128> [[LSHR]], [[AND]]
; CHECK-NEXT: ret <2 x i128> [[ADD]]
;
start:
%xor = xor <2 x i128> %y, %x
%lshr = lshr <2 x i128> %xor, <i128 1, i128 1>
%and = and <2 x i128> %y, %x
%add = add <2 x i128> %lshr, %and
ret <2 x i128> %add
}

Loading