; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
;
; Verify that memrchr calls with one or more constant arguments are folded
; as expected.
declare ptr @memrchr(ptr, i32, i64)
@ax = external global [0 x i8]
@a12345 = constant [5 x i8] c"\01\02\03\04\05"
@a123123 = constant [6 x i8] c"\01\02\03\01\02\03"
; Fold memrchr(ax, C, 0) to null.
define ptr @fold_memrchr_ax_c_0(i32 %C) {
; CHECK-LABEL: @fold_memrchr_ax_c_0(
; CHECK-NEXT: ret ptr null
;
%ret = call ptr @memrchr(ptr @ax, i32 %C, i64 0)
ret ptr %ret
}
; Fold memrchr(a12345, 3, 0) to null.
define ptr @fold_memrchr_a12345_3_0() {
; CHECK-LABEL: @fold_memrchr_a12345_3_0(
; CHECK-NEXT: ret ptr null
;
%ret = call ptr @memrchr(ptr @a12345, i32 3, i64 0)
ret ptr %ret
}
; Fold memrchr(a12345, 1, 1) to a12345.
define ptr @fold_memrchr_a12345_1_1() {
; CHECK-LABEL: @fold_memrchr_a12345_1_1(
; CHECK-NEXT: ret ptr @a12345
;
%ret = call ptr @memrchr(ptr @a12345, i32 1, i64 1)
ret ptr %ret
}
; Fold memrchr(a12345, 5, 1) to null.
define ptr @fold_memrchr_a12345_5_1() {
; CHECK-LABEL: @fold_memrchr_a12345_5_1(
; CHECK-NEXT: ret ptr null
;
%ret = call ptr @memrchr(ptr @a12345, i32 5, i64 1)
ret ptr %ret
}
; Fold memrchr(a123123, 1, 1) to a123123.
define ptr @fold_memrchr_a123123_1_1() {
; CHECK-LABEL: @fold_memrchr_a123123_1_1(
; CHECK-NEXT: ret ptr @a123123
;
%ret = call ptr @memrchr(ptr @a123123, i32 1, i64 1)
ret ptr %ret
}
; Fold memrchr(a123123, 3, 1) to null.
define ptr @fold_memrchr_a123123_3_1() {
; CHECK-LABEL: @fold_memrchr_a123123_3_1(
; CHECK-NEXT: ret ptr null
;
%ret = call ptr @memrchr(ptr @a123123, i32 3, i64 1)
ret ptr %ret
}
; Fold memrchr(ax, C, 1) to *ax == C ? ax : null.
define ptr @fold_memrchr_ax_c_1(i32 %C) {
; CHECK-LABEL: @fold_memrchr_ax_c_1(
; CHECK-NEXT: [[MEMRCHR_CHAR0:%.*]] = load i8, ptr @ax, align 1
; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
; CHECK-NEXT: [[MEMRCHR_CHAR0CMP:%.*]] = icmp eq i8 [[MEMRCHR_CHAR0]], [[TMP1]]
; CHECK-NEXT: [[MEMRCHR_SEL:%.*]] = select i1 [[MEMRCHR_CHAR0CMP]], ptr @ax, ptr null
; CHECK-NEXT: ret ptr [[MEMRCHR_SEL]]
;
%ret = call ptr @memrchr(ptr @ax, i32 %C, i64 1)
ret ptr %ret
}
; Fold memrchr(a12345, 5, 5) to a12345 + 4.
define ptr @fold_memrchr_a12345_5_5() {
; CHECK-LABEL: @fold_memrchr_a12345_5_5(
; CHECK-NEXT: ret ptr getelementptr inbounds (i8, ptr @a12345, i64 4)
;
%ret = call ptr @memrchr(ptr @a12345, i32 5, i64 5)
ret ptr %ret
}
; Fold memrchr(a12345, 5, 4) to null.
define ptr @fold_memrchr_a12345_5_4() {
; CHECK-LABEL: @fold_memrchr_a12345_5_4(
; CHECK-NEXT: ret ptr null
;
%ret = call ptr @memrchr(ptr @a12345, i32 5, i64 4)
ret ptr %ret
}
; Fold memrchr(a12345, 4, 5) to a12345 + 3.
define ptr @fold_memrchr_a12345_4_5() {
; CHECK-LABEL: @fold_memrchr_a12345_4_5(
; CHECK-NEXT: ret ptr getelementptr inbounds (i8, ptr @a12345, i64 3)
;
%ret = call ptr @memrchr(ptr @a12345, i32 4, i64 5)
ret ptr %ret
}
; Fold memrchr(a12345 + 1, 1, 4) to null.
define ptr @fold_memrchr_a12345p1_1_4() {
; CHECK-LABEL: @fold_memrchr_a12345p1_1_4(
; CHECK-NEXT: ret ptr null
;
%ptr = getelementptr [5 x i8], ptr @a12345, i32 0, i32 1
%ret = call ptr @memrchr(ptr %ptr, i32 1, i64 4)
ret ptr %ret
}
; Fold memrchr(a12345 + 1, 2, 4) to a12345 + 1.
define ptr @fold_memrchr_a12345p1_2_4() {
; CHECK-LABEL: @fold_memrchr_a12345p1_2_4(
; CHECK-NEXT: ret ptr getelementptr inbounds (i8, ptr @a12345, i64 1)
;
%ptr = getelementptr [5 x i8], ptr @a12345, i32 0, i32 1
%ret = call ptr @memrchr(ptr %ptr, i32 2, i64 4)
ret ptr %ret
}
; Fold memrchr(a12345, 2, 5) to a12345 + 1.
define ptr @fold_memrchr_a12345_2_5() {
; CHECK-LABEL: @fold_memrchr_a12345_2_5(
; CHECK-NEXT: ret ptr getelementptr inbounds (i8, ptr @a12345, i64 1)
;
%ret = call ptr @memrchr(ptr @a12345, i32 2, i64 5)
ret ptr %ret
}
; Fold memrchr(a12345, 0, %N) to null.
define ptr @fold_memrchr_a12345_0_n(i64 %N) {
; CHECK-LABEL: @fold_memrchr_a12345_0_n(
; CHECK-NEXT: ret ptr null
;
%ret = call ptr @memrchr(ptr @a12345, i32 0, i64 %N)
ret ptr %ret
}
; Fold memrchr(a12345, 3, n) to n < 3 ? null : s + 2.
define ptr @fold_memrchr_a12345_3_n(i64 %n) {
; CHECK-LABEL: @fold_memrchr_a12345_3_n(
; CHECK-NEXT: [[MEMRCHR_CMP:%.*]] = icmp ult i64 [[N:%.*]], 3
; CHECK-NEXT: [[MEMRCHR_SEL:%.*]] = select i1 [[MEMRCHR_CMP]], ptr null, ptr getelementptr inbounds (i8, ptr @a12345, i64 2)
; CHECK-NEXT: ret ptr [[MEMRCHR_SEL]]
;
%ret = call ptr @memrchr(ptr @a12345, i32 3, i64 %n)
ret ptr %ret
}
; Fold memrchr(a12345, 5, n) to n < 5 ? null : s + 4.
define ptr @fold_memrchr_a12345_5_n(i64 %n) {
; CHECK-LABEL: @fold_memrchr_a12345_5_n(
; CHECK-NEXT: [[MEMRCHR_CMP:%.*]] = icmp ult i64 [[N:%.*]], 5
; CHECK-NEXT: [[MEMRCHR_SEL:%.*]] = select i1 [[MEMRCHR_CMP]], ptr null, ptr getelementptr inbounds (i8, ptr @a12345, i64 4)
; CHECK-NEXT: ret ptr [[MEMRCHR_SEL]]
;
%ret = call ptr @memrchr(ptr @a12345, i32 5, i64 %n)
ret ptr %ret
}
; Fold memrchr(a123123, 3, 5) to a123123 + 2.
define ptr @fold_memrchr_a123123_3_5() {
; CHECK-LABEL: @fold_memrchr_a123123_3_5(
; CHECK-NEXT: ret ptr getelementptr inbounds (i8, ptr @a123123, i64 2)
;
%ret = call ptr @memrchr(ptr @a123123, i32 3, i64 5)
ret ptr %ret
}
; Fold memrchr(a123123, 3, 6) to a123123 + 5.
define ptr @fold_memrchr_a123123_3_6() {
; CHECK-LABEL: @fold_memrchr_a123123_3_6(
; CHECK-NEXT: ret ptr getelementptr inbounds (i8, ptr @a123123, i64 5)
;
%ret = call ptr @memrchr(ptr @a123123, i32 3, i64 6)
ret ptr %ret
}
; Fold memrchr(a123123, 2, 6) to a123123 + 4.
define ptr @fold_memrchr_a123123_2_6() {
; CHECK-LABEL: @fold_memrchr_a123123_2_6(
; CHECK-NEXT: ret ptr getelementptr inbounds (i8, ptr @a123123, i64 4)
;
%ret = call ptr @memrchr(ptr @a123123, i32 2, i64 6)
ret ptr %ret
}
; Fold memrchr(a123123, 1, 6) to a123123 + 3.
define ptr @fold_memrchr_a123123_1_6() {
; CHECK-LABEL: @fold_memrchr_a123123_1_6(
; CHECK-NEXT: ret ptr getelementptr inbounds (i8, ptr @a123123, i64 3)
;
%ret = call ptr @memrchr(ptr @a123123, i32 1, i64 6)
ret ptr %ret
}
; Fold memrchr(a123123, 0, 6) to null.
define ptr @fold_memrchr_a123123_0_6() {
; CHECK-LABEL: @fold_memrchr_a123123_0_6(
; CHECK-NEXT: ret ptr null
;
%ret = call ptr @memrchr(ptr @a123123, i32 0, i64 6)
ret ptr %ret
}
; Fold memrchr(a123123, 0, n) to null
define ptr @fold_memrchr_a123123_0_n(i64 %n) {
; CHECK-LABEL: @fold_memrchr_a123123_0_n(
; CHECK-NEXT: ret ptr null
;
%ret = call ptr @memrchr(ptr @a123123, i32 0, i64 %n)
ret ptr %ret
}
; Don't fold memrchr(a123123, 3, n) (although it's possible to fold the call
; for a small number of occurrences of the character greater than one, it's
; less and less profitable as the number grows).
define ptr @call_memrchr_a123123_3_n(i64 %n) {
; CHECK-LABEL: @call_memrchr_a123123_3_n(
; CHECK-NEXT: [[RET:%.*]] = call ptr @memrchr(ptr nonnull @a123123, i32 3, i64 [[N:%.*]])
; CHECK-NEXT: ret ptr [[RET]]
;
%ret = call ptr @memrchr(ptr @a123123, i32 3, i64 %n)
ret ptr %ret
}
; Same as above but for 2.
define ptr @call_memrchr_a123123_2_n(i64 %n) {
; CHECK-LABEL: @call_memrchr_a123123_2_n(
; CHECK-NEXT: [[RET:%.*]] = call ptr @memrchr(ptr nonnull @a123123, i32 2, i64 [[N:%.*]])
; CHECK-NEXT: ret ptr [[RET]]
;
%ret = call ptr @memrchr(ptr @a123123, i32 2, i64 %n)
ret ptr %ret
}
; And again for 1 to exercise the other edge case.
define ptr @call_memrchr_a123123_1_n(i64 %n) {
; CHECK-LABEL: @call_memrchr_a123123_1_n(
; CHECK-NEXT: [[RET:%.*]] = call ptr @memrchr(ptr nonnull @a123123, i32 1, i64 [[N:%.*]])
; CHECK-NEXT: ret ptr [[RET]]
;
%ret = call ptr @memrchr(ptr @a123123, i32 1, i64 %n)
ret ptr %ret
}