Skip to content

Commit 7a8e32b

Browse files
committed
Add "speculate-unpredictables" SimplifyCFGOptions.
1 parent fa967f8 commit 7a8e32b

File tree

7 files changed

+40
-22
lines changed

7 files changed

+40
-22
lines changed

llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct SimplifyCFGOptions {
3030
bool SinkCommonInsts = false;
3131
bool SimplifyCondBranch = true;
3232
bool SpeculateBlocks = true;
33+
bool SpeculateUnpredictables = false;
3334

3435
AssumptionCache *AC = nullptr;
3536

@@ -75,6 +76,10 @@ struct SimplifyCFGOptions {
7576
SpeculateBlocks = B;
7677
return *this;
7778
}
79+
SimplifyCFGOptions &speculateUnpredictables(bool B) {
80+
SpeculateUnpredictables = B;
81+
return *this;
82+
}
7883
};
7984

8085
} // namespace llvm

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,8 @@ Expected<SimplifyCFGOptions> parseSimplifyCFGOptions(StringRef Params) {
845845
Result.hoistCommonInsts(Enable);
846846
} else if (ParamName == "sink-common-insts") {
847847
Result.sinkCommonInsts(Enable);
848+
} else if (ParamName == "speculate-unpredictables") {
849+
Result.speculateUnpredictables(Enable);
848850
} else if (Enable && ParamName.consume_front("bonus-inst-threshold=")) {
849851
APInt BonusInstThreshold;
850852
if (ParamName.getAsInteger(0, BonusInstThreshold))

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,8 +1505,9 @@ PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
15051505

15061506
// LoopSink (and other loop passes since the last simplifyCFG) might have
15071507
// resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
1508-
OptimizePM.addPass(
1509-
SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true)));
1508+
OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions()
1509+
.convertSwitchRangeToICmp(true)
1510+
.speculateUnpredictables(true)));
15101511

15111512
// Add the core optimizing pipeline.
15121513
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM),
@@ -2024,9 +2025,10 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
20242025
LateFPM.addPass(DivRemPairsPass());
20252026

20262027
// Delete basic blocks, which optimization passes may have killed.
2027-
LateFPM.addPass(SimplifyCFGPass(
2028-
SimplifyCFGOptions().convertSwitchRangeToICmp(true).hoistCommonInsts(
2029-
true)));
2028+
LateFPM.addPass(SimplifyCFGPass(SimplifyCFGOptions()
2029+
.convertSwitchRangeToICmp(true)
2030+
.hoistCommonInsts(true)
2031+
.speculateUnpredictables(true)));
20302032
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
20312033

20322034
// Drop bodies of available eternally objects to improve GlobalDCE.

llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ static cl::opt<bool> UserSinkCommonInsts(
7777
"sink-common-insts", cl::Hidden, cl::init(false),
7878
cl::desc("Sink common instructions (default = false)"));
7979

80+
static cl::opt<bool> UserSpeculateUnpredictables(
81+
"speculate-unpredictables", cl::Hidden, cl::init(false),
82+
cl::desc("Speculate unpredictable branches (default = false)"));
8083

8184
STATISTIC(NumSimpl, "Number of blocks simplified");
8285

@@ -325,6 +328,8 @@ static void applyCommandLineOverridesToOptions(SimplifyCFGOptions &Options) {
325328
Options.HoistCommonInsts = UserHoistCommonInsts;
326329
if (UserSinkCommonInsts.getNumOccurrences())
327330
Options.SinkCommonInsts = UserSinkCommonInsts;
331+
if (UserSpeculateUnpredictables.getNumOccurrences())
332+
Options.SpeculateUnpredictables = UserSpeculateUnpredictables;
328333
}
329334

