xref: /aosp_15_r20/external/llvm/test/Analysis/GlobalsModRef/nonescaping-noalias.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -globals-aa -gvn -S | FileCheck %s
2*9880d681SAndroid Build Coastguard Worker;
3*9880d681SAndroid Build Coastguard Worker; This tests the safe no-alias conclusions of GMR -- when there is
4*9880d681SAndroid Build Coastguard Worker; a non-escaping global as one indentified underlying object and some pointer
5*9880d681SAndroid Build Coastguard Worker; that would inherently have escaped any other function as the other underlying
6*9880d681SAndroid Build Coastguard Worker; pointer of an alias query.
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard Worker@g1 = internal global i32 0
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Workerdefine i32 @test1(i32* %param) {
11*9880d681SAndroid Build Coastguard Worker; Ensure that we can fold a store to a load of a global across a store to
12*9880d681SAndroid Build Coastguard Worker; a parameter when the global is non-escaping.
13*9880d681SAndroid Build Coastguard Worker;
14*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test1(
15*9880d681SAndroid Build Coastguard Worker; CHECK: store i32 42, i32* @g1
16*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: load i32
17*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32 42
18*9880d681SAndroid Build Coastguard Workerentry:
19*9880d681SAndroid Build Coastguard Worker  store i32 42, i32* @g1
20*9880d681SAndroid Build Coastguard Worker  store i32 7, i32* %param
21*9880d681SAndroid Build Coastguard Worker  %v = load i32, i32* @g1
22*9880d681SAndroid Build Coastguard Worker  ret i32 %v
23*9880d681SAndroid Build Coastguard Worker}
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Workerdeclare i32* @f()
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Workerdefine i32 @test2() {
28*9880d681SAndroid Build Coastguard Worker; Ensure that we can fold a store to a load of a global across a store to
29*9880d681SAndroid Build Coastguard Worker; the pointer returned by a function call. Since the global could not escape,
30*9880d681SAndroid Build Coastguard Worker; this function cannot be returning its address.
31*9880d681SAndroid Build Coastguard Worker;
32*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test2(
33*9880d681SAndroid Build Coastguard Worker; CHECK: store i32 42, i32* @g1
34*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: load i32
35*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32 42
36*9880d681SAndroid Build Coastguard Workerentry:
37*9880d681SAndroid Build Coastguard Worker  %ptr = call i32* @f() readnone
38*9880d681SAndroid Build Coastguard Worker  store i32 42, i32* @g1
39*9880d681SAndroid Build Coastguard Worker  store i32 7, i32* %ptr
40*9880d681SAndroid Build Coastguard Worker  %v = load i32, i32* @g1
41*9880d681SAndroid Build Coastguard Worker  ret i32 %v
42*9880d681SAndroid Build Coastguard Worker}
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker@g2 = external global i32*
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Workerdefine i32 @test3() {
47*9880d681SAndroid Build Coastguard Worker; Ensure that we can fold a store to a load of a global across a store to
48*9880d681SAndroid Build Coastguard Worker; the pointer loaded from that global. Because the global does not escape, it
49*9880d681SAndroid Build Coastguard Worker; cannot alias a pointer loaded out of a global.
50*9880d681SAndroid Build Coastguard Worker;
51*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test3(
52*9880d681SAndroid Build Coastguard Worker; CHECK: store i32 42, i32* @g1
53*9880d681SAndroid Build Coastguard Worker; CHECK: store i32 7, i32*
54*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: load i32
55*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32 42
56*9880d681SAndroid Build Coastguard Workerentry:
57*9880d681SAndroid Build Coastguard Worker  store i32 42, i32* @g1
58*9880d681SAndroid Build Coastguard Worker  %ptr1 = load i32*, i32** @g2
59*9880d681SAndroid Build Coastguard Worker  store i32 7, i32* %ptr1
60*9880d681SAndroid Build Coastguard Worker  %v = load i32, i32* @g1
61*9880d681SAndroid Build Coastguard Worker  ret i32 %v
62*9880d681SAndroid Build Coastguard Worker}
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker@g3 = internal global i32 1
65*9880d681SAndroid Build Coastguard Worker@g4 = internal global [10 x i32*] zeroinitializer
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Workerdefine i32 @test4(i32* %param, i32 %n, i1 %c1, i1 %c2, i1 %c3) {
68*9880d681SAndroid Build Coastguard Worker; Ensure that we can fold a store to a load of a global across a store to
69*9880d681SAndroid Build Coastguard Worker; the pointer loaded from that global even when the load is behind PHIs and
70*9880d681SAndroid Build Coastguard Worker; selects, and there is a mixture of a load and another global or argument.
71*9880d681SAndroid Build Coastguard Worker; Note that we can't eliminate the load here because it is used in a PHI and
72*9880d681SAndroid Build Coastguard Worker; GVN doesn't try to do real DCE. The store is still forwarded by GVN though.
73*9880d681SAndroid Build Coastguard Worker;
74*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test4(
75*9880d681SAndroid Build Coastguard Worker; CHECK: store i32 42, i32* @g1
76*9880d681SAndroid Build Coastguard Worker; CHECK: store i32 7, i32*
77*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32 42
78*9880d681SAndroid Build Coastguard Workerentry:
79*9880d681SAndroid Build Coastguard Worker  %call = call i32* @f()
80*9880d681SAndroid Build Coastguard Worker  store i32 42, i32* @g1
81*9880d681SAndroid Build Coastguard Worker  %ptr1 = load i32*, i32** @g2
82*9880d681SAndroid Build Coastguard Worker  %ptr2 = select i1 %c1, i32* %ptr1, i32* %param
83*9880d681SAndroid Build Coastguard Worker  %ptr3 = select i1 %c3, i32* %ptr2, i32* @g3
84*9880d681SAndroid Build Coastguard Worker  br label %loop
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Workerloop:
87*9880d681SAndroid Build Coastguard Worker  %iv = phi i32 [ 0, %entry ], [ %inc, %loop ]
88*9880d681SAndroid Build Coastguard Worker  %ptr = phi i32* [ %ptr3, %entry ], [ %ptr5, %loop ]
89*9880d681SAndroid Build Coastguard Worker  store i32 7, i32* %ptr
90*9880d681SAndroid Build Coastguard Worker  %ptr4 = load i32*, i32** getelementptr ([10 x i32*], [10 x i32*]* @g4, i32 0, i32 1)
91*9880d681SAndroid Build Coastguard Worker  %ptr5 = select i1 %c2, i32* %ptr4, i32* %call
92*9880d681SAndroid Build Coastguard Worker  %inc = add i32 %iv, 1
93*9880d681SAndroid Build Coastguard Worker  %test = icmp slt i32 %inc, %n
94*9880d681SAndroid Build Coastguard Worker  br i1 %test, label %loop, label %exit
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Workerexit:
97*9880d681SAndroid Build Coastguard Worker  %v = load i32, i32* @g1
98*9880d681SAndroid Build Coastguard Worker  ret i32 %v
99*9880d681SAndroid Build Coastguard Worker}
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Workerdefine i32 @test5(i32** %param) {
102*9880d681SAndroid Build Coastguard Worker; Ensure that we can fold a store to a load of a global across a store to
103*9880d681SAndroid Build Coastguard Worker; a parameter that has been dereferenced when the global is non-escaping.
104*9880d681SAndroid Build Coastguard Worker;
105*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test5(
106*9880d681SAndroid Build Coastguard Worker; CHECK: %p = load i32*
107*9880d681SAndroid Build Coastguard Worker; CHECK: store i32 42, i32* @g1
108*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: load i32
109*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32 42
110*9880d681SAndroid Build Coastguard Workerentry:
111*9880d681SAndroid Build Coastguard Worker  %p = load i32*, i32** %param
112*9880d681SAndroid Build Coastguard Worker  store i32 42, i32* @g1
113*9880d681SAndroid Build Coastguard Worker  store i32 7, i32* %p
114*9880d681SAndroid Build Coastguard Worker  %v = load i32, i32* @g1
115*9880d681SAndroid Build Coastguard Worker  ret i32 %v
116*9880d681SAndroid Build Coastguard Worker}
117