llvm.org GIT mirror llvm / 3dbefbd
Teach DSE that strcpy, strncpy, strcat and strncat are all stores which may be dead. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@164561 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 7 years ago
2 changed file(s) with 121 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
2929 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
3030 #include "llvm/Analysis/ValueTracking.h"
3131 #include "llvm/Target/TargetData.h"
32 #include "llvm/Target/TargetLibraryInfo.h"
3233 #include "llvm/Transforms/Utils/Local.h"
3334 #include "llvm/Support/Debug.h"
3435 #include "llvm/ADT/SetVector.h"
145146
146147 /// hasMemoryWrite - Does this instruction write some memory? This only returns
147148 /// true for things that we can analyze with other helpers below.
148 static bool hasMemoryWrite(Instruction *I) {
149 static bool hasMemoryWrite(Instruction *I, const TargetLibraryInfo *TLI) {
149150 if (isa(I))
150151 return true;
151152 if (IntrinsicInst *II = dyn_cast(I)) {
160161 return true;
161162 }
162163 }
164 if (CallSite CS = I) {
165 if (Function *F = CS.getCalledFunction()) {
166 if (TLI && TLI->has(LibFunc::strcpy) &&
167 F->getName() == TLI->getName(LibFunc::strcpy)) {
168 return true;
169 }
170 if (TLI && TLI->has(LibFunc::strncpy) &&
171 F->getName() == TLI->getName(LibFunc::strncpy)) {
172 return true;
173 }
174 if (TLI && TLI->has(LibFunc::strcat) &&
175 F->getName() == TLI->getName(LibFunc::strcat)) {
176 return true;
177 }
178 if (TLI && TLI->has(LibFunc::strncat) &&
179 F->getName() == TLI->getName(LibFunc::strncat)) {
180 return true;
181 }
182 }
183 }
163184 return false;
164185 }
165186
207228 /// instruction if any.
208229 static AliasAnalysis::Location
209230 getLocForRead(Instruction *Inst, AliasAnalysis &AA) {
210 assert(hasMemoryWrite(Inst) && "Unknown instruction case");
231 assert(hasMemoryWrite(Inst, AA.getTargetLibraryInfo()) &&
232 "Unknown instruction case");
211233
212234 // The only instructions that both read and write are the mem transfer
213235 // instructions (memcpy/memmove).
224246 if (StoreInst *SI = dyn_cast(I))
225247 return SI->isUnordered();
226248
227 IntrinsicInst *II = cast(I);
228 switch (II->getIntrinsicID()) {
229 default: llvm_unreachable("doesn't pass 'hasMemoryWrite' predicate");
230 case Intrinsic::lifetime_end:
231 // Never remove dead lifetime_end's, e.g. because it is followed by a
232 // free.
233 return false;
234 case Intrinsic::init_trampoline:
235 // Always safe to remove init_trampoline.
236 return true;
237
238 case Intrinsic::memset:
239 case Intrinsic::memmove:
240 case Intrinsic::memcpy:
241 // Don't remove volatile memory intrinsics.
242 return !cast(II)->isVolatile();
243 }
249 if (IntrinsicInst *II = dyn_cast(I)) {
250 switch (II->getIntrinsicID()) {
251 default: llvm_unreachable("doesn't pass 'hasMemoryWrite' predicate");
252 case Intrinsic::lifetime_end:
253 // Never remove dead lifetime_end's, e.g. because it is followed by a
254 // free.
255 return false;
256 case Intrinsic::init_trampoline:
257 // Always safe to remove init_trampoline.
258 return true;
259
260 case Intrinsic::memset:
261 case Intrinsic::memmove:
262 case Intrinsic::memcpy:
263 // Don't remove volatile memory intrinsics.
264 return !cast(II)->isVolatile();
265 }
266 }
267
268 if (CallSite CS = I) // If we assume hasMemoryWrite(I) is true,
269 return true; // then there's nothing left to check.
270
271 return false;
244272 }
245273
246274
251279 if (isa(I))
252280 return false;
253281
254 IntrinsicInst *II = cast(I);
255 switch (II->getIntrinsicID()) {
256 default: return false;
257 case Intrinsic::memset:
258 case Intrinsic::memcpy:
259 // Do shorten memory intrinsics.
260 return true;
261 }
282 if (IntrinsicInst *II = dyn_cast(I)) {
283 switch (II->getIntrinsicID()) {
284 default: return false;
285 case Intrinsic::memset:
286 case Intrinsic::memcpy:
287 // Do shorten memory intrinsics.
288 return true;
289 }
290 }
291
292 // Don't shorten libcalls calls for now.
293
294 return false;
262295 }
263296
264297 /// getStoredPointerOperand - Return the pointer that is being written to.
268301 if (MemIntrinsic *MI = dyn_cast(I))
269302 return MI->getDest();
270303
271 IntrinsicInst *II = cast(I);
272 switch (II->getIntrinsicID()) {
273 default: llvm_unreachable("Unexpected intrinsic!");
274 case Intrinsic::init_trampoline:
275 return II->getArgOperand(0);
276 }
304 if (IntrinsicInst *II = dyn_cast(I)) {
305 switch (II->getIntrinsicID()) {
306 default: llvm_unreachable("Unexpected intrinsic!");
307 case Intrinsic::init_trampoline:
308 return II->getArgOperand(0);
309 }
310 }
311
312 CallSite CS(I);
313 // All the supported functions so far happen to have dest as their first
314 // argument.
315 return CS.getArgument(0);
277316 }
278317
279318 static uint64_t getPointerSize(const Value *V, AliasAnalysis &AA) {
462501 }
463502
464503 // If we find something that writes memory, get its memory dependence.
465 if (!hasMemoryWrite(Inst))
504 if (!hasMemoryWrite(Inst, TLI))
466505 continue;
467506
468507 MemDepResult InstDep = MD->getDependency(Inst);
629668 MemDepResult Dep = MD->getPointerDependencyFrom(Loc, false, InstPt, BB);
630669 while (Dep.isDef() || Dep.isClobber()) {
631670 Instruction *Dependency = Dep.getInst();
632 if (!hasMemoryWrite(Dependency) || !isRemovable(Dependency))
671 if (!hasMemoryWrite(Dependency, TLI) || !isRemovable(Dependency))
633672 break;
634673
635674 Value *DepPointer =
698737 --BBI;
699738
700739 // If we find a store, check to see if it points into a dead stack value.
701 if (hasMemoryWrite(BBI) && isRemovable(BBI)) {
740 if (hasMemoryWrite(BBI, TLI) && isRemovable(BBI)) {
702741 // See through pointer-to-pointer bitcasts
703742 SmallVector Pointers;
704743 GetUnderlyingObjects(getStoredPointerOperand(BBI), Pointers);
0 ; RUN: opt -S -basicaa -dse < %s | FileCheck %s
1
2 declare i8* @strcpy(i8* %dest, i8* %src) nounwind
3 define void @test1(i8* %src) {
4 ; CHECK: @test1
5 %B = alloca [16 x i8]
6 %dest = getelementptr inbounds [16 x i8]* %B, i64 0, i64 0
7 ; CHECK-NOT: @strcpy
8 %call = call i8* @strcpy(i8* %dest, i8* %src)
9 ; CHECK: ret void
10 ret void
11 }
12
13 declare i8* @strncpy(i8* %dest, i8* %src, i32 %n) nounwind
14 define void @test2(i8* %src) {
15 ; CHECK: @test2
16 %B = alloca [16 x i8]
17 %dest = getelementptr inbounds [16 x i8]* %B, i64 0, i64 0
18 ; CHECK-NOT: @strcpy
19 %call = call i8* @strncpy(i8* %dest, i8* %src, i32 12)
20 ; CHECK: ret void
21 ret void
22 }
23
24 declare i8* @strcat(i8* %dest, i8* %src) nounwind
25 define void @test3(i8* %src) {
26 ; CHECK: @test3
27 %B = alloca [16 x i8]
28 %dest = getelementptr inbounds [16 x i8]* %B, i64 0, i64 0
29 ; CHECK-NOT: @strcpy
30 %call = call i8* @strcat(i8* %dest, i8* %src)
31 ; CHECK: ret void
32 ret void
33 }
34
35 declare i8* @strncat(i8* %dest, i8* %src, i32 %n) nounwind
36 define void @test4(i8* %src) {
37 ; CHECK: @test4
38 %B = alloca [16 x i8]
39 %dest = getelementptr inbounds [16 x i8]* %B, i64 0, i64 0
40 ; CHECK-NOT: @strcpy
41 %call = call i8* @strncat(i8* %dest, i8* %src, i32 12)
42 ; CHECK: ret void
43 ret void
44 }
45