llvm.org GIT mirror llvm / d70d514
Restrict somewhat the memory-allocation pointer cmp opt from r223093 Based on review comments from Richard Smith, restrict this optimization from applying to globals that might resolve lazily to other dynamically-loaded modules, and also from dynamic allocas (which might be transformed into malloc calls). In short, take extra care that the compared-to pointer is really simultaneously live with the memory allocation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223347 91177308-0d34-0410-b5e6-96231b3b80d8 Hal Finkel 4 years ago
2 changed file(s) with 165 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
20252025 };
20262026
20272027 // Is the set of underlying objects all things which must be disjoint from
2028 // noalias calls.
2028 // noalias calls. For allocas, we consider only static ones (dynamic
2029 // allocas might be transformed into calls to malloc not simultaneously
2030 // live with the compared-to allocation). For globals, we exclude symbols
2031 // that might be resolve lazily to symbols in another dynamically-loaded
2032 // library (and, thus, could be malloc'ed by the implementation).
20292033 auto IsAllocDisjoint = [](SmallVectorImpl &Objects) {
20302034 return std::all_of(Objects.begin(), Objects.end(),
20312035 [](Value *V){
2032 if (isa(V) || isa(V))
2033 return true;
2036 if (const AllocaInst *AI = dyn_cast(V))
2037 return AI->isStaticAlloca();
2038 if (const GlobalValue *GV = dyn_cast(V))
2039 return (GV->hasLocalLinkage() ||
2040 GV->hasHiddenVisibility() ||
2041 GV->hasProtectedVisibility() ||
2042 GV->hasUnnamedAddr()) &&
2043 !GV->isThreadLocal();
20342044 if (const Argument *A = dyn_cast(V))
20352045 return A->hasByValAttr();
20362046 return false;
22 target triple = "x86_64-unknown-linux-gnu"
33
44 @g1 = global i32 0, align 4
5 @g2 = internal global i32 0, align 4
6 @g3 = unnamed_addr global i32 0, align 4
7 @g4 = hidden global i32 0, align 4
8 @g5 = protected global i32 0, align 4
9 @g6 = thread_local unnamed_addr global i32 0, align 4
510
611 ; Make sure we can simplify away a pointer comparison between
712 ; dynamically-allocated memory and a local stack allocation.
4247 %mStackData = alloca [10 x i32], align 16
4348 %1 = bitcast [10 x i32]* %mStackData to i8*
4449 %2 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0
45 %3 = select i1 %b1, i32* %2, i32* @g1
50 %3 = select i1 %b1, i32* %2, i32* @g2
4651 %4 = tail call noalias i8* @_Znam(i64 48) #4
4752 %5 = tail call noalias i8* @_Znam(i64 48) #4
4853 %.v = select i1 %b2, i8* %4, i8* %5
5156 br i1 %7, label %9, label %8
5257
5358 ; CHECK-LABEL: @_Z2p2bb
59 ; CHECK-NOT: icmp
60 ; CHECK: ret void
61
62 ;
63 call void @_ZdaPv(i8* %4) #5
64 call void @_ZdaPv(i8* %5) #5
65 br label %9
66
67 ;
68 ret void
69 }
70
71 define void @_Z2p4bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
72 %mStackData = alloca [10 x i32], align 16
73 %1 = bitcast [10 x i32]* %mStackData to i8*
74 %2 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0
75 %3 = select i1 %b1, i32* %2, i32* @g3
76 %4 = tail call noalias i8* @_Znam(i64 48) #4
77 %5 = tail call noalias i8* @_Znam(i64 48) #4
78 %.v = select i1 %b2, i8* %4, i8* %5
79 %6 = bitcast i8* %.v to i32*
80 %7 = icmp eq i32* %6, %3
81 br i1 %7, label %9, label %8
82
83 ; CHECK-LABEL: @_Z2p4bb
84 ; CHECK-NOT: icmp
85 ; CHECK: ret void
86
87 ;
88 call void @_ZdaPv(i8* %4) #5
89 call void @_ZdaPv(i8* %5) #5
90 br label %9
91
92 ;
93 ret void
94 }
95
96 define void @_Z2p5bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
97 %mStackData = alloca [10 x i32], align 16
98 %1 = bitcast [10 x i32]* %mStackData to i8*
99 %2 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0
100 %3 = select i1 %b1, i32* %2, i32* @g4
101 %4 = tail call noalias i8* @_Znam(i64 48) #4
102 %5 = tail call noalias i8* @_Znam(i64 48) #4
103 %.v = select i1 %b2, i8* %4, i8* %5
104 %6 = bitcast i8* %.v to i32*
105 %7 = icmp eq i32* %6, %3
106 br i1 %7, label %9, label %8
107
108 ; CHECK-LABEL: @_Z2p5bb
109 ; CHECK-NOT: icmp
110 ; CHECK: ret void
111
112 ;
113 call void @_ZdaPv(i8* %4) #5
114 call void @_ZdaPv(i8* %5) #5
115 br label %9
116
117 ;
118 ret void
119 }
120
121 define void @_Z2p6bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
122 %mStackData = alloca [10 x i32], align 16
123 %1 = bitcast [10 x i32]* %mStackData to i8*
124 %2 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0
125 %3 = select i1 %b1, i32* %2, i32* @g5
126 %4 = tail call noalias i8* @_Znam(i64 48) #4
127 %5 = tail call noalias i8* @_Znam(i64 48) #4
128 %.v = select i1 %b2, i8* %4, i8* %5
129 %6 = bitcast i8* %.v to i32*
130 %7 = icmp eq i32* %6, %3
131 br i1 %7, label %9, label %8
132
133 ; CHECK-LABEL: @_Z2p6bb
54134 ; CHECK-NOT: icmp
55135 ; CHECK: ret void
56136
89169 br label %9
90170
91171 ;
172 ret void
173 }
174
175 define void @_Z2p3bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
176 %mStackData = alloca [10 x i32], align 16
177 %1 = bitcast [10 x i32]* %mStackData to i8*
178 %2 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0
179 %3 = select i1 %b1, i32* %2, i32* @g1
180 %4 = tail call noalias i8* @_Znam(i64 48) #4
181 %5 = tail call noalias i8* @_Znam(i64 48) #4
182 %.v = select i1 %b2, i8* %4, i8* %5
183 %6 = bitcast i8* %.v to i32*
184 %7 = icmp eq i32* %6, %3
185 br i1 %7, label %9, label %8
186
187 ; CHECK-LABEL: @_Z2p3bb
188 ; CHECK: icmp
189 ; CHECK: ret void
190
191 ;
192 call void @_ZdaPv(i8* %4) #5
193 call void @_ZdaPv(i8* %5) #5
194 br label %9
195
196 ;
197 ret void
198 }
199
200 define void @_Z2p7bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
201 %mStackData = alloca [10 x i32], align 16
202 %1 = bitcast [10 x i32]* %mStackData to i8*
203 %2 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0
204 %3 = select i1 %b1, i32* %2, i32* @g6
205 %4 = tail call noalias i8* @_Znam(i64 48) #4
206 %5 = tail call noalias i8* @_Znam(i64 48) #4
207 %.v = select i1 %b2, i8* %4, i8* %5
208 %6 = bitcast i8* %.v to i32*
209 %7 = icmp eq i32* %6, %3
210 br i1 %7, label %9, label %8
211
212 ; CHECK-LABEL: @_Z2p7bb
213 ; CHECK: icmp
214 ; CHECK: ret void
215
216 ;
217 call void @_ZdaPv(i8* %4) #5
218 call void @_ZdaPv(i8* %5) #5
219 br label %9
220
221 ;
222 ret void
223 }
224
225 define void @_Z2p2v(i32 %c) #0 {
226 %mStackData = alloca [10 x i32], i32 %c, align 16
227 %1 = bitcast [10 x i32]* %mStackData to i8*
228 %2 = tail call noalias i8* @_Znam(i64 48) #4
229 %3 = bitcast i8* %2 to i32*
230 %4 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0
231 %5 = icmp eq i32* %3, %4
232 br i1 %5, label %7, label %6
233
234 ; CHECK-LABEL: @_Z2p2v
235 ; CHECK: icmp
236 ; CHECK: ret void
237
238 ;
239 call void @_ZdaPv(i8* %2) #5
240 br label %7
241
242 ;
92243 ret void
93244 }
94245