xref: /llvm-project/llvm/test/Transforms/FunctionSpecialization/global-var-constants.ll (revision 93de97d750548cd90c53efd4367dbd0367aa30fd)
1; RUN:  opt -S --passes=ipsccp,deadargelim --force-specialization                       < %s | FileCheck %s --check-prefix=NO-GLOBALS
2; RUN:  opt -S --passes=ipsccp,deadargelim --force-specialization --funcspec-on-address < %s | FileCheck %s --check-prefix=GLOBALS
3@G = global [10 x i32] zeroinitializer, align 4
4
5define internal i32 @f(ptr %p) noinline {
6entry:
7  %0 = load i32, ptr %p, align 4
8  store i32 0, ptr %p, align 4
9  ret i32 %0
10}
11
12define internal i32 @g(i32 %x, i32 %y, ptr %p) noinline {
13entry:
14  %cmp = icmp sgt i32 %x, %y
15  br i1 %cmp, label %if.then, label %if.else
16
17if.then:
18  br label %if.end
19
20if.else:
21  br label %if.end
22
23if.end:
24  %x.addr.0 = phi i32 [ %x, %if.then ], [ 11, %if.else ]
25  %p.addr.0 = phi ptr [ @G, %if.then ], [ %p, %if.else ]
26  %call = call i32 @f(ptr %p.addr.0)
27  %add = add nsw i32 %call, %x.addr.0
28  ret i32 %add
29}
30
31define i32 @h0(ptr %p) {
32entry:
33  %call = call i32 @g(i32 2, i32 1, ptr %p)
34  ret i32 %call
35}
36
37define i32 @h1() {
38entry:
39  %call = call i32 @f(ptr @G)
40  ret i32 %call
41}
42
43define i32 @h2() {
44entry:
45  %call = call i32 @f(ptr getelementptr inbounds (i32, ptr @G, i64 1))
46  ret i32 %call
47}
48
49; Check if specialisation on the address of a non-const global variable
50; is not allowed, then it is not performed.
51
52; NO-GLOBALS-LABEL: define internal range(i32 -2147483646, -2147483648) i32 @g()
53; NO-GLOBALS: call i32 @f(ptr @G)
54
55; NO-GLOBALS-LABEL: define range(i32 -2147483646, -2147483648) i32 @h0(ptr %p)
56; NO-GLOBALS:call i32 @g()
57
58; NO-GLOBALS-LABEL: define i32 @h1()
59; NO-GLOBALS: call i32 @f(ptr @G)
60
61; NO-GLOBALS-LABEL: define i32 @h2()
62; NO-GLOBALS: call i32 @f(ptr getelementptr inbounds (i32, ptr @G, i64 1))
63
64; Check if specialisation on the address of a non-const global variable
65; is allowed, then it is performed where possible.
66
67; GLOBALS-LABEL: define internal range(i32 -2147483646, -2147483648) i32 @g()
68; GLOBALS: call i32 @f.specialized.2()
69
70; GLOBALS-LABEL: define range(i32 -2147483646, -2147483648) i32 @h0(ptr %p)
71; GLOBALS: call i32 @g()
72
73; GLOBALS-LABEL: define i32 @h1()
74; GLOBALS: call i32 @f.specialized.2()
75
76; GLOBALS-LABEL: define i32 @h2()
77; GLOBALS: call i32 @f.specialized.1()
78
79