File: strnlen-1.ll

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (172 lines) | stat: -rw-r--r-- 4,385 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; Verify that strnlen calls with constant string arguments and offsets
; and constant bounds are folded correctly.
;
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

declare i64 @strnlen(ptr, i64)

@ax = external global [0 x i8]
@s5 = constant [6 x i8] c"12345\00"
@s5_3 = constant [9 x i8] c"12345\00xyz"


; Verify that the strnlen pointer argument is not annotated nonnull when
; nothing is known about the bound.

define i64 @no_access_strnlen_p_n(ptr %ptr, i64 %n) {
; CHECK-LABEL: @no_access_strnlen_p_n(
; CHECK-NEXT:    [[LEN:%.*]] = call i64 @strnlen(ptr [[PTR:%.*]], i64 [[N:%.*]])
; CHECK-NEXT:    ret i64 [[LEN]]
;
  %len = call i64 @strnlen(ptr %ptr, i64 %n)
  ret i64 %len
}


; Verify that the strnlen pointer argument is annotated dereferenceable(1)
; (and not more) when the constant bound is greater than 1.

define i64 @access_strnlen_p_2(ptr %ptr) {
; CHECK-LABEL: @access_strnlen_p_2(
; CHECK-NEXT:    [[LEN:%.*]] = call i64 @strnlen(ptr noundef nonnull dereferenceable(1) [[PTR:%.*]], i64 2)
; CHECK-NEXT:    ret i64 [[LEN]]
;
  %len = call i64 @strnlen(ptr noundef nonnull dereferenceable(1) %ptr, i64 2)
  ret i64 %len
}


; Verify that the strnlen pointer argument is annotated nonnull etc.,
; when the bound is known to be nonzero.

define i64 @access_strnlen_p_nz(ptr %ptr, i64 %n) {
; CHECK-LABEL: @access_strnlen_p_nz(
; CHECK-NEXT:    [[NNZ:%.*]] = or i64 [[N:%.*]], 1
; CHECK-NEXT:    [[LEN:%.*]] = call i64 @strnlen(ptr noundef nonnull dereferenceable(1) [[PTR:%.*]], i64 [[NNZ]])
; CHECK-NEXT:    ret i64 [[LEN]]
;
  %nnz = or i64 %n, 1
  %len = call i64 @strnlen(ptr noundef nonnull dereferenceable(1) %ptr, i64 %nnz)
  ret i64 %len
}


; Fold strnlen(ax, 0) to 0.

define i64 @fold_strnlen_ax_0() {
; CHECK-LABEL: @fold_strnlen_ax_0(
; CHECK-NEXT:    ret i64 0
;
  %len = call i64 @strnlen(ptr @ax, i64 0)
  ret i64 %len
}


; Fold strnlen(ax, 1) to *ax ? 1 : 0.

define i64 @fold_strnlen_ax_1() {
; CHECK-LABEL: @fold_strnlen_ax_1(
; CHECK-NEXT:    [[STRNLEN_CHAR0:%.*]] = load i8, ptr @ax, align 1
; CHECK-NEXT:    [[STRNLEN_CHAR0CMP:%.*]] = icmp ne i8 [[STRNLEN_CHAR0]], 0
; CHECK-NEXT:    [[LEN:%.*]] = zext i1 [[STRNLEN_CHAR0CMP]] to i64
; CHECK-NEXT:    ret i64 [[LEN]]
;
  %len = call i64 @strnlen(ptr @ax, i64 1)
  ret i64 %len
}


; Fold strnlen(s5, 0) to 0.

define i64 @fold_strnlen_s5_0() {
; CHECK-LABEL: @fold_strnlen_s5_0(
; CHECK-NEXT:    ret i64 0
;
  %len = call i64 @strnlen(ptr @s5, i64 0)
  ret i64 %len
}


; Fold strnlen(s5, 4) to 4.

define i64 @fold_strnlen_s5_4() {
; CHECK-LABEL: @fold_strnlen_s5_4(
; CHECK-NEXT:    ret i64 4
;
  %len = call i64 @strnlen(ptr @s5, i64 4)
  ret i64 %len
}


; Fold strnlen(s5, 5) to 5.

define i64 @fold_strnlen_s5_5() {
; CHECK-LABEL: @fold_strnlen_s5_5(
; CHECK-NEXT:    ret i64 5
;
  %len = call i64 @strnlen(ptr @s5, i64 5)
  ret i64 %len
}


; Fold strnlen(s5, (size_t)-1) to 5.

define i64 @fold_strnlen_s5_m1() {
; CHECK-LABEL: @fold_strnlen_s5_m1(
; CHECK-NEXT:    ret i64 5
;
  %len = call i64 @strnlen(ptr @s5, i64 -1)
  ret i64 %len
}


; Fold strnlen(s5_3 + 4, 5) to 1.

define i64 @fold_strnlen_s5_3_p4_5() {
; CHECK-LABEL: @fold_strnlen_s5_3_p4_5(
; CHECK-NEXT:    ret i64 1
;
  %ptr = getelementptr [9 x i8], ptr @s5_3, i32 0, i32 4
  %len = call i64 @strnlen(ptr %ptr, i64 5)
  ret i64 %len
}


; Fold strnlen(s5_3 + 5, 5) to 0.

define i64 @fold_strnlen_s5_3_p5_5() {
; CHECK-LABEL: @fold_strnlen_s5_3_p5_5(
; CHECK-NEXT:    ret i64 0
;
  %ptr = getelementptr [9 x i8], ptr @s5_3, i32 0, i32 5
  %len = call i64 @strnlen(ptr %ptr, i64 5)
  ret i64 %len
}


; Fold strnlen(s5_3 + 6, 3) to 3.

define i64 @fold_strnlen_s5_3_p6_3() {
; CHECK-LABEL: @fold_strnlen_s5_3_p6_3(
; CHECK-NEXT:    ret i64 3
;
  %ptr = getelementptr [9 x i8], ptr @s5_3, i32 0, i32 6
  %len = call i64 @strnlen(ptr %ptr, i64 3)
  ret i64 %len
}


; Fold even the invalid strnlen(s5_3 + 6, 4) call where the bound exceeds
; the number of characters in the array.  This is arguably safer than
; making the library call (although the low bound makes it unlikely that
; the call would misbehave).

define i64 @call_strnlen_s5_3_p6_4() {
; CHECK-LABEL: @call_strnlen_s5_3_p6_4(
; CHECK-NEXT:    ret i64 3
;
  %ptr = getelementptr [9 x i8], ptr @s5_3, i32 0, i32 6
  %len = call i64 @strnlen(ptr %ptr, i64 4)
  ret i64 %len
}