; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -instcombine -S | FileCheck %s ; Instcombine should recognize that this code can be adjusted to fit the canonical max/min pattern. ; No change define i32 @smax1(i32 %n) { ; CHECK-LABEL: @smax1( ; CHECK-NEXT: [[T:%.*]] = icmp sgt i32 [[N:%.*]], 0 ; CHECK-NEXT: [[M:%.*]] = select i1 [[T]], i32 [[N]], i32 0 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp sgt i32 %n, 0 %m = select i1 %t, i32 %n, i32 0 ret i32 %m } ; No change define i32 @smin1(i32 %n) { ; CHECK-LABEL: @smin1( ; CHECK-NEXT: [[T:%.*]] = icmp slt i32 [[N:%.*]], 0 ; CHECK-NEXT: [[M:%.*]] = select i1 [[T]], i32 [[N]], i32 0 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp slt i32 %n, 0 %m = select i1 %t, i32 %n, i32 0 ret i32 %m } ; Canonicalize min/max. define i32 @smax2(i32 %n) { ; CHECK-LABEL: @smax2( ; CHECK-NEXT: [[T:%.*]] = icmp sgt i32 [[N:%.*]], 0 ; CHECK-NEXT: [[M:%.*]] = select i1 [[T]], i32 [[N]], i32 0 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp sge i32 %n, 1 %m = select i1 %t, i32 %n, i32 0 ret i32 %m } ; Canonicalize min/max. define i32 @smin2(i32 %n) { ; CHECK-LABEL: @smin2( ; CHECK-NEXT: [[T:%.*]] = icmp slt i32 [[N:%.*]], 0 ; CHECK-NEXT: [[M:%.*]] = select i1 [[T]], i32 [[N]], i32 0 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp sle i32 %n, -1 %m = select i1 %t, i32 %n, i32 0 ret i32 %m } ; Canonicalize min/max. define i32 @smax3(i32 %n) { ; CHECK-LABEL: @smax3( ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[N:%.*]], 0 ; CHECK-NEXT: [[M:%.*]] = select i1 [[TMP1]], i32 [[N]], i32 0 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp sgt i32 %n, -1 %m = select i1 %t, i32 %n, i32 0 ret i32 %m } ; Canonicalize min/max. define <2 x i32> @smax3_vec(<2 x i32> %n) { ; CHECK-LABEL: @smax3_vec( ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i32> [[N:%.*]], zeroinitializer ; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[N]], <2 x i32> zeroinitializer ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp sgt <2 x i32> %n, %m = select <2 x i1> %t, <2 x i32> %n, <2 x i32> zeroinitializer ret <2 x i32> %m } ; Canonicalize min/max. define i32 @smin3(i32 %n) { ; CHECK-LABEL: @smin3( ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[N:%.*]], 0 ; CHECK-NEXT: [[M:%.*]] = select i1 [[TMP1]], i32 [[N]], i32 0 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp slt i32 %n, 1 %m = select i1 %t, i32 %n, i32 0 ret i32 %m } ; Canonicalize min/max. define <2 x i32> @smin3_vec(<2 x i32> %n) { ; CHECK-LABEL: @smin3_vec( ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[N:%.*]], zeroinitializer ; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[N]], <2 x i32> zeroinitializer ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp slt <2 x i32> %n, %m = select <2 x i1> %t, <2 x i32> %n, <2 x i32> zeroinitializer ret <2 x i32> %m } ; Canonicalize min/max. define i32 @umax3(i32 %n) { ; CHECK-LABEL: @umax3( ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[N:%.*]], 5 ; CHECK-NEXT: [[M:%.*]] = select i1 [[TMP1]], i32 [[N]], i32 5 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp ugt i32 %n, 4 %m = select i1 %t, i32 %n, i32 5 ret i32 %m } ; Canonicalize min/max. define <2 x i32> @umax3_vec(<2 x i32> %n) { ; CHECK-LABEL: @umax3_vec( ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt <2 x i32> [[N:%.*]], ; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[N]], <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp ugt <2 x i32> %n, %m = select <2 x i1> %t, <2 x i32> %n, <2 x i32> ret <2 x i32> %m } ; Canonicalize min/max. define i32 @umin3(i32 %n) { ; CHECK-LABEL: @umin3( ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[N:%.*]], 6 ; CHECK-NEXT: [[M:%.*]] = select i1 [[TMP1]], i32 [[N]], i32 6 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp ult i32 %n, 7 %m = select i1 %t, i32 %n, i32 6 ret i32 %m } ; Canonicalize min/max. define <2 x i32> @umin3_vec(<2 x i32> %n) { ; CHECK-LABEL: @umin3_vec( ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <2 x i32> [[N:%.*]], ; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[N]], <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp ult <2 x i32> %n, %m = select <2 x i1> %t, <2 x i32> %n, <2 x i32> ret <2 x i32> %m } ; Canonicalize min/max. define i32 @smax4(i32 %n) { ; CHECK-LABEL: @smax4( ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[N:%.*]], 0 ; CHECK-NEXT: [[M:%.*]] = select i1 [[TMP1]], i32 [[N]], i32 0 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp sge i32 %n, 0 %m = select i1 %t, i32 %n, i32 0 ret i32 %m } ; Canonicalize min/max. define <2 x i32> @smax4_vec(<2 x i32> %n) { ; CHECK-LABEL: @smax4_vec( ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i32> [[N:%.*]], zeroinitializer ; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[N]], <2 x i32> zeroinitializer ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp sge <2 x i32> %n, zeroinitializer %m = select <2 x i1> %t, <2 x i32> %n, <2 x i32> zeroinitializer ret <2 x i32> %m } ; Canonicalize min/max. define i32 @smin4(i32 %n) { ; CHECK-LABEL: @smin4( ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[N:%.*]], 0 ; CHECK-NEXT: [[M:%.*]] = select i1 [[TMP1]], i32 [[N]], i32 0 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp sle i32 %n, 0 %m = select i1 %t, i32 %n, i32 0 ret i32 %m } ; Canonicalize min/max. define <2 x i32> @smin4_vec(<2 x i32> %n) { ; CHECK-LABEL: @smin4_vec( ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[N:%.*]], zeroinitializer ; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[N]], <2 x i32> zeroinitializer ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp sle <2 x i32> %n, zeroinitializer %m = select <2 x i1> %t, <2 x i32> %n, <2 x i32> zeroinitializer ret <2 x i32> %m } ; Canonicalize min/max. define i32 @umax4(i32 %n) { ; CHECK-LABEL: @umax4( ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[N:%.*]], 8 ; CHECK-NEXT: [[M:%.*]] = select i1 [[TMP1]], i32 [[N]], i32 8 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp uge i32 %n, 8 %m = select i1 %t, i32 %n, i32 8 ret i32 %m } ; Canonicalize min/max. define <2 x i32> @umax4_vec(<2 x i32> %n) { ; CHECK-LABEL: @umax4_vec( ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt <2 x i32> [[N:%.*]], ; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[N]], <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp uge <2 x i32> %n, %m = select <2 x i1> %t, <2 x i32> %n, <2 x i32> ret <2 x i32> %m } ; Canonicalize min/max. define i32 @umin4(i32 %n) { ; CHECK-LABEL: @umin4( ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[N:%.*]], 9 ; CHECK-NEXT: [[M:%.*]] = select i1 [[TMP1]], i32 [[N]], i32 9 ; CHECK-NEXT: ret i32 [[M]] ; %t = icmp ule i32 %n, 9 %m = select i1 %t, i32 %n, i32 9 ret i32 %m } ; Canonicalize min/max. define <2 x i32> @umin4_vec(<2 x i32> %n) { ; CHECK-LABEL: @umin4_vec( ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <2 x i32> [[N:%.*]], ; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[N]], <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp ule <2 x i32> %n, %m = select <2 x i1> %t, <2 x i32> %n, <2 x i32> ret <2 x i32> %m } define i64 @smax_sext(i32 %a) { ; CHECK-LABEL: @smax_sext( ; CHECK-NEXT: [[A_EXT:%.*]] = sext i32 [[A:%.*]] to i64 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i64 [[A_EXT]], 0 ; CHECK-NEXT: [[MAX:%.*]] = select i1 [[TMP1]], i64 [[A_EXT]], i64 0 ; CHECK-NEXT: ret i64 [[MAX]] ; %a_ext = sext i32 %a to i64 %cmp = icmp sgt i32 %a, -1 %max = select i1 %cmp, i64 %a_ext, i64 0 ret i64 %max } define <2 x i64> @smax_sext_vec(<2 x i32> %a) { ; CHECK-LABEL: @smax_sext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> [[A:%.*]] to <2 x i64> ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i64> [[A_EXT]], zeroinitializer ; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[TMP1]], <2 x i64> [[A_EXT]], <2 x i64> zeroinitializer ; CHECK-NEXT: ret <2 x i64> [[MAX]] ; %a_ext = sext <2 x i32> %a to <2 x i64> %cmp = icmp sgt <2 x i32> %a, %max = select <2 x i1> %cmp, <2 x i64> %a_ext, <2 x i64> zeroinitializer ret <2 x i64> %max } define i64 @smin_sext(i32 %a) { ; CHECK-LABEL: @smin_sext( ; CHECK-NEXT: [[A_EXT:%.*]] = sext i32 [[A:%.*]] to i64 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i64 [[A_EXT]], 0 ; CHECK-NEXT: [[MIN:%.*]] = select i1 [[TMP1]], i64 [[A_EXT]], i64 0 ; CHECK-NEXT: ret i64 [[MIN]] ; %a_ext = sext i32 %a to i64 %cmp = icmp slt i32 %a, 1 %min = select i1 %cmp, i64 %a_ext, i64 0 ret i64 %min } define <2 x i64>@smin_sext_vec(<2 x i32> %a) { ; CHECK-LABEL: @smin_sext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> [[A:%.*]] to <2 x i64> ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i64> [[A_EXT]], zeroinitializer ; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[TMP1]], <2 x i64> [[A_EXT]], <2 x i64> zeroinitializer ; CHECK-NEXT: ret <2 x i64> [[MIN]] ; %a_ext = sext <2 x i32> %a to <2 x i64> %cmp = icmp slt <2 x i32> %a, %min = select <2 x i1> %cmp, <2 x i64> %a_ext, <2 x i64> zeroinitializer ret <2 x i64> %min } define i64 @umax_sext(i32 %a) { ; CHECK-LABEL: @umax_sext( ; CHECK-NEXT: [[A_EXT:%.*]] = sext i32 [[A:%.*]] to i64 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i64 [[A_EXT]], 3 ; CHECK-NEXT: [[MAX:%.*]] = select i1 [[TMP1]], i64 [[A_EXT]], i64 3 ; CHECK-NEXT: ret i64 [[MAX]] ; %a_ext = sext i32 %a to i64 %cmp = icmp ugt i32 %a, 2 %max = select i1 %cmp, i64 %a_ext, i64 3 ret i64 %max } define <2 x i64> @umax_sext_vec(<2 x i32> %a) { ; CHECK-LABEL: @umax_sext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> [[A:%.*]] to <2 x i64> ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt <2 x i64> [[A_EXT]], ; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[TMP1]], <2 x i64> [[A_EXT]], <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[MAX]] ; %a_ext = sext <2 x i32> %a to <2 x i64> %cmp = icmp ugt <2 x i32> %a, %max = select <2 x i1> %cmp, <2 x i64> %a_ext, <2 x i64> ret <2 x i64> %max } define i64 @umin_sext(i32 %a) { ; CHECK-LABEL: @umin_sext( ; CHECK-NEXT: [[A_EXT:%.*]] = sext i32 [[A:%.*]] to i64 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i64 [[A_EXT]], 2 ; CHECK-NEXT: [[MIN:%.*]] = select i1 [[TMP1]], i64 [[A_EXT]], i64 2 ; CHECK-NEXT: ret i64 [[MIN]] ; %a_ext = sext i32 %a to i64 %cmp = icmp ult i32 %a, 3 %min = select i1 %cmp, i64 %a_ext, i64 2 ret i64 %min } define <2 x i64> @umin_sext_vec(<2 x i32> %a) { ; CHECK-LABEL: @umin_sext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> [[A:%.*]] to <2 x i64> ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <2 x i64> [[A_EXT]], ; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[TMP1]], <2 x i64> [[A_EXT]], <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[MIN]] ; %a_ext = sext <2 x i32> %a to <2 x i64> %cmp = icmp ult <2 x i32> %a, %min = select <2 x i1> %cmp, <2 x i64> %a_ext, <2 x i64> ret <2 x i64> %min } define i64 @umax_sext2(i32 %a) { ; CHECK-LABEL: @umax_sext2( ; CHECK-NEXT: [[A_EXT:%.*]] = sext i32 [[A:%.*]] to i64 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[A_EXT]], 2 ; CHECK-NEXT: [[MIN:%.*]] = select i1 [[CMP]], i64 [[A_EXT]], i64 2 ; CHECK-NEXT: ret i64 [[MIN]] ; %a_ext = sext i32 %a to i64 %cmp = icmp ult i32 %a, 3 %min = select i1 %cmp, i64 2, i64 %a_ext ret i64 %min } define <2 x i64> @umax_sext2_vec(<2 x i32> %a) { ; CHECK-LABEL: @umax_sext2_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> [[A:%.*]] to <2 x i64> ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i64> [[A_EXT]], ; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> [[A_EXT]], <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[MIN]] ; %a_ext = sext <2 x i32> %a to <2 x i64> %cmp = icmp ult <2 x i32> %a, %min = select <2 x i1> %cmp, <2 x i64> , <2 x i64> %a_ext ret <2 x i64> %min } define i64 @umin_sext2(i32 %a) { ; CHECK-LABEL: @umin_sext2( ; CHECK-NEXT: [[A_EXT:%.*]] = sext i32 [[A:%.*]] to i64 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[A_EXT]], 3 ; CHECK-NEXT: [[MIN:%.*]] = select i1 [[CMP]], i64 [[A_EXT]], i64 3 ; CHECK-NEXT: ret i64 [[MIN]] ; %a_ext = sext i32 %a to i64 %cmp = icmp ugt i32 %a, 2 %min = select i1 %cmp, i64 3, i64 %a_ext ret i64 %min } define <2 x i64> @umin_sext2_vec(<2 x i32> %a) { ; CHECK-LABEL: @umin_sext2_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> [[A:%.*]] to <2 x i64> ; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i64> [[A_EXT]], ; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> [[A_EXT]], <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[MIN]] ; %a_ext = sext <2 x i32> %a to <2 x i64> %cmp = icmp ugt <2 x i32> %a, %min = select <2 x i1> %cmp, <2 x i64> , <2 x i64> %a_ext ret <2 x i64> %min } define i64 @umax_zext(i32 %a) { ; CHECK-LABEL: @umax_zext( ; CHECK-NEXT: [[A_EXT:%.*]] = zext i32 [[A:%.*]] to i64 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i64 [[A_EXT]], 3 ; CHECK-NEXT: [[MAX:%.*]] = select i1 [[TMP1]], i64 [[A_EXT]], i64 3 ; CHECK-NEXT: ret i64 [[MAX]] ; %a_ext = zext i32 %a to i64 %cmp = icmp ugt i32 %a, 2 %max = select i1 %cmp, i64 %a_ext, i64 3 ret i64 %max } define <2 x i64> @umax_zext_vec(<2 x i32> %a) { ; CHECK-LABEL: @umax_zext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = zext <2 x i32> [[A:%.*]] to <2 x i64> ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt <2 x i64> [[A_EXT]], ; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[TMP1]], <2 x i64> [[A_EXT]], <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[MAX]] ; %a_ext = zext <2 x i32> %a to <2 x i64> %cmp = icmp ugt <2 x i32> %a, %max = select <2 x i1> %cmp, <2 x i64> %a_ext, <2 x i64> ret <2 x i64> %max } define i64 @umin_zext(i32 %a) { ; CHECK-LABEL: @umin_zext( ; CHECK-NEXT: [[A_EXT:%.*]] = zext i32 [[A:%.*]] to i64 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i64 [[A_EXT]], 2 ; CHECK-NEXT: [[MIN:%.*]] = select i1 [[TMP1]], i64 [[A_EXT]], i64 2 ; CHECK-NEXT: ret i64 [[MIN]] ; %a_ext = zext i32 %a to i64 %cmp = icmp ult i32 %a, 3 %min = select i1 %cmp, i64 %a_ext, i64 2 ret i64 %min } define <2 x i64> @umin_zext_vec(<2 x i32> %a) { ; CHECK-LABEL: @umin_zext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = zext <2 x i32> [[A:%.*]] to <2 x i64> ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <2 x i64> [[A_EXT]], ; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[TMP1]], <2 x i64> [[A_EXT]], <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[MIN]] ; %a_ext = zext <2 x i32> %a to <2 x i64> %cmp = icmp ult <2 x i32> %a, %min = select <2 x i1> %cmp, <2 x i64> %a_ext, <2 x i64> ret <2 x i64> %min } ; Don't crash mishandling a pattern that can't be transformed. define <2 x i16> @scalar_select_of_vectors(<2 x i16> %a, <2 x i16> %b, i8 %x) { ; CHECK-LABEL: @scalar_select_of_vectors( ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], <2 x i16> [[A:%.*]], <2 x i16> [[B:%.*]] ; CHECK-NEXT: ret <2 x i16> [[SEL]] ; %cmp = icmp slt i8 %x, 0 %sel = select i1 %cmp, <2 x i16> %a, <2 x i16> %b ret <2 x i16> %sel }