Skip to content

Commit 93caf26

Browse files
author
git apple-llvm automerger
committed
Merge commit '92924333c068' from swift/release/5.3 into swift/master
2 parents b2e24d7 + 9292433 commit 93caf26

File tree

5 files changed

+102
-27
lines changed

5 files changed

+102
-27
lines changed

llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ struct CriticalEdgeSplittingOptions {
127127
bool KeepOneInputPHIs = false;
128128
bool PreserveLCSSA = false;
129129
bool IgnoreUnreachableDests = false;
130+
/// SplitCriticalEdge is guaranteed to preserve loop-simplify form if LI is
131+
/// provided. If it cannot be preserved, no splitting will take place. If it
132+
/// is not set, preserve loop-simplify form if possible.
133+
bool PreserveLoopSimplify = true;
130134

131135
CriticalEdgeSplittingOptions(DominatorTree *DT = nullptr,
132136
LoopInfo *LI = nullptr,
@@ -153,6 +157,11 @@ struct CriticalEdgeSplittingOptions {
153157
IgnoreUnreachableDests = true;
154158
return *this;
155159
}
160+
161+
CriticalEdgeSplittingOptions &unsetPreserveLoopSimplify() {
162+
PreserveLoopSimplify = false;
163+
return *this;
164+
}
156165
};
157166

158167
/// If this edge is a critical edge, insert a new node to split the critical

llvm/lib/Transforms/Scalar/GVN.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,8 +2478,11 @@ bool GVN::performPRE(Function &F) {
24782478
/// Split the critical edge connecting the given two blocks, and return
24792479
/// the block inserted to the critical edge.
24802480
BasicBlock *GVN::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
2481-
BasicBlock *BB =
2482-
SplitCriticalEdge(Pred, Succ, CriticalEdgeSplittingOptions(DT, LI));
2481+
// GVN does not require loop-simplify, do not try to preserve it if it is not
2482+
// possible.
2483+
BasicBlock *BB = SplitCriticalEdge(
2484+
Pred, Succ,
2485+
CriticalEdgeSplittingOptions(DT, LI).unsetPreserveLoopSimplify());
24832486
if (MD)
24842487
MD->invalidateCachedPredecessors();
24852488
InvalidBlockRPONumbers = true;
@@ -2718,7 +2721,6 @@ class llvm::gvn::GVNLegacyPass : public FunctionPass {
27182721
AU.addPreserved<GlobalsAAWrapperPass>();
27192722
AU.addPreserved<TargetLibraryInfoWrapperPass>();
27202723
AU.addPreserved<LoopInfoWrapperPass>();
2721-
AU.addPreservedID(LoopSimplifyID);
27222724
AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
27232725
}
27242726

llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,47 @@ llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
158158
isa<UnreachableInst>(DestBB->getFirstNonPHIOrDbgOrLifetime()))
159159
return nullptr;
160160

161+
auto *LI = Options.LI;
162+
SmallVector<BasicBlock *, 4> LoopPreds;
163+
// Check if extra modifications will be required to preserve loop-simplify
164+
// form after splitting. If it would require splitting blocks with IndirectBr
165+
// terminators, bail out if preserving loop-simplify form is requested.
166+
if (LI) {
167+
if (Loop *TIL = LI->getLoopFor(TIBB)) {
168+
169+
// The only that we can break LoopSimplify form by splitting a critical
170+
// edge is if after the split there exists some edge from TIL to DestBB
171+
// *and* the only edge into DestBB from outside of TIL is that of
172+
// NewBB. If the first isn't true, then LoopSimplify still holds, NewBB
173+
// is the new exit block and it has no non-loop predecessors. If the
174+
// second isn't true, then DestBB was not in LoopSimplify form prior to
175+
// the split as it had a non-loop predecessor. In both of these cases,
176+
// the predecessor must be directly in TIL, not in a subloop, or again
177+
// LoopSimplify doesn't hold.
178+
for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E;
179+
++I) {
180+
BasicBlock *P = *I;
181+
if (P == TIBB)
182+
continue; // The new block is known.
183+
if (LI->getLoopFor(P) != TIL) {
184+
// No need to re-simplify, it wasn't to start with.
185+
LoopPreds.clear();
186+
break;
187+
}
188+
LoopPreds.push_back(P);
189+
}
190+
// Loop-simplify form can be preserved, if we can split all in-loop
191+
// predecessors.
192+
if (any_of(LoopPreds, [](BasicBlock *Pred) {
193+
return isa<IndirectBrInst>(Pred->getTerminator());
194+
})) {
195+
if (Options.PreserveLoopSimplify)
196+
return nullptr;
197+
LoopPreds.clear();
198+
}
199+
}
200+
}
201+
161202
// Create a new basic block, linking it into the CFG.
162203
BasicBlock *NewBB = BasicBlock::Create(TI->getContext(),
163204
TIBB->getName() + "." + DestBB->getName() + "_crit_edge");
@@ -212,7 +253,6 @@ llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
212253
// If we have nothing to update, just return.
213254
auto *DT = Options.DT;
214255
auto *PDT = Options.PDT;
215-
auto *LI = Options.LI;
216256
auto *MSSAU = Options.MSSAU;
217257
if (MSSAU)
218258
MSSAU->wireOldPredecessorsToNewImmediatePredecessor(
@@ -281,28 +321,6 @@ llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
281321
createPHIsForSplitLoopExit(TIBB, NewBB, DestBB);
282322
}
283323

