Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 97d7bcd

Browse files
committed
[Local] Make DoesKMove required for combineMetadata.
This patch makes the DoesKMove argument non-optional, to force people to think about it. Most cases where it is false are either code hoisting or code sinking, where we pick one instruction from a set of equal instructions among different code paths. Reviewers: dberlin, nlopes, efriedma, davide Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D47475 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@340606 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 3327f8b commit 97d7bcd

File tree

14 files changed

+288
-14
lines changed

14 files changed

+288
-14
lines changed

include/llvm/Transforms/Utils/Local.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,16 @@ bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr,
391391
///
392392
/// Metadata not listed as known via KnownIDs is removed
393393
void combineMetadata(Instruction *K, const Instruction *J,
394-
ArrayRef<unsigned> KnownIDs, bool DoesKMove = true);
394+
ArrayRef<unsigned> KnownIDs, bool DoesKMove);
395395

396396
/// Combine the metadata of two instructions so that K can replace J. This
397-
/// specifically handles the case of CSE-like transformations.
397+
/// specifically handles the case of CSE-like transformations. Some
398+
/// metadata can only be kept if K dominates J. For this to be correct,
399+
/// K cannot be hoisted.
398400
///
399401
/// Unknown metadata is removed.
400-
void combineMetadataForCSE(Instruction *K, const Instruction *J);
402+
void combineMetadataForCSE(Instruction *K, const Instruction *J,
403+
bool DoesKMove);
401404

402405
/// Patch the replacement so that it is not more restrictive than the value
403406
/// being replaced. It assumes that the replacement does not get moved from

lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
10261026
if (Value *AvailableVal = FindAvailableLoadedValue(
10271027
&LI, LI.getParent(), BBI, DefMaxInstsToScan, AA, &IsLoadCSE)) {
10281028
if (IsLoadCSE)
1029-
combineMetadataForCSE(cast<LoadInst>(AvailableVal), &LI);
1029+
combineMetadataForCSE(cast<LoadInst>(AvailableVal), &LI, false);
10301030

10311031
return replaceInstUsesWith(
10321032
LI, Builder.CreateBitOrPointerCast(AvailableVal, LI.getType(),

lib/Transforms/InstCombine/InstCombinePHI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ Instruction *InstCombiner::FoldPHIArgLoadIntoPHI(PHINode &PN) {
616616
// Add all operands to the new PHI and combine TBAA metadata.
617617
for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
618618
LoadInst *LI = cast<LoadInst>(PN.getIncomingValue(i));
619-
combineMetadata(NewLI, LI, KnownIDs);
619+
combineMetadata(NewLI, LI, KnownIDs, true);
620620
Value *NewInVal = LI->getOperand(0);
621621
if (NewInVal != InVal)
622622
InVal = nullptr;

lib/Transforms/Scalar/GVNHoist.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static void combineKnownMetadata(Instruction *ReplInst, Instruction *I) {
247247
LLVMContext::MD_noalias, LLVMContext::MD_range,
248248
LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
249249
LLVMContext::MD_invariant_group};
250-
combineMetadata(ReplInst, I, KnownIDs);
250+
combineMetadata(ReplInst, I, KnownIDs, true);
251251
}
252252

253253
// This pass hoists common computations across branches sharing common

lib/Transforms/Scalar/GVNSink.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ void GVNSink::sinkLastInstruction(ArrayRef<BasicBlock *> Blocks,
859859
// Update metadata and IR flags.
860860
for (auto *I : Insts)
861861
if (I != I0) {
862-
combineMetadataForCSE(I0, I);
862+
combineMetadataForCSE(I0, I, true);
863863
I0->andIRFlags(I);
864864
}
865865

lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ bool JumpThreadingPass::SimplifyPartiallyRedundantLoad(LoadInst *LoadI) {
13001300

13011301
if (IsLoadCSE) {
13021302
LoadInst *NLoadI = cast<LoadInst>(AvailableVal);
1303-
combineMetadataForCSE(NLoadI, LoadI);
1303+
combineMetadataForCSE(NLoadI, LoadI, false);
13041304
};
13051305

13061306
// If the returned value is the load itself, replace with an undef. This can
@@ -1490,7 +1490,7 @@ bool JumpThreadingPass::SimplifyPartiallyRedundantLoad(LoadInst *LoadI) {
14901490
}
14911491

14921492
for (LoadInst *PredLoadI : CSELoads) {
1493-
combineMetadataForCSE(PredLoadI, LoadI);
1493+
combineMetadataForCSE(PredLoadI, LoadI, true);
14941494
}
14951495

14961496
LoadI->replaceAllUsesWith(PN);

lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpy, Value *cpyDest,
994994
unsigned KnownIDs[] = {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
995995
LLVMContext::MD_noalias,
996996
LLVMContext::MD_invariant_group};
997-
combineMetadata(C, cpy, KnownIDs);
997+
combineMetadata(C, cpy, KnownIDs, true);
998998

999999
// Remove the memcpy.
10001000
MD->removeInstruction(cpy);

lib/Transforms/Utils/Local.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,15 +2354,16 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J,
23542354
K->setMetadata(LLVMContext::MD_invariant_group, JMD);
23552355
}
23562356

2357-
void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J) {
2357+
void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J,
2358+
bool KDominatesJ) {
23582359
unsigned KnownIDs[] = {
23592360
LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
23602361
LLVMContext::MD_noalias, LLVMContext::MD_range,
23612362
LLVMContext::MD_invariant_load, LLVMContext::MD_nonnull,
23622363
LLVMContext::MD_invariant_group, LLVMContext::MD_align,
23632364
LLVMContext::MD_dereferenceable,
23642365
LLVMContext::MD_dereferenceable_or_null};
2365-
combineMetadata(K, J, KnownIDs);
2366+
combineMetadata(K, J, KnownIDs, KDominatesJ);
23662367
}
23672368

23682369
void llvm::patchReplacementInstruction(Instruction *I, Value *Repl) {

lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ static bool HoistThenElseCodeToIf(BranchInst *BI,
13161316
LLVMContext::MD_dereferenceable,
13171317
LLVMContext::MD_dereferenceable_or_null,
13181318
LLVMContext::MD_mem_parallel_loop_access};
1319-
combineMetadata(I1, I2, KnownIDs);
1319+
combineMetadata(I1, I2, KnownIDs, true);
13201320

13211321
// I1 and I2 are being combined into a single instruction. Its debug
13221322
// location is the merged locations of the original instructions.
@@ -1582,7 +1582,7 @@ static bool sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
15821582
// However, as N-way merge for CallInst is rare, so we use simplified API
15831583
// instead of using complex API for N-way merge.
15841584
I0->applyMergedLocation(I0->getDebugLoc(), I->getDebugLoc());
1585-
combineMetadataForCSE(I0, I);
1585+
combineMetadataForCSE(I0, I, true);
15861586
I0->andIRFlags(I);
15871587
}
15881588

test/Transforms/GVNHoist/hoist-md.ll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,29 @@ return: ; preds = %if.end, %if.then
9393
; CHECK: %[[phi:.*]] = phi i32 [ %[[load]], %{{.*}} ], [ %[[load]], %{{.*}} ]
9494
; CHECK: ret i32 %[[phi]]
9595

96+
define i32* @test5(i1 %b, i32** %y) {
97+
entry:
98+
br i1 %b, label %if.then, label %if.end
99+
100+
if.then: ; preds = %entry
101+
%0 = load i32*, i32** %y, align 4, !nonnull !9
102+
br label %return
103+
104+
if.end: ; preds = %entry
105+
%1 = load i32*, i32** %y, align 4
106+
br label %return
107+
108+
return: ; preds = %if.end, %if.then
109+
%retval.0 = phi i32* [ %0, %if.then ], [ %1, %if.end ]
110+
ret i32* %retval.0
111+
}
112+
; CHECK-LABEL: define i32* @test5(
113+
; CHECK: %[[load:.*]] = load i32*, i32** %y, align 4
114+
; CHECK-NOT: !nonnull
115+
; CHECK: %[[phi:.*]] = phi i32* [ %[[load]], %{{.*}} ], [ %[[load]], %{{.*}} ]
116+
; CHECK: ret i32* %[[phi]]
117+
96118
!7 = !{i32 0, i32 2}
97119
!8 = !{i32 3, i32 4}
120+
!9 = !{}
98121
; CHECK: ![[range_md]] = !{i32 0, i32 2, i32 3, i32 4}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
; RUN: opt < %s -gvn-sink -S | FileCheck %s
2+
3+
; Check that nonnull metadata for non-dominating loads is not propagated.
4+
; CHECK-LABEL: @test1(
5+
; CHECK-LABEL: if.end:
6+
; CHECK: %[[ptr:.*]] = phi i32**
7+
; CHECK: %[[load:.*]] = load i32*, i32** %[[ptr]]
8+
; CHECK-NOT: !nonnull
9+
; CHECK: ret i32* %[[load]]
10+
define i32* @test1(i1 zeroext %flag, i32*** %p) {
11+
entry:
12+
br i1 %flag, label %if.then, label %if.else
13+
14+
if.then:
15+
%a = load i32**, i32*** %p
16+
%aa = load i32*, i32** %a, !nonnull !0
17+
br label %if.end
18+
19+
if.else:
20+
%b = load i32**, i32*** %p
21+
%bb= load i32*, i32** %b
22+
br label %if.end
23+
24+
if.end:
25+
%c = phi i32* [ %aa, %if.then ], [ %bb, %if.else ]
26+
ret i32* %c
27+
}
28+
29+
; CHECK-LABEL: @test2(
30+
; CHECK-LABEL: if.end:
31+
; CHECK: %[[ptr:.*]] = phi i32**
32+
; CHECK: %[[load:.*]] = load i32*, i32** %[[ptr]]
33+
; CHECK-NOT: !nonnull
34+
; CHECK: ret i32* %[[load]]
35+
define i32* @test2(i1 zeroext %flag, i32*** %p) {
36+
entry:
37+
br i1 %flag, label %if.then, label %if.else
38+
39+
if.then:
40+
%a = load i32**, i32*** %p
41+
%aa = load i32*, i32** %a
42+
br label %if.end
43+
44+
if.else:
45+
%b = load i32**, i32*** %p
46+
%bb= load i32*, i32** %b, !nonnull !0
47+
br label %if.end
48+
49+
if.end:
50+
%c = phi i32* [ %aa, %if.then ], [ %bb, %if.else ]
51+
ret i32* %c
52+
}
53+
54+
55+
!0 = !{}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; RUN: opt -instcombine -S < %s | FileCheck %s
2+
3+
target datalayout = "e-m:e-p:64:64:64-i64:64-f80:128-n8:16:32:64-S128"
4+
5+
; Check that nonnull metadata is propagated from dominating load.
6+
; CHECK-LABEL: @combine_metadata_dominance1(
7+
; CHECK-LABEL: bb1:
8+
; CHECK: load i32*, i32** %p, align 8, !nonnull !0
9+
; CHECK-NOT: load i32*, i32** %p
10+
define void @combine_metadata_dominance1(i32** %p) {
11+
entry:
12+
%a = load i32*, i32** %p, !nonnull !0
13+
br label %bb1
14+
15+
bb1:
16+
%b = load i32*, i32** %p
17+
store i32 0, i32* %a
18+
store i32 0, i32* %b
19+
ret void
20+
}
21+
22+
declare i32 @use(i32*, i32) readonly
23+
24+
; Check that nonnull from the dominated load does not get propagated.
25+
; There are some cases where it would be safe to keep it.
26+
; CHECK-LABEL: @combine_metadata_dominance2(
27+
; CHECK-NOT: nonnull
28+
define void @combine_metadata_dominance2(i32** %p) {
29+
entry:
30+
%a = load i32*, i32** %p
31+
br i1 undef, label %bb1, label %bb2
32+
33+
bb1:
34+
%b = load i32*, i32** %p, !nonnull !0
35+
store i32 0, i32* %a
36+
store i32 0, i32* %b
37+
ret void
38+
39+
bb2:
40+
ret void
41+
}
42+
43+
44+
!0 = !{}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; RUN: opt -instcombine -S < %s | FileCheck %s
2+
3+
declare void @bar()
4+
declare void @baz()
5+
6+
; Check that nonnull metadata is from non-dominating loads is not propagated.
7+
; CHECK-LABEL: cont:
8+
; CHECK-NOT: !nonnull
9+
define i32* @test_combine_metadata_dominance(i1 %c, i32** dereferenceable(8) %p1, i32** dereferenceable(8) %p2) {
10+
br i1 %c, label %t, label %f
11+
t:
12+
call void @bar()
13+
%v1 = load i32*, i32** %p1, align 8, !nonnull !0
14+
br label %cont
15+
16+
f:
17+
call void @baz()
18+
%v2 = load i32*, i32** %p2, align 8
19+
br label %cont
20+
21+
cont:
22+
%res = phi i32* [ %v1, %t ], [ %v2, %f ]
23+
ret i32* %res
24+
}
25+
26+
!0 = !{}

0 commit comments

Comments
 (0)