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