330335
SimplifyCFGPass::SimplifyCFGPass() {
@@ -351,7 +356,9 @@ void SimplifyCFGPass::printPipeline(
351356
OS << (Options.HoistCommonInsts ? "" : "no-") << "hoist-common-insts;";
352357
OS << (Options.SinkCommonInsts ? "" : "no-") << "sink-common-insts;";
353358
OS << (Options.SpeculateBlocks ? "" : "no-") << "speculate-blocks;";
354-
OS << (Options.SimplifyCondBranch ? "" : "no-") << "simplify-cond-branch";
359+
OS << (Options.SimplifyCondBranch ? "" : "no-") << "simplify-cond-branch;";
360+
OS << (Options.SpeculateUnpredictables ? "" : "no-")
361+
<< "speculate-unpredictables";
355362
OS << '>';
356363
}
357364

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3476,7 +3476,8 @@ static bool FoldCondBranchOnValueKnownInPredecessor(BranchInst *BI,
34763476
/// Given a BB that starts with the specified two-entry PHI node,
34773477
/// see if we can eliminate it.
34783478
static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
3479-
DomTreeUpdater *DTU, const DataLayout &DL) {
3479+
DomTreeUpdater *DTU, const DataLayout &DL,
3480+
bool SpeculateUnpredictables) {
34803481
// Ok, this is a two entry PHI node. Check to see if this is a simple "if
34813482
// statement", which has a very simple dominance structure. Basically, we
34823483
// are trying to find the condition that is being branched on, which
@@ -3552,7 +3553,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
35523553
InstructionCost Cost = 0;
35533554
InstructionCost Budget =
35543555
TwoEntryPHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
3555-
if (IsUnpredictable)
3556+
if (SpeculateUnpredictables && IsUnpredictable)
35563557
Budget += TTI.getBranchMispredictPenalty();
35573558

35583559
bool Changed = false;
@@ -7818,7 +7819,8 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
78187819
// eliminate it, do so now.
78197820
if (auto *PN = dyn_cast<PHINode>(BB->begin()))
78207821
if (PN->getNumIncomingValues() == 2)
7821-
if (FoldTwoEntryPHINode(PN, TTI, DTU, DL))
7822+
if (FoldTwoEntryPHINode(PN, TTI, DTU, DL,
7823+
Options.SpeculateUnpredictables))
78227824
return true;
78237825
}
78247826

llvm/test/Other/new-pm-print-pipeline.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='function(print<stack-lifetime><may>,print<stack-lifetime><must>)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-17
5050
; CHECK-17: function(print<stack-lifetime><may>,print<stack-lifetime><must>)
5151

52-
; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='function(simplifycfg<bonus-inst-threshold=5;forward-switch-cond;switch-to-lookup;keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,simplifycfg<bonus-inst-threshold=7;no-forward-switch-cond;no-switch-to-lookup;no-keep-loops;no-hoist-common-insts;no-sink-common-insts;no-speculate-blocks;no-simplify-cond-branch>)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-18
53-
; CHECK-18: function(simplifycfg<bonus-inst-threshold=5;forward-switch-cond;no-switch-range-to-icmp;switch-to-lookup;keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,simplifycfg<bonus-inst-threshold=7;no-forward-switch-cond;no-switch-range-to-icmp;no-switch-to-lookup;no-keep-loops;no-hoist-common-insts;no-sink-common-insts;no-speculate-blocks;no-simplify-cond-branch>)
52+
; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='function(simplifycfg<bonus-inst-threshold=5;forward-switch-cond;switch-to-lookup;keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch;speculate-unpredictables>,simplifycfg<bonus-inst-threshold=7;no-forward-switch-cond;no-switch-to-lookup;no-keep-loops;no-hoist-common-insts;no-sink-common-insts;no-speculate-blocks;no-simplify-cond-branch;no-speculate-unpredictables>)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-18
53+
; CHECK-18: function(simplifycfg<bonus-inst-threshold=5;forward-switch-cond;no-switch-range-to-icmp;switch-to-lookup;keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch;speculate-unpredictables>,simplifycfg<bonus-inst-threshold=7;no-forward-switch-cond;no-switch-range-to-icmp;no-switch-to-lookup;no-keep-loops;no-hoist-common-insts;no-sink-common-insts;no-speculate-blocks;no-simplify-cond-branch;no-speculate-unpredictables>)
5454

5555
; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only>,loop-vectorize<interleave-forced-only;vectorize-forced-only>)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-19
5656
; CHECK-19: function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,loop-vectorize<interleave-forced-only;vectorize-forced-only;>)

llvm/test/Transforms/SimplifyCFG/two-entry-phi-fold-unpredictable.ll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 5
22
; Two-entry phi nodes with unpredictable conditions may get increased budget for folding.
33
; RUN: opt < %s -S -passes=simplifycfg | FileCheck --check-prefix=CHECK-NOFOLD %s
4-
; RUN: opt -mtriple=x86_64-unknown-linux-gnu -mattr=+avx2 < %s -S -passes=simplifycfg | FileCheck --check-prefix=CHECK-FOLD %s
4+
; RUN: opt < %s -S -passes='simplifycfg<speculate-unpredictables>' | FileCheck --check-prefix=CHECK-NOFOLD %s
5+
; RUN: opt -mtriple=x86_64-unknown-linux-gnu -mattr=+avx2 < %s -S -passes=simplifycfg | FileCheck --check-prefix=CHECK-NOFOLD %s
6+
; RUN: opt -mtriple=x86_64-unknown-linux-gnu -mattr=+avx2 < %s -S -passes='simplifycfg<speculate-unpredictables>' | FileCheck --check-prefix=CHECK-FOLD %s
57

