[llvm] d49984f - [SimplifyCFG] Add option to not speculate blocks
Arthur Eubanks via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 22 08:54:50 PDT 2023
Author: Arthur Eubanks
Date: 2023-06-22T08:51:40-07:00
New Revision: d49984fa4f83d6fca83b2c594a2475956de170f1
URL: https://github.com/llvm/llvm-project/commit/d49984fa4f83d6fca83b2c594a2475956de170f1
DIFF: https://github.com/llvm/llvm-project/commit/d49984fa4f83d6fca83b2c594a2475956de170f1.diff
LOG: [SimplifyCFG] Add option to not speculate blocks
Required for phase ordering changes to not regress Rust code with D145265.
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D153391
Added:
llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll
Modified:
llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h
llvm/lib/Passes/PassBuilder.cpp
llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Other/new-pm-print-pipeline.ll
Removed:
################################################################################
diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h b/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h
index 7af879638a4d8..8008fc6e8422d 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h
@@ -29,7 +29,7 @@ struct SimplifyCFGOptions {
bool HoistCommonInsts = false;
bool SinkCommonInsts = false;
bool SimplifyCondBranch = true;
- bool FoldTwoEntryPHINode = true;
+ bool SpeculateBlocks = true;
AssumptionCache *AC = nullptr;
@@ -71,8 +71,8 @@ struct SimplifyCFGOptions {
return *this;
}
- SimplifyCFGOptions &setFoldTwoEntryPHINode(bool B) {
- FoldTwoEntryPHINode = B;
+ SimplifyCFGOptions &speculateBlocks(bool B) {
+ SpeculateBlocks = B;
return *this;
}
};
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 181dd15d8f84f..7fd8b578ac3f9 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -776,8 +776,8 @@ Expected<SimplifyCFGOptions> parseSimplifyCFGOptions(StringRef Params) {
std::tie(ParamName, Params) = Params.split(';');
bool Enable = !ParamName.consume_front("no-");
- if (ParamName == "fold-two-entry-phi") {
- Result.setFoldTwoEntryPHINode(Enable);
+ if (ParamName == "speculate-blocks") {
+ Result.speculateBlocks(Enable);
} else if (ParamName == "simplify-cond-branch") {
Result.setSimplifyCondBranch(Enable);
} else if (ParamName == "forward-switch-cond") {
diff --git a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
index 46651cede7895..7017f6adf3a2b 100644
--- a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
+++ b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
@@ -348,7 +348,7 @@ void SimplifyCFGPass::printPipeline(
OS << (Options.NeedCanonicalLoop ? "" : "no-") << "keep-loops;";
OS << (Options.HoistCommonInsts ? "" : "no-") << "hoist-common-insts;";
OS << (Options.SinkCommonInsts ? "" : "no-") << "sink-common-insts;";
- OS << (Options.FoldTwoEntryPHINode ? "" : "no-") << "fold-two-entry-phi;";
+ OS << (Options.SpeculateBlocks ? "" : "no-") << "speculate-blocks;";
OS << (Options.SimplifyCondBranch ? "" : "no-") << "simplify-cond-branch";
OS << '>';
}
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index a18b45f178c98..5c8821f6f2b23 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2842,6 +2842,9 @@ static bool validateAndCostRequiredSelects(BasicBlock *BB, BasicBlock *ThenBB,
/// \returns true if the conditional block is removed.
bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI,
BasicBlock *ThenBB) {
+ if (!Options.SpeculateBlocks)
+ return false;
+
// Be conservative for now. FP select instruction can often be expensive.
Value *BrCond = BI->getCondition();
if (isa<FCmpInst>(BrCond))
@@ -7276,7 +7279,7 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
IRBuilder<> Builder(BB);
- if (Options.FoldTwoEntryPHINode &&
+ if (Options.SpeculateBlocks &&
!BB->getParent()->hasFnAttribute(Attribute::OptForFuzzing)) {
// If there is a trivial two-entry PHI node in this basic block, and we can
// eliminate it, do so now.
diff --git a/llvm/test/Other/new-pm-print-pipeline.ll b/llvm/test/Other/new-pm-print-pipeline.ll
index f21ebd6bb79e2..637e29fb1b48b 100644
--- a/llvm/test/Other/new-pm-print-pipeline.ll
+++ b/llvm/test/Other/new-pm-print-pipeline.ll
@@ -52,8 +52,8 @@
; 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
; CHECK-17: function(print<stack-lifetime><may>,print<stack-lifetime><must>)
-; 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;fold-two-entry-phi;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-fold-two-entry-phi;no-simplify-cond-branch>)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-18
-; 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;fold-two-entry-phi;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-fold-two-entry-phi;no-simplify-cond-branch>)
+; 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
+; 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>)
; 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
; CHECK-19: function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,loop-vectorize<interleave-forced-only;vectorize-forced-only;>)
diff --git a/llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll b/llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll
new file mode 100644
index 0000000000000..9921942bbdea1
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll
@@ -0,0 +1,29 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt < %s -S -passes='simplifycfg<speculate-blocks>' | FileCheck %s --check-prefix=YES
+; RUN: opt < %s -S -passes='simplifycfg<no-speculate-blocks>' | FileCheck %s --check-prefix=NO
+
+define i32 @f(i1 %a) {
+; YES-LABEL: define i32 @f
+; YES-SAME: (i1 [[A:%.*]]) {
+; YES-NEXT: entry:
+; YES-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[A]], i32 5, i32 2
+; YES-NEXT: ret i32 [[SPEC_SELECT]]
+;
+; NO-LABEL: define i32 @f
+; NO-SAME: (i1 [[A:%.*]]) {
+; NO-NEXT: entry:
+; NO-NEXT: br i1 [[A]], label [[BB:%.*]], label [[BB2:%.*]]
+; NO: bb:
+; NO-NEXT: br label [[BB2]]
+; NO: bb2:
+; NO-NEXT: [[R:%.*]] = phi i32 [ 2, [[ENTRY:%.*]] ], [ 5, [[BB]] ]
+; NO-NEXT: ret i32 [[R]]
+;
+entry:
+ br i1 %a, label %bb, label %bb2
+bb:
+ br label %bb2
+bb2:
+ %r = phi i32 [ 2, %entry ], [ 5, %bb ]
+ ret i32 %r
+}
More information about the llvm-commits
mailing list