xref: /llvm-project/llvm/test/Transforms/InstCombine/memchr-4.ll (revision 10f315dc9c96ec2413881ab55a285e35d80def88)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3
4; Verify that an excessive size to memchr() isn't truncated to an in-bounds
5; value that results in the call being incorrectly folded (as might happen
6; when LLVM is compiled in ILP32 mode).
7
8declare ptr @memchr(ptr, i32, i64)
9
10@ax = external global [0 x i8]
11@a12345 = constant [5 x i8] c"\01\02\03\04\05"
12
13
14; Do not fold memchr(ax, 1, UINT_MAX + (size_t)1) to null.  Only the first
15; byte in ax must be dereferenceable.
16
17define ptr @call_memchr_ax_2_uimax_p1() {
18; CHECK-LABEL: @call_memchr_ax_2_uimax_p1(
19; CHECK-NEXT:    [[RES:%.*]] = call ptr @memchr(ptr noundef nonnull dereferenceable(1) @ax, i32 1, i64 4294967296)
20; CHECK-NEXT:    ret ptr [[RES]]
21;
22
23  %res = call ptr @memchr(ptr @ax, i32 1, i64 4294967296)
24  ret ptr %res
25}
26
27
28; Do not fold memchr(ax, 1, UINT_MAX + (size_t)2) to *ax == 1 ? ax : null.
29; As above, only the first byte in ax must be dereferenceable.
30
31define ptr @call_memchr_ax_2_uimax_p2() {
32; CHECK-LABEL: @call_memchr_ax_2_uimax_p2(
33; CHECK-NEXT:    [[RES:%.*]] = call ptr @memchr(ptr noundef nonnull dereferenceable(1) @ax, i32 1, i64 4294967296)
34; CHECK-NEXT:    ret ptr [[RES]]
35;
36
37  %res = call ptr @memchr(ptr @ax, i32 1, i64 4294967296)
38  ret ptr %res
39}
40
41
42; Fold memchr(a12345, 3, UINT_MAX + (size_t)2) to a12345 + 2 (and not to
43; null).
44
45define ptr @fold_memchr_a12345_3_uimax_p2() {
46; CHECK-LABEL: @fold_memchr_a12345_3_uimax_p2(
47; CHECK-NEXT:    ret ptr getelementptr inbounds nuw (i8, ptr @a12345, i64 2)
48;
49
50  %res = call ptr @memchr(ptr @a12345, i32 3, i64 4294967297)
51  ret ptr %res
52}
53
54
55; Do not fold memchr(a12345, c, UINT_MAX + (size_t)2).
56
57define ptr @fold_memchr_a12345_c_uimax_p2(i32 %0) {
58; CHECK-LABEL: @fold_memchr_a12345_c_uimax_p2(
59; CHECK-NEXT:    [[RES:%.*]] = call ptr @memchr(ptr noundef nonnull dereferenceable(1) @a12345, i32 [[TMP0:%.*]], i64 4294967297)
60; CHECK-NEXT:    ret ptr [[RES]]
61;
62
63  %res = call ptr @memchr(ptr @a12345, i32 %0, i64 4294967297)
64  ret ptr %res
65}
66