6-
define { <2 x float>, <2 x float> } @foo(float %arg, <2 x float> %arg1, <2 x float> %arg2) {
8+
define { <2 x float>, <2 x float> } @foo(float %arg, <2 x float> %arg1, <2 x float> %arg2) #0 {
79
; CHECK-NOFOLD-LABEL: define { <2 x float>, <2 x float> } @foo(
8-
; CHECK-NOFOLD-SAME: float [[ARG:%.*]], <2 x float> [[ARG1:%.*]], <2 x float> [[ARG2:%.*]]) {
10+
; CHECK-NOFOLD-SAME: float [[ARG:%.*]], <2 x float> [[ARG1:%.*]], <2 x float> [[ARG2:%.*]]) #[[ATTR0:[0-9]+]] {
911
; CHECK-NOFOLD-NEXT: [[BB:.*]]:
1012
; CHECK-NOFOLD-NEXT: [[I:%.*]] = fcmp fast ogt float [[ARG]], 0x3F747AE140000000
1113
; CHECK-NOFOLD-NEXT: br i1 [[I]], label %[[BB3:.*]], label %[[BB20:.*]], !unpredictable [[META0:![0-9]+]]
@@ -36,10 +38,8 @@ define { <2 x float>, <2 x float> } @foo(float %arg, <2 x float> %arg1, <2 x flo
3638
;
3739
; CHECK-FOLD-LABEL: define { <2 x float>, <2 x float> } @foo(
3840
; CHECK-FOLD-SAME: float [[ARG:%.*]], <2 x float> [[ARG1:%.*]], <2 x float> [[ARG2:%.*]]) #[[ATTR0:[0-9]+]] {
39-
; CHECK-FOLD-NEXT: [[BB:.*]]:
41+
; CHECK-FOLD-NEXT: [[BB:.*:]]
4042
; CHECK-FOLD-NEXT: [[I:%.*]] = fcmp fast ogt float [[ARG]], 0x3F747AE140000000
41-
; CHECK-FOLD-NEXT: br i1 [[I]], label %[[BB3:.*]], label %[[BB20:.*]], !unpredictable [[META0:![0-9]+]]
42-
; CHECK-FOLD: [[BB3]]:
4343
; CHECK-FOLD-NEXT: [[I4:%.*]] = extractelement <2 x float> [[ARG1]], i64 0
4444
; CHECK-FOLD-NEXT: [[I5:%.*]] = fmul fast float [[I4]], [[I4]]
4545
; CHECK-FOLD-NEXT: [[I6:%.*]] = extractelement <2 x float> [[ARG1]], i64 1
@@ -48,18 +48,16 @@ define { <2 x float>, <2 x float> } @foo(float %arg, <2 x float> %arg1, <2 x flo
4848
; CHECK-FOLD-NEXT: [[I9:%.*]] = extractelement <2 x float> [[ARG2]], i64 0
4949
; CHECK-FOLD-NEXT: [[I10:%.*]] = fmul fast float [[I9]], [[I9]]
5050
; CHECK-FOLD-NEXT: [[I11:%.*]] = fadd fast float [[I8]], [[I10]]
51-
; CHECK-FOLD-NEXT: [[I12:%.*]] = tail call fast noundef float @llvm.sqrt.f32(float [[I11]])
51+
; CHECK-FOLD-NEXT: [[I12:%.*]] = tail call fast float @llvm.sqrt.f32(float [[I11]])
5252
; CHECK-FOLD-NEXT: [[I13:%.*]] = fdiv fast float 0x3FEFD70A40000000, [[I12]]
5353
; CHECK-FOLD-NEXT: [[I14:%.*]] = fmul fast float [[I13]], [[I4]]
5454
; CHECK-FOLD-NEXT: [[I15:%.*]] = insertelement <2 x float> poison, float [[I14]], i64 0
5555
; CHECK-FOLD-NEXT: [[I16:%.*]] = fmul fast float [[I13]], [[I6]]
5656
; CHECK-FOLD-NEXT: [[I17:%.*]] = insertelement <2 x float> [[I15]], float [[I16]], i64 1
5757
; CHECK-FOLD-NEXT: [[I18:%.*]] = fmul fast float [[I13]], [[I9]]
5858
; CHECK-FOLD-NEXT: [[I19:%.*]] = insertelement <2 x float> [[ARG2]], float [[I18]], i64 0
59-
; CHECK-FOLD-NEXT: br label %[[BB20]]
60-
; CHECK-FOLD: [[BB20]]:
61-
; CHECK-FOLD-NEXT: [[I21:%.*]] = phi nsz <2 x float> [ [[I17]], %[[BB3]] ], [ zeroinitializer, %[[BB]] ]
62-
; CHECK-FOLD-NEXT: [[I22:%.*]] = phi nsz <2 x float> [ [[I19]], %[[BB3]] ], [ zeroinitializer, %[[BB]] ]
59+
; CHECK-FOLD-NEXT: [[I21:%.*]] = select nsz i1 [[I]], <2 x float> [[I17]], <2 x float> zeroinitializer, !unpredictable [[META0:![0-9]+]]
60+
; CHECK-FOLD-NEXT: [[I22:%.*]] = select nsz i1 [[I]], <2 x float> [[I19]], <2 x float> zeroinitializer, !unpredictable [[META0]]
6361
; CHECK-FOLD-NEXT: [[I23:%.*]] = insertvalue { <2 x float>, <2 x float> } poison, <2 x float> [[I21]], 0
6462
; CHECK-FOLD-NEXT: [[I24:%.*]] = insertvalue { <2 x float>, <2 x float> } [[I23]], <2 x float> [[I22]], 1
6563
; CHECK-FOLD-NEXT: ret { <2 x float>, <2 x float> } [[I24]]
@@ -97,4 +95,6 @@ bb20: ; preds = %bb3, %bb
9795

9896
declare float @llvm.sqrt.f32(float)
9997

98+
attributes #0 = { nounwind }
99+
100100
!0 = !{}

0 commit comments

Comments
 (0)