284-
// The only that we can break LoopSimplify form by splitting a critical
285-
// edge is if after the split there exists some edge from TIL to DestBB
286-
// *and* the only edge into DestBB from outside of TIL is that of
287-
// NewBB. If the first isn't true, then LoopSimplify still holds, NewBB
288-
// is the new exit block and it has no non-loop predecessors. If the
289-
// second isn't true, then DestBB was not in LoopSimplify form prior to
290-
// the split as it had a non-loop predecessor. In both of these cases,
291-
// the predecessor must be directly in TIL, not in a subloop, or again
292-
// LoopSimplify doesn't hold.
293-
SmallVector<BasicBlock *, 4> LoopPreds;
294-
for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E;
295-
++I) {
296-
BasicBlock *P = *I;
297-
if (P == NewBB)
298-
continue; // The new block is known.
299-
if (LI->getLoopFor(P) != TIL) {
300-
// No need to re-simplify, it wasn't to start with.
301-
LoopPreds.clear();
302-
break;
303-
}
304-
LoopPreds.push_back(P);
305-
}
306324
if (!LoopPreds.empty()) {
307325
assert(!DestBB->isEHPad() && "We don't split edges to EH pads!");
308326
BasicBlock *NewExitBB = SplitBlockPredecessors(
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -gvn -S -verify-loop-info %s | FileCheck %s
3+
4+
; Make sure we do not try to preserve loop-simplify form, if it would mean
5+
; splitting blocks with indirectbr predecessors.
6+
7+
declare i1 @cond()
8+
9+
define i32 @foo(i8* %p1, i8* %p2, i32* %p3) {
10+
; CHECK-LABEL: @foo(
11+
; CHECK-NEXT: bb:
12+
; CHECK-NEXT: br label [[LOOP_HEADER:%.*]]
13+
; CHECK: loop.header:
14+
; CHECK-NEXT: store i32 0, i32* [[P3:%.*]], align 4
15+
; CHECK-NEXT: indirectbr i8* [[P1:%.*]], [label [[LOOP_LATCH1:%.*]], label %loop.latch2]
16+
; CHECK: loop.latch1:
17+
; CHECK-NEXT: [[C:%.*]] = call i1 @cond()
18+
; CHECK-NEXT: br i1 [[C]], label [[LOOP_HEADER]], label [[LOOP_LATCH1_EXIT_CRIT_EDGE:%.*]]
19+
; CHECK: loop.latch1.exit_crit_edge:
20+
; CHECK-NEXT: [[X_PRE:%.*]] = load i32, i32* [[P3]], align 4
21+
; CHECK-NEXT: br label [[EXIT:%.*]]
22+
; CHECK: loop.latch2:
23+
; CHECK-NEXT: indirectbr i8* [[P2:%.*]], [label [[LOOP_HEADER]], label %exit]
24+
; CHECK: exit:
25+
; CHECK-NEXT: [[X:%.*]] = phi i32 [ [[X_PRE]], [[LOOP_LATCH1_EXIT_CRIT_EDGE]] ], [ 0, [[LOOP_LATCH2:%.*]] ]
26+
; CHECK-NEXT: ret i32 [[X]]
27+
;
28+
bb:
29+
br label %loop.header
30+
31+
loop.header: ; preds = %loop.latch2, %loop.latch1, %bb
32+
store i32 0, i32* %p3, align 4
33+
indirectbr i8* %p1, [label %loop.latch1, label %loop.latch2]
34+
35+
loop.latch1: ; preds = %loop.header
36+
%c = call i1 @cond()
37+
br i1 %c, label %loop.header, label %exit
38+
39+
loop.latch2: ; preds = %loop.header
40+
indirectbr i8* %p2, [label %loop.header, label %exit]
41+
42+
exit: ; preds = %loop.latch2, %loop.latch1
43+
%x = load i32, i32* %p3, align 4
44+
%y = add i32 %x, 0
45+
ret i32 %y
46+
}

llvm/test/Transforms/GVN/preserve-analysis.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
; CHECK: Global Value Numbering
1313
; CHECK-NOT: Dominator Tree Construction
1414
; CHECK-NOT: Natural Loop Information
15-
; CHECK-NOT: Canonicalize natural loops
15+
; CHECK: Canonicalize natural loops
1616

1717
; NEW-PM-DAG: Running analysis: LoopAnalysis on test
1818
; NEW-PM-DAG: Running analysis: DominatorTreeAnalysis on test

0 commit comments

Comments
 (0)