Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,19 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
if (Instruction *I = foldMaxMulShift(I1, I0))
return I;
}

// umax(nuw_shl(C0, x), nuw_shl(C0, y)) -> nuw_shl(C0, umax(x, y))
// umin(nuw_shl(C0, x), nuw_shl(C0, y)) -> nuw_shl(C0, umin(x, y))
const APInt *C1, *C2;
if (match(I0, m_OneUse(m_NUWShl(m_APInt(C1), m_Value()))) &&
match(I1, m_OneUse(m_NUWShl(m_APInt(C2), m_Value()))) && *C1 == *C2) {
Value *X = cast<ShlOperator>(I0)->getOperand(1);
Value *Y = cast<ShlOperator>(I1)->getOperand(1);
Value *MaxMin = Builder.CreateBinaryIntrinsic(IID, X, Y);
return BinaryOperator::CreateNUWShl(ConstantInt::get(I0->getType(), *C1),
MaxMin);
}

// If both operands of unsigned min/max are sign-extended, it is still ok
// to narrow the operation.
[[fallthrough]];
Expand Down
232 changes: 232 additions & 0 deletions llvm/test/Transforms/InstCombine/shift-uminmax.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

; For the following patterns:
; umax(nuw_shl(C0, x), nuw_shl(C0, y)) -> nuw_shl(C0, umax(x, y))
; umin(nuw_shl(C0, x), nuw_shl(C0, y)) -> nuw_shl(C0, umin(x, y))

define i32 @test_umax_shl_const1(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umax_shl_const1(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
; CHECK-NEXT: [[MAX:%.*]] = shl nuw i32 1, [[TMP1]]
; CHECK-NEXT: ret i32 [[MAX]]
;
%shl_x = shl nuw i32 1, %x
%shl_y = shl nuw i32 1, %y
%max = call i32 @llvm.umax.i32(i32 %shl_x, i32 %shl_y)
ret i32 %max
}

define i32 @test_umin_shl_const1(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umin_shl_const1(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 [[Y]])
; CHECK-NEXT: [[MIN:%.*]] = shl nuw i32 1, [[TMP1]]
; CHECK-NEXT: ret i32 [[MIN]]
;
%shl_x = shl nuw i32 1, %x
%shl_y = shl nuw i32 1, %y
%min = call i32 @llvm.umin.i32(i32 %shl_x, i32 %shl_y)
ret i32 %min
}

define i32 @test_umax_shl_const5(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umax_shl_const5(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
; CHECK-NEXT: [[MAX:%.*]] = shl nuw i32 5, [[TMP1]]
; CHECK-NEXT: ret i32 [[MAX]]
;
%shl_x = shl nuw i32 5, %x
%shl_y = shl nuw i32 5, %y
%max = call i32 @llvm.umax.i32(i32 %shl_x, i32 %shl_y)
ret i32 %max
}

define i32 @test_umin_shl_const5(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umin_shl_const5(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 [[Y]])
; CHECK-NEXT: [[MIN:%.*]] = shl nuw i32 5, [[TMP1]]
; CHECK-NEXT: ret i32 [[MIN]]
;
%shl_x = shl nuw i32 5, %x
%shl_y = shl nuw i32 5, %y
%min = call i32 @llvm.umin.i32(i32 %shl_x, i32 %shl_y)
ret i32 %min
}

declare void @use(i8)

define i32 @test_umax_shl_const1_multi_use(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umax_shl_const1_multi_use(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[SHL_X:%.*]] = shl nuw i32 1, [[X]]
; CHECK-NEXT: [[SHL_Y:%.*]] = shl nuw i32 1, [[Y]]
; CHECK-NEXT: call void @use(i32 [[SHL_X]])
; CHECK-NEXT: call void @use(i32 [[SHL_Y]])
; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umax.i32(i32 [[SHL_X]], i32 [[SHL_Y]])
; CHECK-NEXT: ret i32 [[MAX]]
;
%shl_x = shl nuw i32 1, %x
%shl_y = shl nuw i32 1, %y
call void @use(i32 %shl_x)
call void @use(i32 %shl_y)
%max = call i32 @llvm.umax.i32(i32 %shl_x, i32 %shl_y)
ret i32 %max
}

define i32 @test_umin_shl_const1_multi_use(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umin_shl_const1_multi_use(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[SHL_X:%.*]] = shl nuw i32 1, [[X]]
; CHECK-NEXT: [[SHL_Y:%.*]] = shl nuw i32 1, [[Y]]
; CHECK-NEXT: call void @use(i32 [[SHL_X]])
; CHECK-NEXT: call void @use(i32 [[SHL_Y]])
; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[SHL_X]], i32 [[SHL_Y]])
; CHECK-NEXT: ret i32 [[MIN]]
;
%shl_x = shl nuw i32 1, %x
%shl_y = shl nuw i32 1, %y
call void @use(i32 %shl_x)
call void @use(i32 %shl_y)
%min = call i32 @llvm.umin.i32(i32 %shl_x, i32 %shl_y)
ret i32 %min
}

