llvm.org GIT mirror llvm / 7c0b2ec
[MemCpyOpt] Don't sink LoadInst below possible clobber. Differential Revision: https://reviews.llvm.org/D26811 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@290611 91177308-0d34-0410-b5e6-96231b3b80d8 Bryant Wong 2 years ago
2 changed file(s) with 47 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
488488 // It will lift the store and its argument + that anything that
489489 // may alias with these.
490490 // The method returns true if it was successful.
491 static bool moveUp(AliasAnalysis &AA, StoreInst *SI, Instruction *P) {
491 static bool moveUp(AliasAnalysis &AA, StoreInst *SI, Instruction *P,
492 const LoadInst *LI) {
492493 // If the store alias this position, early bail out.
493494 MemoryLocation StoreLoc = MemoryLocation::get(SI);
494495 if (AA.getModRefInfo(P, StoreLoc) != MRI_NoModRef)
505506 SmallVector ToLift;
506507
507508 // Memory locations of lifted instructions.
508 SmallVector MemLocs;
509 MemLocs.push_back(StoreLoc);
509 SmallVector MemLocs{StoreLoc};
510510
511511 // Lifted callsites.
512512 SmallVector CallSites;
513
514 const MemoryLocation LoadLoc = MemoryLocation::get(LI);
513515
514516 for (auto I = --SI->getIterator(), E = P->getIterator(); I != E; --I) {
515517 auto *C = &*I;
534536 continue;
535537
536538 if (MayAlias) {
537 if (auto CS = ImmutableCallSite(C)) {
539 // Since LI is implicitly moved downwards past the lifted instructions,
540 // none of them may modify its source.
541 if (AA.getModRefInfo(C, LoadLoc) & MRI_Mod)
542 return false;
543 else if (auto CS = ImmutableCallSite(C)) {
538544 // If we can't lift this before P, it's game over.
539545 if (AA.getModRefInfo(P, CS) != MRI_NoModRef)
540546 return false;
609615 // position if nothing alias the store memory after this and the store
610616 // destination is not in the range.
611617 if (P && P != SI) {
612 if (!moveUp(AA, SI, P))
618 if (!moveUp(AA, SI, P, LI))
613619 P = nullptr;
614620 }
615621
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; RUN: opt -basicaa -scoped-noalias -memcpyopt -S %s | FileCheck %s
2
3 %T = type { i8, i32 }
4
5 ; memcpy(%d, %a) should not be generated since store2 may-aliases load %a.
6 define void @f(%T* %a, %T* %b, %T* %c, %T* %d) {
7 ; CHECK-LABEL: @f(
8 ; CHECK-NEXT: [[VAL:%.*]] = load %T, %T* %a, !alias.scope !0
9 ; CHECK-NEXT: store %T { i8 23, i32 23 }, %T* %b, !alias.scope !3
10 ; CHECK-NEXT: store %T { i8 44, i32 44 }, %T* %c, !alias.scope !6, !noalias !3
11 ; CHECK-NEXT: store %T [[VAL]], %T* %d, !alias.scope !9, !noalias !12
12 ; CHECK-NEXT: ret void
13 ;
14 %val = load %T, %T* %a, !alias.scope !{!10}
15
16 ; store1 may-aliases the load
17 store %T { i8 23, i32 23 }, %T* %b, !alias.scope !{!11}
18
19 ; store2 may-aliases the load and store3
20 store %T { i8 44, i32 44 }, %T* %c, !alias.scope !{!12}, !noalias !{!11}
21
22 ; store3
23 store %T %val, %T* %d, !alias.scope !{!13}, !noalias !{!10, !11}
24 ret void
25 }
26
27 !0 = !{!0}
28 !1 = !{!1}
29 !2 = !{!2}
30 !3 = !{!3}
31
32 !10 = !{ !10, !0 }
33 !11 = !{ !11, !1 }
34 !12 = !{ !12, !2 }
35 !13 = !{ !13, !3 }