Skip to content

Commit 62beb89

Browse files
committed
fixup Enable hoisting if all ops are exactly the same.
1 parent 863f593 commit 62beb89

File tree

7 files changed

+46
-83
lines changed

7 files changed

+46
-83
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class SimplifyCFGOpt {
284284
bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI,
285285
IRBuilder<> &Builder);
286286

287-
bool hoistCommonCodeFromSuccessors(Instruction *TI, bool EqTermsOnly);
287+
bool hoistCommonCodeFromSuccessors(Instruction *TI, bool EqInstsOnly);
288288
bool hoistSuccIdenticalTerminatorToSwitchOrIf(
289289
Instruction *TI, Instruction *I1,
290290
SmallVectorImpl<Instruction *> &OtherSuccTIs);
@@ -1823,12 +1823,12 @@ namespace {
18231823
} // end anonymous namespace
18241824

18251825
/// Hoist any common code in the successor blocks up into the block. This
1826-
/// function guarantees that BB dominates all successors. If EqTermsOnly is
1827-
/// given, only perform hoisting in case both blocks only contain a terminator.
1828-
/// In that case, only the original BI will be replaced and selects for PHIs are
1829-
/// added.
1826+
/// function guarantees that BB dominates all successors. If EqInstsOnly is
1827+
/// given, only perform hoisting in case both blocks contain equivalent
1828+
/// instructions. In that case, no selects need to be added for the hoisted
1829+
/// instruction. We still have to add selects for PHIs
18301830
bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(Instruction *TI,
1831-
bool EqTermsOnly) {
1831+
bool EqInstsOnly) {
18321832
// This does very trivial matching, with limited scanning, to find identical
18331833
// instructions in the two blocks. In particular, we don't want to get into
18341834
// O(N1*N2*...) situations here where Ni are the sizes of these successors. As
@@ -1857,14 +1857,20 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(Instruction *TI,
18571857
SuccIterPairs.push_back(SuccIterPair(SuccItr, 0));
18581858
}
18591859

1860-
// Check if only hoisting terminators is allowed. This does not add new
1861-
// instructions to the hoist location.
1862-
if (EqTermsOnly) {
1863-
// Skip any debug intrinsics, as they are free to hoist.
1864-
for (auto &SuccIter : make_first_range(SuccIterPairs)) {
1865-
auto *INonDbg = &*skipDebugIntrinsics(SuccIter);
1866-
if (!INonDbg->isTerminator())
1860+
// Check if only hoisting equivalent instructions is allowed. This may add new
1861+
// instructions to the hoist location, but is guaranteed to remove at least
1862+
// twice as many instructions from the source blocks.
1863+
if (EqInstsOnly) {
1864+
LockstepReverseIterator LRI(to_vector(successors(BB)));
1865+
while (LRI.isValid()) {
1866+
auto Insts = *LRI;
1867+
Instruction *I0 = Insts.front();
1868+
if (any_of(Insts.drop_front(), [I0](Instruction *I) {
1869+
return !I->isSameOperationAs(I0) ||
1870+
!equal(I->operands(), I0->operands());
1871+
}))
18671872
return false;
1873+
--LRI;
18681874
}
18691875
// Now we know that we only need to hoist debug intrinsics and the
18701876
// terminator. Let the loop below handle those 2 cases.
@@ -3845,29 +3851,6 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
38453851
dbgs() << " T: " << IfTrue->getName()
38463852
<< " F: " << IfFalse->getName() << "\n");
38473853

3848-
// Collect common TBAA metadata, for instructions that match in all if-blocks
3849-
// and have the same TBAA metadata. If that is the case, they access the same
3850-
// type on all paths and the TBAA info can be preserved after hoisting.
3851-
// TODO: preserve other common metadata.
3852-
LockstepReverseIterator LRI(IfBlocks);
3853-
DenseMap<Instruction *, MDNode *> CommonTBAA;
3854-
while (LRI.isValid()) {
3855-
auto Insts = *LRI;
3856-
Instruction *I0 = Insts.front();
3857-
MDNode *MD = I0->getMetadata(LLVMContext::MD_tbaa);
3858-
if (!MD || any_of(Insts, [I0, MD](Instruction *I) {
3859-
return !I->isSameOperationAs(I0) ||
3860-
!equal(I->operands(), I0->operands()) ||
3861-
I->getMetadata(LLVMContext::MD_tbaa) != MD;
3862-
})) {
3863-
--LRI;
3864-
continue;
3865-
}
3866-
for (Instruction *I : Insts)
3867-
CommonTBAA[I] = MD;
3868-
--LRI;
3869-
}
3870-
38713854
// If we can still promote the PHI nodes after this gauntlet of tests,
38723855
// do all of the PHI's now.
38733856

@@ -3876,10 +3859,6 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
38763859
for (BasicBlock *IfBlock : IfBlocks)
38773860
hoistAllInstructionsInto(DomBlock, DomBI, IfBlock);
38783861

3879-
for (Instruction &I : *DomBlock)
3880-
if (auto *MD = CommonTBAA.lookup(&I))
3881-
I.setMetadata(LLVMContext::MD_tbaa, MD);
3882-
38833862
IRBuilder<NoFolder> Builder(DomBI);
38843863
// Propagate fast-math-flags from phi nodes to replacement selects.
38853864
IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);

llvm/test/Transforms/SimplifyCFG/2008-05-16-PHIBlockMerge.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ define void @c() {
9494
; CHECK: Succ:
9595
; CHECK-NEXT: [[B:%.*]] = phi i32 [ 1, [[BB_NOMERGE]] ], [ 1, [[COMMON:%.*]] ], [ 2, [[PRE_EXIT:%.*]] ]
9696
; CHECK-NEXT: [[CONDE:%.*]] = call i1 @foo()
97+
; CHECK-NEXT: [[COND:%.*]] = call i1 @foo()
9798
; CHECK-NEXT: br i1 [[CONDE]], label [[COMMON]], label [[PRE_EXIT]]
9899
; CHECK: Common:
99-
; CHECK-NEXT: [[COND:%.*]] = call i1 @foo()
100100
; CHECK-NEXT: br i1 [[COND]], label [[BB_NOMERGE]], label [[SUCC]]
101101
; CHECK: Pre-Exit:
102-
; CHECK-NEXT: [[COND2:%.*]] = call i1 @foo()
103-
; CHECK-NEXT: br i1 [[COND2]], label [[SUCC]], label [[EXIT:%.*]]
102+
; CHECK-NEXT: br i1 [[COND]], label [[SUCC]], label [[EXIT:%.*]]
104103
; CHECK: Exit:
105104
; CHECK-NEXT: ret void
106105
;

llvm/test/Transforms/SimplifyCFG/EqualPHIEdgeBlockMerge.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,12 @@ define void @c() {
268268
; CHECK: Succ:
269269
; CHECK-NEXT: [[B:%.*]] = phi i32 [ 1, [[BB_NOMERGE]] ], [ 1, [[COMMON:%.*]] ], [ 2, [[PRE_EXIT:%.*]] ]
270270
; CHECK-NEXT: [[CONDE:%.*]] = call i1 @foo()
271+
; CHECK-NEXT: [[COND:%.*]] = call i1 @foo()
271272
; CHECK-NEXT: br i1 [[CONDE]], label [[COMMON]], label [[PRE_EXIT]]
272273
; CHECK: Common:
273-
; CHECK-NEXT: [[COND:%.*]] = call i1 @foo()
274274
; CHECK-NEXT: br i1 [[COND]], label [[BB_NOMERGE]], label [[SUCC]]
275275
; CHECK: Pre-Exit:
276-
; CHECK-NEXT: [[COND2:%.*]] = call i1 @foo()
277-
; CHECK-NEXT: br i1 [[COND2]], label [[SUCC]], label [[EXIT:%.*]]
276+
; CHECK-NEXT: br i1 [[COND]], label [[SUCC]], label [[EXIT:%.*]]
278277
; CHECK: Exit:
279278
; CHECK-NEXT: ret void
280279
;

llvm/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,10 @@ define i32 @foo(i32 %i) nounwind ssp !dbg !0 {
77
; CHECK-NEXT: entry:
88
; CHECK-NEXT: #dbg_value(i32 [[I:%.*]], [[META7:![0-9]+]], !DIExpression(), [[META8:![0-9]+]])
99
; CHECK-NEXT: #dbg_value(i32 0, [[META9:![0-9]+]], !DIExpression(), [[META11:![0-9]+]])
10-
; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[I]], 0, !dbg [[DBG12:![0-9]+]]
11-
; CHECK-NEXT: br i1 [[COND]], label [[THEN:%.*]], label [[ELSE:%.*]], !dbg [[DBG12]]
12-
; CHECK: then:
13-
; CHECK-NEXT: [[CALL_1:%.*]] = call i32 (...) @bar(), !dbg [[DBG13:![0-9]+]]
14-
; CHECK-NEXT: #dbg_value(i32 [[CALL_1]], [[META9]], !DIExpression(), [[DBG13]])
15-
; CHECK-NEXT: br label [[EXIT:%.*]], !dbg [[DBG15:![0-9]+]]
16-
; CHECK: else:
17-
; CHECK-NEXT: [[CALL_2:%.*]] = call i32 (...) @bar(), !dbg [[DBG16:![0-9]+]]
18-
; CHECK-NEXT: #dbg_value(i32 [[CALL_2]], [[META9]], !DIExpression(), [[DBG16]])
19-
; CHECK-NEXT: br label [[EXIT]], !dbg [[DBG18:![0-9]+]]
20-
; CHECK: exit:
21-
; CHECK-NEXT: [[K_0:%.*]] = phi i32 [ [[CALL_1]], [[THEN]] ], [ [[CALL_2]], [[ELSE]] ]
22-
; CHECK-NEXT: ret i32 [[K_0]], !dbg [[DBG19:![0-9]+]]
10+
; CHECK-NEXT: [[CALL_1:%.*]] = call i32 (...) @bar(), !dbg [[DBG12:![0-9]+]]
11+
; CHECK-NEXT: #dbg_value(i32 [[CALL_1]], [[META9]], !DIExpression(), [[META13:![0-9]+]])
12+
; CHECK-NEXT: #dbg_value(i32 [[CALL_1]], [[META9]], !DIExpression(), [[META15:![0-9]+]])
13+
; CHECK-NEXT: ret i32 [[CALL_1]], !dbg [[DBG17:![0-9]+]]
2314
;
2415
entry:
2516
call void @llvm.dbg.value(metadata i32 %i, metadata !6, metadata !DIExpression()), !dbg !7
@@ -46,8 +37,8 @@ define i1 @hoist_with_debug2(i32 %x) !dbg !22 {
4637
; CHECK-LABEL: @hoist_with_debug2(
4738
; CHECK-NEXT: entry:
4839
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp ugt i32 [[X:%.*]], 2
49-
; CHECK-NEXT: #dbg_value(i32 [[X]], [[META21:![0-9]+]], !DIExpression(), [[META23:![0-9]+]])
50-
; CHECK-NEXT: #dbg_value(i32 [[X]], [[META21]], !DIExpression(), [[META23]])
40+
; CHECK-NEXT: #dbg_value(i32 [[X]], [[META19:![0-9]+]], !DIExpression(), [[META21:![0-9]+]])
41+
; CHECK-NEXT: #dbg_value(i32 [[X]], [[META19]], !DIExpression(), [[META21]])
5142
; CHECK-NEXT: [[DOT:%.*]] = select i1 [[TOBOOL_NOT]], i1 false, i1 true
5243
; CHECK-NEXT: ret i1 [[DOT]]
5344
;

llvm/test/Transforms/SimplifyCFG/hoisting-metadata.ll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
12
; RUN: opt -p simplifycfg -S %s | FileCheck %s
23

34
declare void @init(ptr)
@@ -8,9 +9,7 @@ define i64 @hoist_load_with_matching_pointers_and_tbaa(i1 %c) {
89
; CHECK-NEXT: [[ENTRY:.*:]]
910
; CHECK-NEXT: [[TMP:%.*]] = alloca i64, align 8
1011
; CHECK-NEXT: call void @init(ptr [[TMP]])
11-
; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[TMP]], align 8, !tbaa [[M:!.+]]
12-
; CHECK-NEXT: [[TMP1:%.*]] = load i64, ptr [[TMP]], align 8, !tbaa [[M]]
13-
; CHECK-NEXT: [[P:%.*]] = select i1 [[C]], i64 [[TMP0]], i64 [[TMP1]]
12+
; CHECK-NEXT: [[P:%.*]] = load i64, ptr [[TMP]], align 8, !tbaa [[TBAA0:![0-9]+]]
1413
; CHECK-NEXT: ret i64 [[P]]
1514
;
1615
entry:
@@ -40,9 +39,7 @@ define i64 @hoist_load_with_matching_tbaa_different_pointers(i1 %c) {
4039
; CHECK-NEXT: call void @init(ptr [[TMP]])
4140
; CHECK-NEXT: call void @init(ptr [[TMP_1]])
4241
; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[TMP]], align 8
43-
; CHECK-NOT: !tbaa
4442
; CHECK-NEXT: [[TMP1:%.*]] = load i64, ptr [[TMP_1]], align 8
45-
; CHECK-NOT: !tbaa
4643
; CHECK-NEXT: [[P:%.*]] = select i1 [[C]], i64 [[TMP0]], i64 [[TMP1]]
4744
; CHECK-NEXT: ret i64 [[P]]
4845
;
@@ -72,11 +69,7 @@ define i64 @hoist_load_with_different_tbaa(i1 %c) {
7269
; CHECK-NEXT: [[ENTRY:.*:]]
7370
; CHECK-NEXT: [[TMP:%.*]] = alloca i64, align 8
7471
; CHECK-NEXT: call void @init(ptr [[TMP]])
75-
; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[TMP]], align 8
76-
; CHECK-NOT: !tbaa
77-
; CHECK-NEXT: [[TMP1:%.*]] = load i64, ptr [[TMP]], align 8
78-
; CHECK-NOT: !tbaa
79-
; CHECK-NEXT: [[P:%.*]] = select i1 [[C]], i64 [[TMP0]], i64 [[TMP1]]
72+
; CHECK-NEXT: [[P:%.*]] = load i64, ptr [[TMP]], align 8, !tbaa [[TBAA5:![0-9]+]]
8073
; CHECK-NEXT: ret i64 [[P]]
8174
;
8275
entry:
@@ -104,7 +97,6 @@ define i64 @hoist_different_ops(i1 %c, i64 %a) {
10497
; CHECK-NEXT: [[TMP:%.*]] = alloca i64, align 8
10598
; CHECK-NEXT: call void @init(ptr [[TMP]])
10699
; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[TMP]], align 8
107-
; CHECK-NOT: !tbaa
108100
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[A]], 123
109101
; CHECK-NEXT: [[P:%.*]] = select i1 [[C]], i64 [[TMP0]], i64 [[TMP1]]
110102
; CHECK-NEXT: ret i64 [[P]]
@@ -133,3 +125,11 @@ exit:
133125
!3 = !{!"omnipotent char", !4, i64 0}
134126
!4 = !{!"Simple C++ TBAA"}
135127
!5 = !{!3, !3, i64 0}
128+
;.
129+
; CHECK: [[TBAA0]] = !{[[META1:![0-9]+]], [[META1]], i64 0}
130+
; CHECK: [[META1]] = !{!"p2 long long", [[META2:![0-9]+]], i64 0}
131+
; CHECK: [[META2]] = !{!"any pointer", [[META3:![0-9]+]], i64 0}
132+
; CHECK: [[META3]] = !{!"omnipotent char", [[META4:![0-9]+]], i64 0}
133+
; CHECK: [[META4]] = !{!"Simple C++ TBAA"}
134+
; CHECK: [[TBAA5]] = !{[[META3]], [[META3]], i64 0}
135+
;.

llvm/test/Transforms/SimplifyCFG/merge-deopt-bundle-constants.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ target triple = "x86_64-unknown-linux-gnu"
77
define void @test_01(i1 %cond) gc "statepoint-example" personality ptr @zot {
88
; CHECK-LABEL: @test_01(
99
; CHECK-NEXT: bb:
10+
; CHECK-NEXT: [[TMP4:%.*]] = call ptr @wibble()
1011
; CHECK-NEXT: br i1 [[COND:%.*]], label [[BB3:%.*]], label [[BB8:%.*]]
1112
; CHECK: bb3:
12-
; CHECK-NEXT: [[TMP4:%.*]] = call ptr @wibble()
1313
; CHECK-NEXT: [[TMP6:%.*]] = invoke align 8 dereferenceable_or_null(8) ptr addrspace(1) [[TMP4]](ptr addrspace(1) undef) [ "deopt"(i32 0) ]
1414
; CHECK-NEXT: to label [[BB7:%.*]] unwind label [[BB13:%.*]]
1515
; CHECK: bb7:
1616
; CHECK-NEXT: unreachable
1717
; CHECK: bb8:
18-
; CHECK-NEXT: [[TMP9:%.*]] = call ptr @wibble()
19-
; CHECK-NEXT: [[TMP11:%.*]] = invoke align 8 dereferenceable_or_null(8) ptr addrspace(1) [[TMP9]](ptr addrspace(1) undef) [ "deopt"(i32 1) ]
18+
; CHECK-NEXT: [[TMP11:%.*]] = invoke align 8 dereferenceable_or_null(8) ptr addrspace(1) [[TMP4]](ptr addrspace(1) undef) [ "deopt"(i32 1) ]
2019
; CHECK-NEXT: to label [[BB12:%.*]] unwind label [[BB13]]
2120
; CHECK: bb12:
2221
; CHECK-NEXT: unreachable

llvm/test/Transforms/SimplifyCFG/multiple-phis.ll

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,28 +265,24 @@ define i8 @merge1_unfoldable_all_block(i8 noundef %arg, i1 %c1, i1 %c2) {
265265
; CHECK-LABEL: define i8 @merge1_unfoldable_all_block
266266
; CHECK-SAME: (i8 noundef [[ARG:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) {
267267
; CHECK-NEXT: entry:
268+
; CHECK-NEXT: call void @dummy()
268269
; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [
269270
; CHECK-NEXT: i8 -123, label [[CASE0:%.*]]
270-
; CHECK-NEXT: i8 66, label [[CASE1:%.*]]
271+
; CHECK-NEXT: i8 66, label [[SUCC:%.*]]
271272
; CHECK-NEXT: i8 123, label [[CASE2:%.*]]
272273
; CHECK-NEXT: ]
273274
; CHECK: unreachable:
274275
; CHECK-NEXT: unreachable
275276
; CHECK: case0:
276-
; CHECK-NEXT: call void @dummy()
277-
; CHECK-NEXT: br i1 [[C1]], label [[COMMONPRED:%.*]], label [[SUCC:%.*]]
278-
; CHECK: case1:
279-
; CHECK-NEXT: call void @dummy()
280-
; CHECK-NEXT: br label [[SUCC]]
277+
; CHECK-NEXT: br i1 [[C1]], label [[COMMONPRED:%.*]], label [[SUCC]]
281278
; CHECK: case2:
282-
; CHECK-NEXT: call void @dummy()
283279
; CHECK-NEXT: br label [[SUCC]]
284280
; CHECK: CommonPred:
285281
; CHECK-NEXT: call void @dummy()
286282
; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[C2]], i8 4, i8 3
287283
; CHECK-NEXT: br label [[SUCC]]
288284
; CHECK: Succ:
289-
; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ 0, [[CASE0]] ], [ 1, [[CASE1]] ], [ 2, [[CASE2]] ], [ [[SPEC_SELECT]], [[COMMONPRED]] ]
285+
; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ 0, [[CASE0]] ], [ 2, [[CASE2]] ], [ 1, [[ENTRY:%.*]] ], [ [[SPEC_SELECT]], [[COMMONPRED]] ]
290286
; CHECK-NEXT: ret i8 [[PHI2]]
291287
;
292288
entry:

0 commit comments

Comments
 (0)