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