Skip to content

Commit febeaa9

Browse files
committed
!fixup Enable hoisting if all ops are exactly the same.
1 parent 38f39f8 commit febeaa9

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
@@ -283,7 +283,7 @@ class SimplifyCFGOpt {
283283
bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI,
284284
IRBuilder<> &Builder);
285285

286-
bool hoistCommonCodeFromSuccessors(BasicBlock *BB, bool EqTermsOnly);
286+
bool hoistCommonCodeFromSuccessors(BasicBlock *BB, bool EqInstsOnly);
287287
bool hoistSuccIdenticalTerminatorToSwitchOrIf(
288288
Instruction *TI, Instruction *I1,
289289
SmallVectorImpl<Instruction *> &OtherSuccTIs);
@@ -1691,12 +1691,12 @@ namespace {
16911691
} // end anonymous namespace
16921692

16931693
/// Hoist any common code in the successor blocks up into the block. This
1694-
/// function guarantees that BB dominates all successors. If EqTermsOnly is
1695-
/// given, only perform hoisting in case both blocks only contain a terminator.
1696-
/// In that case, only the original BI will be replaced and selects for PHIs are
1697-
/// added.
1694+
/// function guarantees that BB dominates all successors. If EqInstsOnly is
1695+
/// given, only perform hoisting in case both blocks contain equivalent
1696+
/// instructions. In that case, no selects need to be added for the hoisted
1697+
/// instruction. We still have to add selects for PHIs
16981698
bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
1699-
bool EqTermsOnly) {
1699+
bool EqInstsOnly) {
17001700
// This does very trivial matching, with limited scanning, to find identical
17011701
// instructions in the two blocks. In particular, we don't want to get into
17021702
// O(N1*N2*...) situations here where Ni are the sizes of these successors. As
@@ -1726,14 +1726,20 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
17261726
SuccIterPairs.push_back(SuccIterPair(SuccItr, 0));
17271727
}
17281728

1729-
// Check if only hoisting terminators is allowed. This does not add new
1730-
// instructions to the hoist location.
1731-
if (EqTermsOnly) {
1732-
// Skip any debug intrinsics, as they are free to hoist.
1733-
for (auto &SuccIter : make_first_range(SuccIterPairs)) {
1734-
auto *INonDbg = &*skipDebugIntrinsics(SuccIter);
1735-
if (!INonDbg->isTerminator())
1729+
// Check if only hoisting equivalent instructions is allowed. This may add new
1730+
// instructions to the hoist location, but is guaranteed to remove at least
1731+
// twice as many instructions from the source blocks.
1732+
if (EqInstsOnly) {
1733+
LockstepReverseIterator LRI(to_vector(successors(BB)));
1734+
while (LRI.isValid()) {
1735+
auto Insts = *LRI;
1736+
Instruction *I0 = Insts.front();
1737+
if (any_of(Insts.drop_front(), [I0](Instruction *I) {
1738+
return !I->isSameOperationAs(I0) ||
1739+
!equal(I->operands(), I0->operands());
1740+
}))
17361741
return false;
1742+
--LRI;
17371743
}
17381744
// Now we know that we only need to hoist debug intrinsics and the
17391745
// terminator. Let the loop below handle those 2 cases.
@@ -3789,29 +3795,6 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
37893795
dbgs() << " T: " << IfTrue->getName()
37903796
<< " F: " << IfFalse->getName() << "\n");
37913797

3792-
// Collect common TBAA metadata, for instructions that match in all if-blocks
3793-
// and have the same TBAA metadata. If that is the case, they access the same
3794-
// type on all paths and the TBAA info can be preserved after hoisting.
3795-
// TODO: preserve other common metadata.
3796-
LockstepReverseIterator LRI(IfBlocks);
3797-
DenseMap<Instruction *, MDNode *> CommonTBAA;
3798-
while (LRI.isValid()) {
3799-
auto Insts = *LRI;
3800-
Instruction *I0 = Insts.front();
3801-
MDNode *MD = I0->getMetadata(LLVMContext::MD_tbaa);
3802-
if (!MD || any_of(Insts, [I0, MD](Instruction *I) {
3803-
return !I->isSameOperationAs(I0) ||
3804-
!equal(I->operands(), I0->operands()) ||
3805-
I->getMetadata(LLVMContext::MD_tbaa) != MD;
3806-
})) {
3807-
--LRI;
3808-
continue;
3809-
}
3810-
for (Instruction *I : Insts)
3811-
CommonTBAA[I] = MD;
3812-
--LRI;
3813-
}
3814-
38153798
// If we can still promote the PHI nodes after this gauntlet of tests,
38163799
// do all of the PHI's now.
38173800

@@ -3820,10 +3803,6 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
38203803
for (BasicBlock *IfBlock : IfBlocks)
38213804
hoistAllInstructionsInto(DomBlock, DomBI, IfBlock);
38223805

3823-
for (Instruction &I : *DomBlock)
3824-
if (auto *MD = CommonTBAA.lookup(&I))
3825-
I.setMetadata(LLVMContext::MD_tbaa, MD);
3826-
38273806
IRBuilder<NoFolder> Builder(DomBI);
38283807
// Propagate fast-math-flags from phi nodes to replacement selects.
38293808
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)