; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -instcombine -S | FileCheck %s define i32 @extractelement_in_range( %a) { ; CHECK-LABEL: @extractelement_in_range( ; CHECK-NEXT: [[R:%.*]] = extractelement [[A:%.*]], i64 1 ; CHECK-NEXT: ret i32 [[R]] ; %r = extractelement %a, i64 1 ret i32 %r } define i32 @extractelement_maybe_out_of_range( %a) { ; CHECK-LABEL: @extractelement_maybe_out_of_range( ; CHECK-NEXT: [[R:%.*]] = extractelement [[A:%.*]], i64 4 ; CHECK-NEXT: ret i32 [[R]] ; %r = extractelement %a, i64 4 ret i32 %r } define i32 @extractelement_bitcast(float %f) { ; CHECK-LABEL: @extractelement_bitcast( ; CHECK-NEXT: [[R:%.*]] = bitcast float [[F:%.*]] to i32 ; CHECK-NEXT: ret i32 [[R]] ; %vec_float = insertelement undef, float %f, i32 0 %vec_int = bitcast %vec_float to %r = extractelement %vec_int, i32 0 ret i32 %r } define i8 @extractelement_bitcast_to_trunc( %a, i32 %x) { ; CHECK-LABEL: @extractelement_bitcast_to_trunc( ; CHECK-NEXT: [[R:%.*]] = trunc i32 [[X:%.*]] to i8 ; CHECK-NEXT: ret i8 [[R]] ; %vec = insertelement %a, i32 %x, i32 1 %vec_cast = bitcast %vec to %r = extractelement %vec_cast, i32 4 ret i8 %r } ; TODO: Instcombine could remove the insert. define i8 @extractelement_bitcast_wrong_insert( %a, i32 %x) { ; CHECK-LABEL: @extractelement_bitcast_wrong_insert( ; CHECK-NEXT: [[VEC:%.*]] = insertelement [[A:%.*]], i32 [[X:%.*]], i32 1 ; CHECK-NEXT: [[VEC_CAST:%.*]] = bitcast [[VEC]] to ; CHECK-NEXT: [[R:%.*]] = extractelement [[VEC_CAST]], i32 2 ; CHECK-NEXT: ret i8 [[R]] ; %vec = insertelement %a, i32 %x, i32 1 ; <- This insert could be removed. %vec_cast = bitcast %vec to %r = extractelement %vec_cast, i32 2 ret i8 %r } ; TODO: Instcombine could optimize to return %v. define i32 @extractelement_shuffle_in_range(i32 %v) { ; CHECK-LABEL: @extractelement_shuffle_in_range( ; CHECK-NEXT: [[IN:%.*]] = insertelement undef, i32 [[V:%.*]], i32 0 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector [[IN]], undef, zeroinitializer ; CHECK-NEXT: [[R:%.*]] = extractelement [[SPLAT]], i32 1 ; CHECK-NEXT: ret i32 [[R]] ; %in = insertelement undef, i32 %v, i32 0 %splat = shufflevector %in, undef, zeroinitializer %r = extractelement %splat, i32 1 ret i32 %r } define i32 @extractelement_shuffle_maybe_out_of_range(i32 %v) { ; CHECK-LABEL: @extractelement_shuffle_maybe_out_of_range( ; CHECK-NEXT: [[IN:%.*]] = insertelement undef, i32 [[V:%.*]], i32 0 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector [[IN]], undef, zeroinitializer ; CHECK-NEXT: [[R:%.*]] = extractelement [[SPLAT]], i32 4 ; CHECK-NEXT: ret i32 [[R]] ; %in = insertelement undef, i32 %v, i32 0 %splat = shufflevector %in, undef, zeroinitializer %r = extractelement %splat, i32 4 ret i32 %r } define i32 @extractelement_shuffle_invalid_index(i32 %v) { ; CHECK-LABEL: @extractelement_shuffle_invalid_index( ; CHECK-NEXT: [[IN:%.*]] = insertelement undef, i32 [[V:%.*]], i32 0 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector [[IN]], undef, zeroinitializer ; CHECK-NEXT: [[R:%.*]] = extractelement [[SPLAT]], i32 -1 ; CHECK-NEXT: ret i32 [[R]] ; %in = insertelement undef, i32 %v, i32 0 %splat = shufflevector %in, undef, zeroinitializer %r = extractelement %splat, i32 -1 ret i32 %r } define i32 @extractelement_shuffle_symbolic_index(i32 %v, i32 %idx) { ; CHECK-LABEL: @extractelement_shuffle_symbolic_index( ; CHECK-NEXT: [[IN:%.*]] = insertelement undef, i32 [[V:%.*]], i32 0 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector [[IN]], undef, zeroinitializer ; CHECK-NEXT: [[R:%.*]] = extractelement [[SPLAT]], i32 [[IDX:%.*]] ; CHECK-NEXT: ret i32 [[R]] ; %in = insertelement undef, i32 %v, i32 0 %splat = shufflevector %in, undef, zeroinitializer %r = extractelement %splat, i32 %idx ret i32 %r } define @extractelement_insertelement_same_positions( %vec) { ; CHECK-LABEL: @extractelement_insertelement_same_positions( ; CHECK-NEXT: ret [[VEC:%.*]] ; %vec.e0 = extractelement %vec, i32 0 %vec.e1 = extractelement %vec, i32 1 %vec.e2 = extractelement %vec, i32 2 %vec.e3 = extractelement %vec, i32 3 %1 = insertelement %vec, i32 %vec.e0, i32 0 %2 = insertelement %1, i32 %vec.e1, i32 1 %3 = insertelement %2, i32 %vec.e2, i32 2 %4 = insertelement %3, i32 %vec.e3, i32 3 ret %4 } define @extractelement_insertelement_diff_positions( %vec) { ; CHECK-LABEL: @extractelement_insertelement_diff_positions( ; CHECK-NEXT: [[VEC_E0:%.*]] = extractelement [[VEC:%.*]], i32 4 ; CHECK-NEXT: [[VEC_E1:%.*]] = extractelement [[VEC]], i32 5 ; CHECK-NEXT: [[VEC_E2:%.*]] = extractelement [[VEC]], i32 6 ; CHECK-NEXT: [[VEC_E3:%.*]] = extractelement [[VEC]], i32 7 ; CHECK-NEXT: [[TMP1:%.*]] = insertelement [[VEC]], i32 [[VEC_E0]], i32 0 ; CHECK-NEXT: [[TMP2:%.*]] = insertelement [[TMP1]], i32 [[VEC_E1]], i32 1 ; CHECK-NEXT: [[TMP3:%.*]] = insertelement [[TMP2]], i32 [[VEC_E2]], i32 2 ; CHECK-NEXT: [[TMP4:%.*]] = insertelement [[TMP3]], i32 [[VEC_E3]], i32 3 ; CHECK-NEXT: ret [[TMP4]] ; %vec.e0 = extractelement %vec, i32 4 %vec.e1 = extractelement %vec, i32 5 %vec.e2 = extractelement %vec, i32 6 %vec.e3 = extractelement %vec, i32 7 %1 = insertelement %vec, i32 %vec.e0, i32 0 %2 = insertelement %1, i32 %vec.e1, i32 1 %3 = insertelement %2, i32 %vec.e2, i32 2 %4 = insertelement %3, i32 %vec.e3, i32 3 ret %4 } define i32 @bitcast_of_extractelement( %d) { ; CHECK-LABEL: @bitcast_of_extractelement( ; CHECK-NEXT: [[BC:%.*]] = bitcast [[D:%.*]] to ; CHECK-NEXT: [[CAST:%.*]] = extractelement [[BC]], i32 0 ; CHECK-NEXT: ret i32 [[CAST]] ; %ext = extractelement %d, i32 0 %cast = bitcast float %ext to i32 ret i32 %cast } define i1 @extractelement_is_zero( %d, i1 %b, i32 %z) { ; CHECK-LABEL: @extractelement_is_zero( ; CHECK-NEXT: [[EXT:%.*]] = extractelement [[D:%.*]], i32 0 ; CHECK-NEXT: [[BB:%.*]] = icmp eq i32 [[EXT]], 0 ; CHECK-NEXT: ret i1 [[BB]] ; %ext = extractelement %d, i32 0 %bb = icmp eq i32 %ext, 0 ret i1 %bb } ; OSS-Fuzz #25272 ; https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25272 define i32 @ossfuzz_25272(float %f) { ; CHECK-LABEL: @ossfuzz_25272( ; CHECK-NEXT: [[VEC_FLOAT:%.*]] = insertelement undef, float [[F:%.*]], i32 0 ; CHECK-NEXT: [[VEC_INT:%.*]] = bitcast [[VEC_FLOAT]] to ; CHECK-NEXT: [[E:%.*]] = extractelement [[VEC_INT]], i32 2147483647 ; CHECK-NEXT: ret i32 [[E]] ; %vec_float = insertelement undef, float %f, i32 0 %vec_int = bitcast %vec_float to %E = extractelement %vec_int, i32 2147483647 ret i32 %E }