Skip to content

Commit b201819

Browse files
committed
[NFCI] createCFGSimplificationPass(): migrate to also take SimplifyCFGOptions
Taking so many parameters is simply unmaintainable. We don't want to include the entire llvm/Transforms/Utils/Local.h into llvm/Transforms/Scalar.h so i've split SimplifyCFGOptions into it's own header.
1 parent af19b1c commit b201819

File tree

10 files changed

+110
-85
lines changed

10 files changed

+110
-85
lines changed

llvm/include/llvm/Transforms/Scalar.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_TRANSFORMS_SCALAR_H
1515
#define LLVM_TRANSFORMS_SCALAR_H
1616

17+
#include "llvm/Transforms/Scalar/SimplifyCFGOptions.h"
1718
#include <functional>
1819

1920
namespace llvm {
@@ -256,8 +257,7 @@ FunctionPass *createJumpThreadingPass(int Threshold = -1);
256257
// simplify terminator instructions, convert switches to lookup tables, etc.
257258
//
258259
FunctionPass *createCFGSimplificationPass(
259-
unsigned Threshold = 1, bool ForwardSwitchCond = false,
260-
bool ConvertSwitch = false, bool KeepLoops = true, bool SinkCommon = false,
260+
SimplifyCFGOptions Options = SimplifyCFGOptions(),
261261
std::function<bool(const Function &)> Ftor = nullptr);
262262

263263
//===----------------------------------------------------------------------===//

llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#ifndef LLVM_TRANSFORMS_SCALAR_SIMPLIFYCFG_H
1515
#define LLVM_TRANSFORMS_SCALAR_SIMPLIFYCFG_H
1616

17-
#include "llvm/Transforms/Utils/Local.h"
1817
#include "llvm/IR/Function.h"
1918
#include "llvm/IR/PassManager.h"
19+
#include "llvm/Transforms/Scalar/SimplifyCFGOptions.h"
2020

2121
namespace llvm {
2222

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//===- SimplifyCFGOptions.h - Control structure for SimplifyCFG -*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// A set of parameters used to control the transforms in the SimplifyCFG pass.
10+
// Options may change depending on the position in the optimization pipeline.
11+
// For example, canonical form that includes switches and branches may later be
12+
// replaced by lookup tables and selects.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef LLVM_TRANSFORMS_SCALAR_SIMPLIFYCFGOPTIONS_H
17+
#define LLVM_TRANSFORMS_SCALAR_SIMPLIFYCFGOPTIONS_H
18+
19+
namespace llvm {
20+
21+
class AssumptionCache;
22+
23+
struct SimplifyCFGOptions {
24+
int BonusInstThreshold;
25+
bool ForwardSwitchCondToPhi;
26+
bool ConvertSwitchToLookupTable;
27+
bool NeedCanonicalLoop;
28+
bool SinkCommonInsts;
29+
bool SimplifyCondBranch;
30+
bool FoldTwoEntryPHINode;
31+
32+
AssumptionCache *AC;
33+
34+
SimplifyCFGOptions(unsigned BonusThreshold = 1,
35+
bool ForwardSwitchCond = false,
36+
bool SwitchToLookup = false, bool CanonicalLoops = true,
37+
bool SinkCommon = false,
38+
AssumptionCache *AssumpCache = nullptr,
39+
bool SimplifyCondBranch = true,
40+
bool FoldTwoEntryPHINode = true)
41+
: BonusInstThreshold(BonusThreshold),
42+
ForwardSwitchCondToPhi(ForwardSwitchCond),
43+
ConvertSwitchToLookupTable(SwitchToLookup),
44+
NeedCanonicalLoop(CanonicalLoops), SinkCommonInsts(SinkCommon),
45+
SimplifyCondBranch(SimplifyCondBranch),
46+
FoldTwoEntryPHINode(FoldTwoEntryPHINode), AC(AssumpCache) {}
47+
48+
// Support 'builder' pattern to set members by name at construction time.
49+
SimplifyCFGOptions &bonusInstThreshold(int I) {
50+
BonusInstThreshold = I;
51+
return *this;
52+
}
53+
SimplifyCFGOptions &forwardSwitchCondToPhi(bool B) {
54+
ForwardSwitchCondToPhi = B;
55+
return *this;
56+
}
57+
SimplifyCFGOptions &convertSwitchToLookupTable(bool B) {
58+
ConvertSwitchToLookupTable = B;
59+
return *this;
60+
}
61+
SimplifyCFGOptions &needCanonicalLoops(bool B) {
62+
NeedCanonicalLoop = B;
63+
return *this;
64+
}
65+
SimplifyCFGOptions &sinkCommonInsts(bool B) {
66+
SinkCommonInsts = B;
67+
return *this;
68+
}
69+
SimplifyCFGOptions &setAssumptionCache(AssumptionCache *Cache) {
70+
AC = Cache;
71+
return *this;
72+
}
73+
SimplifyCFGOptions &setSimplifyCondBranch(bool B) {
74+
SimplifyCondBranch = B;
75+
return *this;
76+
}
77+
78+
SimplifyCFGOptions &setFoldTwoEntryPHINode(bool B) {
79+
FoldTwoEntryPHINode = B;
80+
return *this;
81+
}
82+
};
83+
84+
} // namespace llvm
85+
86+
#endif // LLVM_TRANSFORMS_SCALAR_SIMPLIFYCFGOPTIONS_H

llvm/include/llvm/Transforms/Utils/Local.h

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "llvm/IR/Value.h"
3131
#include "llvm/IR/ValueHandle.h"
3232
#include "llvm/Support/Casting.h"
33+
#include "llvm/Transforms/Scalar/SimplifyCFGOptions.h"
3334
#include <cstdint>
3435
#include <limits>
3536

@@ -58,73 +59,6 @@ class StoreInst;
5859
class TargetLibraryInfo;
5960
class TargetTransformInfo;
6061

61-
/// A set of parameters used to control the transforms in the SimplifyCFG pass.
62-
/// Options may change depending on the position in the optimization pipeline.
63-
/// For example, canonical form that includes switches and branches may later be
64-
/// replaced by lookup tables and selects.
65-
struct SimplifyCFGOptions {
66-
int BonusInstThreshold;
67-
bool ForwardSwitchCondToPhi;
68-
bool ConvertSwitchToLookupTable;
69-
bool NeedCanonicalLoop;
70-
bool SinkCommonInsts;
71-
bool SimplifyCondBranch;
72-
bool FoldTwoEntryPHINode;
73-
74-
AssumptionCache *AC;
75-
76-
SimplifyCFGOptions(unsigned BonusThreshold = 1,
77-
bool ForwardSwitchCond = false,
78-
bool SwitchToLookup = false, bool CanonicalLoops = true,
79-
bool SinkCommon = false,
80-
AssumptionCache *AssumpCache = nullptr,
81-
bool SimplifyCondBranch = true,
82-
bool FoldTwoEntryPHINode = true)
83-
: BonusInstThreshold(BonusThreshold),
84-
ForwardSwitchCondToPhi(ForwardSwitchCond),
85-
ConvertSwitchToLookupTable(SwitchToLookup),
86-
NeedCanonicalLoop(CanonicalLoops),
87-
SinkCommonInsts(SinkCommon),
88-
SimplifyCondBranch(SimplifyCondBranch),
89-
FoldTwoEntryPHINode(FoldTwoEntryPHINode),
90-
AC(AssumpCache) {}
91-
92-
// Support 'builder' pattern to set members by name at construction time.
93-
SimplifyCFGOptions &bonusInstThreshold(int I) {
94-
BonusInstThreshold = I;
95-
return *this;
96-
}
97-
SimplifyCFGOptions &forwardSwitchCondToPhi(bool B) {
98-
ForwardSwitchCondToPhi = B;
99-
return *this;
100-
}
101-
SimplifyCFGOptions &convertSwitchToLookupTable(bool B) {
102-
ConvertSwitchToLookupTable = B;
103-
return *this;
104-
}
105-
SimplifyCFGOptions &needCanonicalLoops(bool B) {
106-
NeedCanonicalLoop = B;
107-
return *this;
108-
}
109-
SimplifyCFGOptions &sinkCommonInsts(bool B) {
110-
SinkCommonInsts = B;
111-
return *this;
112-
}
113-
SimplifyCFGOptions &setAssumptionCache(AssumptionCache *Cache) {
114-
AC = Cache;
115-
return *this;
116-
}
117-
SimplifyCFGOptions &setSimplifyCondBranch(bool B) {
118-
SimplifyCondBranch = B;
119-
return *this;
120-
}
121-
122-
SimplifyCFGOptions &setFoldTwoEntryPHINode(bool B) {
123-
FoldTwoEntryPHINode = B;
124-
return *this;
125-
}
126-
};
127-
12862
//===----------------------------------------------------------------------===//
12963
// Local constant propagation.
13064
//

llvm/lib/Target/AArch64/AArch64TargetMachine.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,11 @@ void AArch64PassConfig::addIRPasses() {
453453
// determine whether it succeeded. We can exploit existing control-flow in
454454
// ldrex/strex loops to simplify this, but it needs tidying up.
455455
if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
456-
addPass(createCFGSimplificationPass(1, true, true, false, true));
456+
addPass(createCFGSimplificationPass(SimplifyCFGOptions()
457+
.forwardSwitchCondToPhi(true)
458+
.convertSwitchToLookupTable(true)
459+
.needCanonicalLoops(false)
460+
.sinkCommonInsts(true)));
457461

458462
// Run LoopDataPrefetch
459463
//

llvm/lib/Target/ARM/ARMTargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ void ARMPassConfig::addIRPasses() {
409409
// ldrex/strex loops to simplify this, but it needs tidying up.
410410
if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
411411
addPass(createCFGSimplificationPass(
412-
1, false, false, true, true, [this](const Function &F) {
412+
SimplifyCFGOptions().sinkCommonInsts(true), [this](const Function &F) {
413413
const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
414414
return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
415415
}));

llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ void HexagonPassConfig::addIRPasses() {
320320

321321
if (!NoOpt) {
322322
if (EnableInitialCFGCleanup)
323-
addPass(createCFGSimplificationPass(1, true, true, false, true));
323+
addPass(createCFGSimplificationPass(SimplifyCFGOptions()
324+
.forwardSwitchCondToPhi(true)
325+
.convertSwitchToLookupTable(true)
326+
.needCanonicalLoops(false)
327+
.sinkCommonInsts(true)));
324328
if (EnableLoopPrefetch)
325329
addPass(createLoopDataPrefetchPass());
326330
if (EnableCommGEP)

llvm/lib/Transforms/IPO/PassManagerBuilder.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,11 @@ void PassManagerBuilder::populateModulePassManager(
777777
// convert to more optimized IR using more aggressive simplify CFG options.
778778
// The extra sinking transform can create larger basic blocks, so do this
779779
// before SLP vectorization.
780-
MPM.add(createCFGSimplificationPass(1, true, true, false, true));
780+
MPM.add(createCFGSimplificationPass(SimplifyCFGOptions()
781+
.forwardSwitchCondToPhi(true)
782+
.convertSwitchToLookupTable(true)
783+
.needCanonicalLoops(false)
784+
.sinkCommonInsts(true)));
781785

782786
if (SLPVectorize) {
783787
MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.

llvm/lib/Transforms/Scalar/Scalar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void LLVMAddAlignmentFromAssumptionsPass(LLVMPassManagerRef PM) {
139139
}
140140

141141
void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM) {
142-
unwrap(PM)->add(createCFGSimplificationPass(1, false, false, true));
142+
unwrap(PM)->add(createCFGSimplificationPass());
143143
}
144144

145145
void LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM) {

llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "llvm/Support/CommandLine.h"
4040
#include "llvm/Transforms/Scalar.h"
4141
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
42+
#include "llvm/Transforms/Scalar/SimplifyCFGOptions.h"
4243
#include "llvm/Transforms/Utils/Local.h"
4344
#include <utility>
4445
using namespace llvm;
@@ -304,15 +305,7 @@ INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
304305

305306
// Public interface to the CFGSimplification pass
306307
FunctionPass *
307-
llvm::createCFGSimplificationPass(unsigned Threshold, bool ForwardSwitchCond,
308-
bool ConvertSwitch, bool KeepLoops,
309-
bool SinkCommon,
308+
llvm::createCFGSimplificationPass(SimplifyCFGOptions Options,
310309
std::function<bool(const Function &)> Ftor) {
311-
return new CFGSimplifyPass(SimplifyCFGOptions()
312-
.bonusInstThreshold(Threshold)
313-
.forwardSwitchCondToPhi(ForwardSwitchCond)
314-
.convertSwitchToLookupTable(ConvertSwitch)
315-
.needCanonicalLoops(KeepLoops)
316-
.sinkCommonInsts(SinkCommon),
317-
std::move(Ftor));
310+
return new CFGSimplifyPass(Options, std::move(Ftor));
318311
}

0 commit comments

Comments
 (0)