define i32 @test_umax_shl_const1_commuted(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umax_shl_const1_commuted(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[Y]], i32 [[X]])
; CHECK-NEXT: [[MAX:%.*]] = shl nuw i32 1, [[TMP1]]
; CHECK-NEXT: ret i32 [[MAX]]
;
%shl_x = shl nuw i32 1, %x
%shl_y = shl nuw i32 1, %y
%max = call i32 @llvm.umax.i32(i32 %shl_y, i32 %shl_x)
ret i32 %max
}

define i32 @test_umin_shl_const1_commuted(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umin_shl_const1_commuted(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.umin.i32(i32 [[Y]], i32 [[X]])
; CHECK-NEXT: [[MIN:%.*]] = shl nuw i32 1, [[TMP1]]
; CHECK-NEXT: ret i32 [[MIN]]
;
%shl_x = shl nuw i32 1, %x
%shl_y = shl nuw i32 1, %y
%min = call i32 @llvm.umin.i32(i32 %shl_y, i32 %shl_x)
ret i32 %min
}

define <2 x i32> @test_umax_shl_vector_splat(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: define <2 x i32> @test_umax_shl_vector_splat(
; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i32> @llvm.umax.v2i32(<2 x i32> [[X]], <2 x i32> [[Y]])
; CHECK-NEXT: [[MAX:%.*]] = shl nuw <2 x i32> splat (i32 1), [[TMP1]]
; CHECK-NEXT: ret <2 x i32> [[MAX]]
;
%shl_x = shl nuw <2 x i32> <i32 1, i32 1>, %x
%shl_y = shl nuw <2 x i32> <i32 1, i32 1>, %y
%max = call <2 x i32> @llvm.umax.v2i32(<2 x i32> %shl_x, <2 x i32> %shl_y)
ret <2 x i32> %max
}

define <2 x i32> @test_umin_shl_vector_splat(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: define <2 x i32> @test_umin_shl_vector_splat(
; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i32> @llvm.umin.v2i32(<2 x i32> [[X]], <2 x i32> [[Y]])
; CHECK-NEXT: [[MIN:%.*]] = shl nuw <2 x i32> splat (i32 1), [[TMP1]]
; CHECK-NEXT: ret <2 x i32> [[MIN]]
;
%shl_x = shl nuw <2 x i32> <i32 1, i32 1>, %x
%shl_y = shl nuw <2 x i32> <i32 1, i32 1>, %y
%min = call <2 x i32> @llvm.umin.v2i32(<2 x i32> %shl_x, <2 x i32> %shl_y)
ret <2 x i32> %min
}

define <2 x i32> @test_umax_shl_vector_non_splat(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: define <2 x i32> @test_umax_shl_vector_non_splat(
; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]]) {
; CHECK-NEXT: [[SHL_X:%.*]] = shl nuw <2 x i32> <i32 1, i32 2>, [[X]]
; CHECK-NEXT: [[SHL_Y:%.*]] = shl nuw <2 x i32> <i32 1, i32 2>, [[Y]]
; CHECK-NEXT: [[MAX:%.*]] = call <2 x i32> @llvm.umax.v2i32(<2 x i32> [[SHL_X]], <2 x i32> [[SHL_Y]])
; CHECK-NEXT: ret <2 x i32> [[MAX]]
;
%shl_x = shl nuw <2 x i32> <i32 1, i32 2>, %x
%shl_y = shl nuw <2 x i32> <i32 1, i32 2>, %y
%max = call <2 x i32> @llvm.umax.v2i32(<2 x i32> %shl_x, <2 x i32> %shl_y)
ret <2 x i32> %max
}

define <2 x i32> @test_umin_shl_vector_non_splat(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: define <2 x i32> @test_umin_shl_vector_non_splat(
; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]]) {
; CHECK-NEXT: [[SHL_X:%.*]] = shl nuw <2 x i32> <i32 1, i32 2>, [[X]]
; CHECK-NEXT: [[SHL_Y:%.*]] = shl nuw <2 x i32> <i32 1, i32 2>, [[Y]]
; CHECK-NEXT: [[MIN:%.*]] = call <2 x i32> @llvm.umin.v2i32(<2 x i32> [[SHL_X]], <2 x i32> [[SHL_Y]])
; CHECK-NEXT: ret <2 x i32> [[MIN]]
;
%shl_x = shl nuw <2 x i32> <i32 1, i32 2>, %x
%shl_y = shl nuw <2 x i32> <i32 1, i32 2>, %y
%min = call <2 x i32> @llvm.umin.v2i32(<2 x i32> %shl_x, <2 x i32> %shl_y)
ret <2 x i32> %min
}

define i32 @test_umax_shl_different_base(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umax_shl_different_base(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[SHL_X:%.*]] = shl nuw i32 1, [[X]]
; CHECK-NEXT: [[SHL_Y:%.*]] = shl nuw i32 2, [[Y]]
; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umax.i32(i32 [[SHL_X]], i32 [[SHL_Y]])
; CHECK-NEXT: ret i32 [[MAX]]
;
%shl_x = shl nuw i32 1, %x
%shl_y = shl nuw i32 2, %y
%max = call i32 @llvm.umax.i32(i32 %shl_x, i32 %shl_y)
ret i32 %max
}

define i32 @test_umin_shl_different_base(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umin_shl_different_base(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[SHL_X:%.*]] = shl nuw i32 1, [[X]]
; CHECK-NEXT: [[SHL_Y:%.*]] = shl nuw i32 2, [[Y]]
; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[SHL_X]], i32 [[SHL_Y]])
; CHECK-NEXT: ret i32 [[MIN]]
;
%shl_x = shl nuw i32 1, %x
%shl_y = shl nuw i32 2, %y
%min = call i32 @llvm.umin.i32(i32 %shl_x, i32 %shl_y)
ret i32 %min
}

define i32 @test_umax_shl_no_nuw_flag(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umax_shl_no_nuw_flag(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[SHL_X:%.*]] = shl i32 2, [[X]]
; CHECK-NEXT: [[SHL_Y:%.*]] = shl i32 2, [[Y]]
; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umax.i32(i32 [[SHL_X]], i32 [[SHL_Y]])
; CHECK-NEXT: ret i32 [[MAX]]
;
%shl_x = shl i32 2, %x
%shl_y = shl i32 2, %y
%max = call i32 @llvm.umax.i32(i32 %shl_x, i32 %shl_y)
ret i32 %max
}

define i32 @test_umin_shl_no_nuw_flag(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_umin_shl_no_nuw_flag(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[SHL_X:%.*]] = shl i32 2, [[X]]
; CHECK-NEXT: [[SHL_Y:%.*]] = shl i32 2, [[Y]]
; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[SHL_X]], i32 [[SHL_Y]])
; CHECK-NEXT: ret i32 [[MIN]]
;
%shl_x = shl i32 2, %x
%shl_y = shl i32 2, %y
%min = call i32 @llvm.umin.i32(i32 %shl_x, i32 %shl_y)
ret i32 %min
}
Loading