1; RUN: opt -instsimplify -S < %s | FileCheck %s
2target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3target triple = "x86_64-unknown-linux-gnu"
4
5@g1 = global i32 0, align 4
6@g2 = internal global i32 0, align 4
7@g3 = unnamed_addr global i32 0, align 4
8@g4 = hidden global i32 0, align 4
9@g5 = protected global i32 0, align 4
10@g6 = thread_local unnamed_addr global i32 0, align 4
11
12; Make sure we can simplify away a pointer comparison between
13; dynamically-allocated memory and a local stack allocation.
14;   void p()
15;   {
16;     int *mData;
17;     int mStackData[10];
18;     mData = new int[12];
19;     if (mData != mStackData) {
20;       delete[] mData;
21;     }
22;   }
23
24define void @_Z2p1v() #0 {
25  %mStackData = alloca [10 x i32], align 16
26  %1 = bitcast [10 x i32]* %mStackData to i8*
27  %2 = tail call noalias i8* @_Znam(i64 48) #4
28  %3 = bitcast i8* %2 to i32*
29  %4 = getelementptr inbounds [10 x i32], [10 x i32]* %mStackData, i64 0, i64 0
30  %5 = icmp eq i32* %3, %4
31  br i1 %5, label %7, label %6
32
33; CHECK-LABEL: @_Z2p1v
34; CHECK-NOT: icmp
35; CHECK: ret void
36
37; <label>:6                                       ; preds = %0
38  call void @_ZdaPv(i8* %2) #5
39  br label %7
40
41; <label>:7                                       ; preds = %0, %6
42  ret void
43}
44
45; Also check a more-complicated case with multiple underlying objects.
46
47define void @_Z2p2bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
48  %mStackData = alloca [10 x i32], align 16
49  %1 = bitcast [10 x i32]* %mStackData to i8*
50  %2 = getelementptr inbounds [10 x i32], [10 x i32]* %mStackData, i64 0, i64 0
51  %3 = select i1 %b1, i32* %2, i32* @g2
52  %4 = tail call noalias i8* @_Znam(i64 48) #4
53  %5 = tail call noalias i8* @_Znam(i64 48) #4
54  %.v = select i1 %b2, i8* %4, i8* %5
55  %6 = bitcast i8* %.v to i32*
56  %7 = icmp eq i32* %6, %3
57  br i1 %7, label %9, label %8
58
59; CHECK-LABEL: @_Z2p2bb
60; CHECK-NOT: icmp
61; CHECK: ret void
62
63; <label>:8                                       ; preds = %0
64  call void @_ZdaPv(i8* %4) #5
65  call void @_ZdaPv(i8* %5) #5
66  br label %9
67
68; <label>:9                                       ; preds = %0, %8
69  ret void
70}
71
72define void @_Z2p4bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
73  %mStackData = alloca [10 x i32], align 16
74  %1 = bitcast [10 x i32]* %mStackData to i8*
75  %2 = getelementptr inbounds [10 x i32], [10 x i32]* %mStackData, i64 0, i64 0
76  %3 = select i1 %b1, i32* %2, i32* @g3
77  %4 = tail call noalias i8* @_Znam(i64 48) #4
78  %5 = tail call noalias i8* @_Znam(i64 48) #4
79  %.v = select i1 %b2, i8* %4, i8* %5
80  %6 = bitcast i8* %.v to i32*
81  %7 = icmp eq i32* %6, %3
82  br i1 %7, label %9, label %8
83
84; CHECK-LABEL: @_Z2p4bb
85; CHECK-NOT: icmp
86; CHECK: ret void
87
88; <label>:8                                       ; preds = %0
89  call void @_ZdaPv(i8* %4) #5
90  call void @_ZdaPv(i8* %5) #5
91  br label %9
92
93; <label>:9                                       ; preds = %0, %8
94  ret void
95}
96
97define void @_Z2p5bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
98  %mStackData = alloca [10 x i32], align 16
99  %1 = bitcast [10 x i32]* %mStackData to i8*
100  %2 = getelementptr inbounds [10 x i32], [10 x i32]* %mStackData, i64 0, i64 0
101  %3 = select i1 %b1, i32* %2, i32* @g4
102  %4 = tail call noalias i8* @_Znam(i64 48) #4
103  %5 = tail call noalias i8* @_Znam(i64 48) #4
104  %.v = select i1 %b2, i8* %4, i8* %5
105  %6 = bitcast i8* %.v to i32*
106  %7 = icmp eq i32* %6, %3
107  br i1 %7, label %9, label %8
108
109; CHECK-LABEL: @_Z2p5bb
110; CHECK-NOT: icmp
111; CHECK: ret void
112
113; <label>:8                                       ; preds = %0
114  call void @_ZdaPv(i8* %4) #5
115  call void @_ZdaPv(i8* %5) #5
116  br label %9
117
118; <label>:9                                       ; preds = %0, %8
119  ret void
120}
121
122define void @_Z2p6bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
123  %mStackData = alloca [10 x i32], align 16
124  %1 = bitcast [10 x i32]* %mStackData to i8*
125  %2 = getelementptr inbounds [10 x i32], [10 x i32]* %mStackData, i64 0, i64 0
126  %3 = select i1 %b1, i32* %2, i32* @g5
127  %4 = tail call noalias i8* @_Znam(i64 48) #4
128  %5 = tail call noalias i8* @_Znam(i64 48) #4
129  %.v = select i1 %b2, i8* %4, i8* %5
130  %6 = bitcast i8* %.v to i32*
131  %7 = icmp eq i32* %6, %3
132  br i1 %7, label %9, label %8
133
134; CHECK-LABEL: @_Z2p6bb
135; CHECK-NOT: icmp
136; CHECK: ret void
137
138; <label>:8                                       ; preds = %0
139  call void @_ZdaPv(i8* %4) #5
140  call void @_ZdaPv(i8* %5) #5
141  br label %9
142
143; <label>:9                                       ; preds = %0, %8
144  ret void
145}
146
147; Here's another case involving multiple underlying objects, but this time we
148; must keep the comparison (it might involve a regular pointer-typed function
149; argument).
150
151define void @_Z4nopebbPi(i1 zeroext %b1, i1 zeroext %b2, i32* readnone %q) #0 {
152  %mStackData = alloca [10 x i32], align 16
153  %1 = bitcast [10 x i32]* %mStackData to i8*
154  %2 = getelementptr inbounds [10 x i32], [10 x i32]* %mStackData, i64 0, i64 0
155  %3 = select i1 %b1, i32* %2, i32* %q
156  %4 = tail call noalias i8* @_Znam(i64 48) #4
157  %5 = tail call noalias i8* @_Znam(i64 48) #4
158  %.v = select i1 %b2, i8* %4, i8* %5
159  %6 = bitcast i8* %.v to i32*
160  %7 = icmp eq i32* %6, %3
161  br i1 %7, label %9, label %8
162
163; CHECK-LABEL: @_Z4nopebbPi
164; CHECK: icmp
165; CHECK: ret void
166
167; <label>:8                                       ; preds = %0
168  call void @_ZdaPv(i8* %4) #5
169  call void @_ZdaPv(i8* %5) #5
170  br label %9
171
172; <label>:9                                       ; preds = %0, %8
173  ret void
174}
175
176define void @_Z2p3bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
177  %mStackData = alloca [10 x i32], align 16
178  %1 = bitcast [10 x i32]* %mStackData to i8*
179  %2 = getelementptr inbounds [10 x i32], [10 x i32]* %mStackData, i64 0, i64 0
180  %3 = select i1 %b1, i32* %2, i32* @g1
181  %4 = tail call noalias i8* @_Znam(i64 48) #4
182  %5 = tail call noalias i8* @_Znam(i64 48) #4
183  %.v = select i1 %b2, i8* %4, i8* %5
184  %6 = bitcast i8* %.v to i32*
185  %7 = icmp eq i32* %6, %3
186  br i1 %7, label %9, label %8
187
188; CHECK-LABEL: @_Z2p3bb
189; CHECK: icmp
190; CHECK: ret void
191
192; <label>:8                                       ; preds = %0
193  call void @_ZdaPv(i8* %4) #5
194  call void @_ZdaPv(i8* %5) #5
195  br label %9
196
197; <label>:9                                       ; preds = %0, %8
198  ret void
199}
200
201define void @_Z2p7bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
202  %mStackData = alloca [10 x i32], align 16
203  %1 = bitcast [10 x i32]* %mStackData to i8*
204  %2 = getelementptr inbounds [10 x i32], [10 x i32]* %mStackData, i64 0, i64 0
205  %3 = select i1 %b1, i32* %2, i32* @g6
206  %4 = tail call noalias i8* @_Znam(i64 48) #4
207  %5 = tail call noalias i8* @_Znam(i64 48) #4
208  %.v = select i1 %b2, i8* %4, i8* %5
209  %6 = bitcast i8* %.v to i32*
210  %7 = icmp eq i32* %6, %3
211  br i1 %7, label %9, label %8
212
213; CHECK-LABEL: @_Z2p7bb
214; CHECK: icmp
215; CHECK: ret void
216
217; <label>:8                                       ; preds = %0
218  call void @_ZdaPv(i8* %4) #5
219  call void @_ZdaPv(i8* %5) #5
220  br label %9
221
222; <label>:9                                       ; preds = %0, %8
223  ret void
224}
225
226define void @_Z2p2v(i32 %c) #0 {
227  %mStackData = alloca [10 x i32], i32 %c, align 16
228  %1 = bitcast [10 x i32]* %mStackData to i8*
229  %2 = tail call noalias i8* @_Znam(i64 48) #4
230  %3 = bitcast i8* %2 to i32*
231  %4 = getelementptr inbounds [10 x i32], [10 x i32]* %mStackData, i64 0, i64 0
232  %5 = icmp eq i32* %3, %4
233  br i1 %5, label %7, label %6
234
235; CHECK-LABEL: @_Z2p2v
236; CHECK: icmp
237; CHECK: ret void
238
239; <label>:6                                       ; preds = %0
240  call void @_ZdaPv(i8* %2) #5
241  br label %7
242
243; <label>:7                                       ; preds = %0, %6
244  ret void
245}
246
247; Function Attrs: nobuiltin
248declare noalias i8* @_Znam(i64) #2
249
250; Function Attrs: nobuiltin nounwind
251declare void @_ZdaPv(i8*) #3
252
253attributes #0 = { uwtable }
254attributes #1 = { nounwind }
255attributes #2 = { nobuiltin }
256attributes #3 = { nobuiltin nounwind }
257attributes #4 = { builtin }
258attributes #5 = { builtin nounwind }
259
260