diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h b/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h --- a/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h +++ b/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h @@ -29,7 +29,7 @@ bool HoistCommonInsts = false; bool SinkCommonInsts = false; bool SimplifyCondBranch = true; - bool FoldTwoEntryPHINode = true; + bool SpeculateBlocks = true; AssumptionCache *AC = nullptr; @@ -71,8 +71,8 @@ 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 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -776,8 +776,8 @@ 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 --- a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp +++ b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp @@ -348,7 +348,7 @@ 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 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -2842,6 +2842,9 @@ /// \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(BrCond)) @@ -7276,7 +7279,7 @@ 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 --- 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,print)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-17 ; CHECK-17: function(print,print) -; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='function(simplifycfg,simplifycfg)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-18 -; CHECK-18: function(simplifycfg,simplifycfg) +; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='function(simplifycfg,simplifycfg)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-18 +; CHECK-18: function(simplifycfg,simplifycfg) ; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='function(loop-vectorize,loop-vectorize)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-19 ; CHECK-19: function(loop-vectorize,loop-vectorize) diff --git a/llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll b/llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll new file mode 100644 --- /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' | FileCheck %s --check-prefix=YES +; RUN: opt < %s -S -passes='simplifycfg' | 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 +}