File: strchr-4.ll

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (79 lines) | stat: -rw-r--r-- 2,211 bytes parent folder | download
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
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
;
; Verify that the result of strchr calls used in equality expressions
; with either the first argument or null are optimally folded.

declare i8* @strchr(i8*, i32)


; Fold strchr(s, c) == s to *s == c.

define i1 @fold_strchr_s_c_eq_s(i8* %s, i32 %c) {
; CHECK-LABEL: @fold_strchr_s_c_eq_s(
; CHECK-NEXT:    [[TMP1:%.*]] = load i8, i8* [[S:%.*]], align 1
; CHECK-NEXT:    [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i8
; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], [[TMP2]]
; CHECK-NEXT:    ret i1 [[CHAR0CMP]]
;
  %p = call i8* @strchr(i8* %s, i32 %c)
  %cmp = icmp eq i8* %p, %s
  ret i1 %cmp
}


; Fold strchr(s, c) != s to *s != c.

define i1 @fold_strchr_s_c_neq_s(i8* %s, i32 %c) {
; CHECK-LABEL: @fold_strchr_s_c_neq_s(
; CHECK-NEXT:    [[TMP1:%.*]] = load i8, i8* [[S:%.*]], align 1
; CHECK-NEXT:    [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i8
; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp ne i8 [[TMP1]], [[TMP2]]
; CHECK-NEXT:    ret i1 [[CHAR0CMP]]
;
  %p = call i8* @strchr(i8* %s, i32 %c)
  %cmp = icmp ne i8* %p, %s
  ret i1 %cmp
}


; Fold strchr(s, '\0') == null to false.  (A string must be nul-terminated,
; otherwise the call would read past the end of the array.)

define i1 @fold_strchr_s_nul_eqz(i8* %s) {
; CHECK-LABEL: @fold_strchr_s_nul_eqz(
; CHECK-NEXT:    ret i1 false
;
  %p = call i8* @strchr(i8* %s, i32 0)
  %cmp = icmp eq i8* %p, null
  ret i1 %cmp
}


; Fold strchr(s, '\0') != null to true.

define i1 @fold_strchr_s_nul_nez(i8* %s) {
; CHECK-LABEL: @fold_strchr_s_nul_nez(
; CHECK-NEXT:    ret i1 true
;
  %p = call i8* @strchr(i8* %s, i32 0)
  %cmp = icmp ne i8* %p, null
  ret i1 %cmp
}


@a5 = constant [5 x i8] c"12345";

; Fold strchr(a5, c) == a5 to *a5 == c.

define i1 @fold_strchr_a_c_eq_a(i32 %c) {
; CHECK-LABEL: @fold_strchr_a_c_eq_a(
; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 49
; CHECK-NEXT:    ret i1 [[CHAR0CMP]]
;
  %p = getelementptr [5 x i8], [5 x i8]* @a5, i32 0, i32 0
  %q = call i8* @strchr(i8* %p, i32 %c)
  %cmp = icmp eq i8* %q, %p
  ret i1 %cmp
}