Skip to content

Commit cdd006e

Browse files
committed
SimplifyCFG: Clean up optforfuzzing implementation
This should function as any other SimplifyCFGOption rather than having the transform check and specially consider the attribute itself.
1 parent 27fe841 commit cdd006e

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,25 @@ struct SimplifyCFGOptions {
6666
bool ConvertSwitchToLookupTable;
6767
bool NeedCanonicalLoop;
6868
bool SinkCommonInsts;
69+
bool SimplifyCondBranch;
70+
bool FoldTwoEntryPHINode;
71+
6972
AssumptionCache *AC;
7073

7174
SimplifyCFGOptions(unsigned BonusThreshold = 1,
7275
bool ForwardSwitchCond = false,
7376
bool SwitchToLookup = false, bool CanonicalLoops = true,
7477
bool SinkCommon = false,
75-
AssumptionCache *AssumpCache = nullptr)
78+
AssumptionCache *AssumpCache = nullptr,
79+
bool SimplifyCondBranch = true,
80+
bool FoldTwoEntryPHINode = true)
7681
: BonusInstThreshold(BonusThreshold),
7782
ForwardSwitchCondToPhi(ForwardSwitchCond),
7883
ConvertSwitchToLookupTable(SwitchToLookup),
7984
NeedCanonicalLoop(CanonicalLoops),
8085
SinkCommonInsts(SinkCommon),
86+
SimplifyCondBranch(SimplifyCondBranch),
87+
FoldTwoEntryPHINode(FoldTwoEntryPHINode),
8188
AC(AssumpCache) {}
8289

8390
// Support 'builder' pattern to set members by name at construction time.
@@ -105,6 +112,15 @@ struct SimplifyCFGOptions {
105112
AC = Cache;
106113
return *this;
107114
}
115+
SimplifyCFGOptions &setSimplifyCondBranch(bool B) {
116+
SimplifyCondBranch = B;
117+
return *this;
118+
}
119+
120+
SimplifyCFGOptions &setFoldTwoEntryPHINode(bool B) {
121+
FoldTwoEntryPHINode = B;
122+
return *this;
123+
}
108124
};
109125

110126
//===----------------------------------------------------------------------===//

llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,14 @@ struct CFGSimplifyPass : public FunctionPass {
281281
return false;
282282

283283
Options.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
284+
if (F.hasFnAttribute(Attribute::OptForFuzzing)) {
285+
Options.setSimplifyCondBranch(false)
286+
.setFoldTwoEntryPHINode(false);
287+
} else {
288+
Options.setSimplifyCondBranch(true)
289+
.setFoldTwoEntryPHINode(true);
290+
}
291+
284292
auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
285293
return simplifyFunctionCFG(F, TTI, Options);
286294
}

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,9 +2336,6 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
23362336
// dependence information for this check, but simplifycfg can't keep it up
23372337
// to date, and this catches most of the cases we care about anyway.
23382338
BasicBlock *BB = PN->getParent();
2339-
const Function *Fn = BB->getParent();
2340-
if (Fn && Fn->hasFnAttribute(Attribute::OptForFuzzing))
2341-
return false;
23422339

23432340
BasicBlock *IfTrue, *IfFalse;
23442341
Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
@@ -5969,8 +5966,7 @@ static BasicBlock *allPredecessorsComeFromSameSource(BasicBlock *BB) {
59695966

59705967
bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
59715968
BasicBlock *BB = BI->getParent();
5972-
const Function *Fn = BB->getParent();
5973-
if (Fn && Fn->hasFnAttribute(Attribute::OptForFuzzing))
5969+
if (!Options.SimplifyCondBranch)
59745970
return false;
59755971

59765972
// Conditional branch
@@ -6184,11 +6180,13 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
61846180

61856181
IRBuilder<> Builder(BB);
61866182

6187-
// If there is a trivial two-entry PHI node in this basic block, and we can
6188-
// eliminate it, do so now.
6189-
if (auto *PN = dyn_cast<PHINode>(BB->begin()))
6190-
if (PN->getNumIncomingValues() == 2)
6191-
Changed |= FoldTwoEntryPHINode(PN, TTI, DL);
6183+
if (Options.FoldTwoEntryPHINode) {
6184+
// If there is a trivial two-entry PHI node in this basic block, and we can
6185+
// eliminate it, do so now.
6186+
if (auto *PN = dyn_cast<PHINode>(BB->begin()))
6187+
if (PN->getNumIncomingValues() == 2)
6188+
Changed |= FoldTwoEntryPHINode(PN, TTI, DL);
6189+
}
61926190

61936191
Instruction *Terminator = BB->getTerminator();
61946192
Builder.SetInsertPoint(Terminator);

0 commit comments

Comments
 (0)