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 "DeltaPass.h"
16#include "TestRunner.h"
17#include "deltas/ReduceAliases.h"
18#include "deltas/ReduceArguments.h"
19#include "deltas/ReduceAttributes.h"
20#include "deltas/ReduceBasicBlocks.h"
21#include "deltas/ReduceDIMetadata.h"
22#include "deltas/ReduceDbgRecords.h"
23#include "deltas/ReduceDistinctMetadata.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/ReduceInlineCallSites.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/ReduceSinkDefsToUses.h"
49#include "deltas/ReduceSpecialGlobals.h"
50#include "deltas/ReduceTargetFeaturesAttr.h"
51#include "deltas/ReduceUsingSimplifyCFG.h"
52#include "deltas/ReduceValuesToReturn.h"
53#include "deltas/ReduceVirtualRegisters.h"
54#include "deltas/RunIRPasses.h"
55#include "deltas/SimplifyInstructions.h"
56#include "deltas/StripDebugInfo.h"
57#include "llvm/ADT/SmallSet.h"
58#include "llvm/Support/CommandLine.h"
59
60using namespace llvm;
61
62using SmallStringSet = SmallSet<StringRef, 8>;
63
64extern cl::OptionCategory LLVMReduceOptions;
65static cl::list<std::string>
66 DeltaPasses("delta-passes",
67 cl::desc("Delta passes to run, separated by commas. By "
68 "default, run all delta passes."),
69 cl::cat(LLVMReduceOptions), cl::CommaSeparated);
70
71static cl::list<std::string>
72 SkipDeltaPasses("skip-delta-passes",
73 cl::desc("Delta passes to not run, separated by commas. By "
74 "default, run all delta passes."),
75 cl::cat(LLVMReduceOptions), cl::CommaSeparated);
76
77// Generate two separate Pass lists: IR_Passes and MIR_Passes
78static const DeltaPass IR_Passes[] = {
79#undef DELTA_PASS_IR
80#undef DELTA_PASS_MIR
81#define DELTA_PASS_IR(NAME, FUNC, DESC) {NAME, FUNC, DESC},
82#include "DeltaPasses.def"
83#undef DELTA_PASS_IR
84};
85
86static const DeltaPass MIR_Passes[] = {
87#undef DELTA_PASS_IR
88#undef DELTA_PASS_MIR
89#define DELTA_PASS_MIR(NAME, FUNC, DESC) {NAME, FUNC, DESC},
90#include "DeltaPasses.def"
91#undef DELTA_PASS_MIR
92};
93
94static void runAllDeltaPasses(TestRunner &Tester,
95 const SmallStringSet &SkipPass) {
96 if (Tester.getProgram().isMIR()) {
97 for (const DeltaPass &Pass : MIR_Passes) {
98 if (!SkipPass.count(V: Pass.Name)) {
99 runDeltaPass(Test&: Tester, Pass);
100 }
101 }
102 } else {
103 for (const DeltaPass &Pass : IR_Passes) {
104 if (!SkipPass.count(V: Pass.Name)) {
105 runDeltaPass(Test&: Tester, Pass);
106 }
107 }
108 }
109}
110
111static void runDeltaPassName(TestRunner &Tester, StringRef PassName) {
112 if (Tester.getProgram().isMIR()) {
113 for (const DeltaPass &Pass : MIR_Passes) {
114 if (PassName == Pass.Name) {
115 runDeltaPass(Test&: Tester, Pass);
116 return;
117 }
118 }
119 } else {
120 for (const DeltaPass &Pass : IR_Passes) {
121 if (PassName == Pass.Name) {
122 runDeltaPass(Test&: Tester, Pass);
123 return;
124 }
125 }
126 }
127
128 // We should have errored on unrecognized passes before trying to run
129 // anything.
130 llvm_unreachable("unknown delta pass");
131}
132
133void llvm::printDeltaPasses(raw_ostream &OS) {
134 OS << "Delta passes (pass to `--delta-passes=` as a comma separated list):\n";
135 OS << " IR:\n";
136 for (const DeltaPass &Pass : IR_Passes) {
137 OS << " " << Pass.Name << '\n';
138 }
139 OS << " MIR:\n";
140 for (const DeltaPass &Pass : MIR_Passes) {
141 OS << " " << Pass.Name << '\n';
142 }
143}
144
145// Built a set of available delta passes.
146static void collectPassNames(const TestRunner &Tester,
147 SmallStringSet &NameSet) {
148 for (const DeltaPass &Pass : MIR_Passes) {
149 NameSet.insert(V: Pass.Name);
150 }
151 for (const DeltaPass &Pass : IR_Passes) {
152 NameSet.insert(V: Pass.Name);
153 }
154}
155
156/// Verify all requested or skipped passes are valid names, and return them in a
157/// set.
158static SmallStringSet handlePassList(const TestRunner &Tester,
159 const cl::list<std::string> &PassList) {
160 SmallStringSet AllPasses;
161 collectPassNames(Tester, NameSet&: AllPasses);
162
163 SmallStringSet PassSet;
164 for (StringRef PassName : PassList) {
165 if (!AllPasses.count(V: PassName)) {
166 errs() << "unknown pass \"" << PassName << "\"\n";
167 exit(status: 1);
168 }
169
170 PassSet.insert(V: PassName);
171 }
172
173 return PassSet;
174}
175
176void llvm::runDeltaPasses(TestRunner &Tester, int MaxPassIterations) {
177 uint64_t OldComplexity = Tester.getProgram().getComplexityScore();
178
179 SmallStringSet RunPassSet, SkipPassSet;
180
181 if (!DeltaPasses.empty())
182 RunPassSet = handlePassList(Tester, PassList: DeltaPasses);
183
184 if (!SkipDeltaPasses.empty())
185 SkipPassSet = handlePassList(Tester, PassList: SkipDeltaPasses);
186
187 for (int Iter = 0; Iter < MaxPassIterations; ++Iter) {
188 if (DeltaPasses.empty()) {
189 runAllDeltaPasses(Tester, SkipPass: SkipPassSet);
190 } else {
191 for (StringRef PassName : DeltaPasses) {
192 if (!SkipPassSet.count(V: PassName))
193 runDeltaPassName(Tester, PassName);
194 }
195 }
196
197 uint64_t NewComplexity = Tester.getProgram().getComplexityScore();
198 if (NewComplexity >= OldComplexity)
199 break;
200 OldComplexity = NewComplexity;
201 }
202}
203