1 //===- DeltaManager.cpp - Runs Delta Passes to reduce Input ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file calls each specialized Delta pass in order to reduce the input IR
12 //===----------------------------------------------------------------------===//
14 #include "DeltaManager.h"
15 #include "ReducerWorkItem.h"
16 #include "TestRunner.h"
17 #include "deltas/Delta.h"
18 #include "deltas/ReduceAliases.h"
19 #include "deltas/ReduceArguments.h"
20 #include "deltas/ReduceAttributes.h"
21 #include "deltas/ReduceBasicBlocks.h"
22 #include "deltas/ReduceDIMetadata.h"
23 #include "deltas/ReduceDbgRecords.h"
24 #include "deltas/ReduceDistinctMetadata.h"
25 #include "deltas/ReduceFunctionBodies.h"
26 #include "deltas/ReduceFunctions.h"
27 #include "deltas/ReduceGlobalObjects.h"
28 #include "deltas/ReduceGlobalValues.h"
29 #include "deltas/ReduceGlobalVarInitializers.h"
30 #include "deltas/ReduceGlobalVars.h"
31 #include "deltas/ReduceIRReferences.h"
32 #include "deltas/ReduceInstructionFlags.h"
33 #include "deltas/ReduceInstructionFlagsMIR.h"
34 #include "deltas/ReduceInstructions.h"
35 #include "deltas/ReduceInstructionsMIR.h"
36 #include "deltas/ReduceInvokes.h"
37 #include "deltas/ReduceMemoryOperations.h"
38 #include "deltas/ReduceMetadata.h"
39 #include "deltas/ReduceModuleData.h"
40 #include "deltas/ReduceOpcodes.h"
41 #include "deltas/ReduceOperandBundles.h"
42 #include "deltas/ReduceOperands.h"
43 #include "deltas/ReduceOperandsSkip.h"
44 #include "deltas/ReduceOperandsToArgs.h"
45 #include "deltas/ReduceRegisterDefs.h"
46 #include "deltas/ReduceRegisterMasks.h"
47 #include "deltas/ReduceRegisterUses.h"
48 #include "deltas/ReduceSpecialGlobals.h"
49 #include "deltas/ReduceUsingSimplifyCFG.h"
50 #include "deltas/ReduceVirtualRegisters.h"
51 #include "deltas/RunIRPasses.h"
52 #include "deltas/SimplifyInstructions.h"
53 #include "deltas/StripDebugInfo.h"
54 #include "llvm/ADT/SmallSet.h"
55 #include "llvm/Support/CommandLine.h"
59 using SmallStringSet
= SmallSet
<StringRef
, 8>;
61 extern cl::OptionCategory LLVMReduceOptions
;
62 static cl::list
<std::string
>
63 DeltaPasses("delta-passes",
64 cl::desc("Delta passes to run, separated by commas. By "
65 "default, run all delta passes."),
66 cl::cat(LLVMReduceOptions
), cl::CommaSeparated
);
68 static cl::list
<std::string
>
69 SkipDeltaPasses("skip-delta-passes",
70 cl::desc("Delta passes to not run, separated by commas. By "
71 "default, run all delta passes."),
72 cl::cat(LLVMReduceOptions
), cl::CommaSeparated
);
74 #define DELTA_PASSES \
76 DELTA_PASS("strip-debug-info", stripDebugInfoDeltaPass) \
77 DELTA_PASS("functions", reduceFunctionsDeltaPass) \
78 DELTA_PASS("function-bodies", reduceFunctionBodiesDeltaPass) \
79 DELTA_PASS("special-globals", reduceSpecialGlobalsDeltaPass) \
80 DELTA_PASS("aliases", reduceAliasesDeltaPass) \
81 DELTA_PASS("ifuncs", reduceIFuncsDeltaPass) \
82 DELTA_PASS("simplify-conditionals-true", reduceConditionalsTrueDeltaPass) \
83 DELTA_PASS("simplify-conditionals-false", \
84 reduceConditionalsFalseDeltaPass) \
85 DELTA_PASS("invokes", reduceInvokesDeltaPass) \
86 DELTA_PASS("unreachable-basic-blocks", \
87 reduceUnreachableBasicBlocksDeltaPass) \
88 DELTA_PASS("basic-blocks", reduceBasicBlocksDeltaPass) \
89 DELTA_PASS("simplify-cfg", reduceUsingSimplifyCFGDeltaPass) \
90 DELTA_PASS("function-data", reduceFunctionDataDeltaPass) \
91 DELTA_PASS("global-values", reduceGlobalValuesDeltaPass) \
92 DELTA_PASS("global-objects", reduceGlobalObjectsDeltaPass) \
93 DELTA_PASS("global-initializers", reduceGlobalsInitializersDeltaPass) \
94 DELTA_PASS("global-variables", reduceGlobalsDeltaPass) \
95 DELTA_PASS("di-metadata", reduceDIMetadataDeltaPass) \
96 DELTA_PASS("dbg-records", reduceDbgRecordDeltaPass) \
97 DELTA_PASS("distinct-metadata", reduceDistinctMetadataDeltaPass) \
98 DELTA_PASS("metadata", reduceMetadataDeltaPass) \
99 DELTA_PASS("named-metadata", reduceNamedMetadataDeltaPass) \
100 DELTA_PASS("arguments", reduceArgumentsDeltaPass) \
101 DELTA_PASS("instructions", reduceInstructionsDeltaPass) \
102 DELTA_PASS("simplify-instructions", simplifyInstructionsDeltaPass) \
103 DELTA_PASS("ir-passes", runIRPassesDeltaPass) \
104 DELTA_PASS("operands-zero", reduceOperandsZeroDeltaPass) \
105 DELTA_PASS("operands-one", reduceOperandsOneDeltaPass) \
106 DELTA_PASS("operands-nan", reduceOperandsNaNDeltaPass) \
107 DELTA_PASS("operands-to-args", reduceOperandsToArgsDeltaPass) \
108 DELTA_PASS("operands-skip", reduceOperandsSkipDeltaPass) \
109 DELTA_PASS("operand-bundles", reduceOperandBundesDeltaPass) \
110 DELTA_PASS("attributes", reduceAttributesDeltaPass) \
111 DELTA_PASS("module-data", reduceModuleDataDeltaPass) \
112 DELTA_PASS("opcodes", reduceOpcodesDeltaPass) \
113 DELTA_PASS("volatile", reduceVolatileInstructionsDeltaPass) \
114 DELTA_PASS("atomic-ordering", reduceAtomicOrderingDeltaPass) \
115 DELTA_PASS("syncscopes", reduceAtomicSyncScopesDeltaPass) \
116 DELTA_PASS("instruction-flags", reduceInstructionFlagsDeltaPass) \
119 #define DELTA_PASSES_MIR \
121 DELTA_PASS("instructions", reduceInstructionsMIRDeltaPass) \
122 DELTA_PASS("ir-instruction-references", \
123 reduceIRInstructionReferencesDeltaPass) \
124 DELTA_PASS("ir-block-references", reduceIRBlockReferencesDeltaPass) \
125 DELTA_PASS("ir-function-references", reduceIRFunctionReferencesDeltaPass) \
126 DELTA_PASS("instruction-flags", reduceInstructionFlagsMIRDeltaPass) \
127 DELTA_PASS("register-uses", reduceRegisterUsesMIRDeltaPass) \
128 DELTA_PASS("register-defs", reduceRegisterDefsMIRDeltaPass) \
129 DELTA_PASS("register-hints", reduceVirtualRegisterHintsDeltaPass) \
130 DELTA_PASS("register-masks", reduceRegisterMasksMIRDeltaPass) \
133 static void runAllDeltaPasses(TestRunner
&Tester
,
134 const SmallStringSet
&SkipPass
) {
135 #define DELTA_PASS(NAME, FUNC) \
136 if (!SkipPass.count(NAME)) { \
139 if (Tester
.getProgram().isMIR()) {
147 static void runDeltaPassName(TestRunner
&Tester
, StringRef PassName
) {
148 #define DELTA_PASS(NAME, FUNC) \
149 if (PassName == NAME) { \
153 if (Tester
.getProgram().isMIR()) {
160 // We should have errored on unrecognized passes before trying to run
162 llvm_unreachable("unknown delta pass");
165 void llvm::printDeltaPasses(raw_ostream
&OS
) {
166 OS
<< "Delta passes (pass to `--delta-passes=` as a comma separated list):\n";
167 #define DELTA_PASS(NAME, FUNC) OS << " " << NAME << "\n";
175 // Built a set of available delta passes.
176 static void collectPassNames(const TestRunner
&Tester
,
177 SmallStringSet
&NameSet
) {
178 #define DELTA_PASS(NAME, FUNC) NameSet.insert(NAME);
179 if (Tester
.getProgram().isMIR()) {
187 /// Verify all requested or skipped passes are valid names, and return them in a
189 static SmallStringSet
handlePassList(const TestRunner
&Tester
,
190 const cl::list
<std::string
> &PassList
) {
191 SmallStringSet AllPasses
;
192 collectPassNames(Tester
, AllPasses
);
194 SmallStringSet PassSet
;
195 for (StringRef PassName
: PassList
) {
196 if (!AllPasses
.count(PassName
)) {
197 errs() << "unknown pass \"" << PassName
<< "\"\n";
201 PassSet
.insert(PassName
);
207 void llvm::runDeltaPasses(TestRunner
&Tester
, int MaxPassIterations
) {
208 uint64_t OldComplexity
= Tester
.getProgram().getComplexityScore();
210 SmallStringSet RunPassSet
, SkipPassSet
;
212 if (!DeltaPasses
.empty())
213 RunPassSet
= handlePassList(Tester
, DeltaPasses
);
215 if (!SkipDeltaPasses
.empty())
216 SkipPassSet
= handlePassList(Tester
, SkipDeltaPasses
);
218 for (int Iter
= 0; Iter
< MaxPassIterations
; ++Iter
) {
219 if (DeltaPasses
.empty()) {
220 runAllDeltaPasses(Tester
, SkipPassSet
);
222 for (StringRef PassName
: DeltaPasses
) {
223 if (!SkipPassSet
.count(PassName
))
224 runDeltaPassName(Tester
, PassName
);
228 uint64_t NewComplexity
= Tester
.getProgram().getComplexityScore();
229 if (NewComplexity
>= OldComplexity
)
231 OldComplexity
= NewComplexity
;