[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / tools / llvm-reduce / DeltaManager.cpp
blob4d646a7c861d68466144ef5ed9edfed49764f7de
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.
12 //===----------------------------------------------------------------------===//
14 #include "DeltaManager.h"
15 #include "TestRunner.h"
16 #include "deltas/Delta.h"
17 #include "deltas/ReduceAliases.h"
18 #include "deltas/ReduceArguments.h"
19 #include "deltas/ReduceAttributes.h"
20 #include "deltas/ReduceBasicBlocks.h"
21 #include "deltas/ReduceFunctionBodies.h"
22 #include "deltas/ReduceFunctions.h"
23 #include "deltas/ReduceGlobalObjects.h"
24 #include "deltas/ReduceGlobalValues.h"
25 #include "deltas/ReduceGlobalVarInitializers.h"
26 #include "deltas/ReduceGlobalVars.h"
27 #include "deltas/ReduceInstructions.h"
28 #include "deltas/ReduceInstructionsMIR.h"
29 #include "deltas/ReduceMetadata.h"
30 #include "deltas/ReduceModuleData.h"
31 #include "deltas/ReduceOperandBundles.h"
32 #include "deltas/ReduceOperands.h"
33 #include "deltas/ReduceOperandsSkip.h"
34 #include "deltas/ReduceOperandsToArgs.h"
35 #include "deltas/ReduceSpecialGlobals.h"
36 #include "llvm/Support/CommandLine.h"
38 using namespace llvm;
40 static cl::opt<std::string>
41 DeltaPasses("delta-passes",
42 cl::desc("Delta passes to run, separated by commas. By "
43 "default, run all delta passes."));
45 #define DELTA_PASSES \
46 DELTA_PASS("special-globals", reduceSpecialGlobalsDeltaPass) \
47 DELTA_PASS("aliases", reduceAliasesDeltaPass) \
48 DELTA_PASS("function-bodies", reduceFunctionBodiesDeltaPass) \
49 DELTA_PASS("functions", reduceFunctionsDeltaPass) \
50 DELTA_PASS("basic-blocks", reduceBasicBlocksDeltaPass) \
51 DELTA_PASS("global-values", reduceGlobalValuesDeltaPass) \
52 DELTA_PASS("global-objects", reduceGlobalObjectsDeltaPass) \
53 DELTA_PASS("global-initializers", reduceGlobalsInitializersDeltaPass) \
54 DELTA_PASS("global-variables", reduceGlobalsDeltaPass) \
55 DELTA_PASS("metadata", reduceMetadataDeltaPass) \
56 DELTA_PASS("arguments", reduceArgumentsDeltaPass) \
57 DELTA_PASS("instructions", reduceInstructionsDeltaPass) \
58 DELTA_PASS("operands-zero", reduceOperandsZeroDeltaPass) \
59 DELTA_PASS("operands-one", reduceOperandsOneDeltaPass) \
60 DELTA_PASS("operands-undef", reduceOperandsUndefDeltaPass) \
61 DELTA_PASS("operands-to-args", reduceOperandsToArgsDeltaPass) \
62 DELTA_PASS("operands-skip", reduceOperandsSkipDeltaPass) \
63 DELTA_PASS("operand-bundles", reduceOperandBundesDeltaPass) \
64 DELTA_PASS("attributes", reduceAttributesDeltaPass) \
65 DELTA_PASS("module-data", reduceModuleDataDeltaPass)
67 #define DELTA_PASSES_MIR \
68 DELTA_PASS("instructions", reduceInstructionsMIRDeltaPass)
70 static void runAllDeltaPasses(TestRunner &Tester) {
71 #define DELTA_PASS(NAME, FUNC) FUNC(Tester);
72 if (Tester.getProgram().isMIR()) {
73 DELTA_PASSES_MIR
74 } else {
75 DELTA_PASSES
77 #undef DELTA_PASS
80 static void runDeltaPassName(TestRunner &Tester, StringRef PassName) {
81 #define DELTA_PASS(NAME, FUNC) \
82 if (PassName == NAME) { \
83 FUNC(Tester); \
84 return; \
86 if (Tester.getProgram().isMIR()) {
87 DELTA_PASSES_MIR
88 } else {
89 DELTA_PASSES
91 #undef DELTA_PASS
92 errs() << "unknown pass \"" << PassName << "\"\n";
93 exit(1);
96 void llvm::printDeltaPasses(raw_ostream &OS) {
97 OS << "Delta passes (pass to `--delta-passes=` as a comma separated list):\n";
98 #define DELTA_PASS(NAME, FUNC) OS << " " << NAME << "\n";
99 OS << " IR:\n";
100 DELTA_PASSES
101 OS << " MIR:\n";
102 DELTA_PASSES_MIR
103 #undef DELTA_PASS
106 // FIXME: We might want to use a different metric than "number of
107 // bytes in serialized IR" to detect non-progress of the main delta
108 // loop
109 static int getIRSize(TestRunner &Tester) {
110 std::string Str;
111 raw_string_ostream SS(Str);
112 Tester.getProgram().print(SS, /*AnnotationWriter=*/nullptr);
113 return Str.length();
116 void llvm::runDeltaPasses(TestRunner &Tester, int MaxPassIterations) {
117 int OldSize = getIRSize(Tester);
118 for (int Iter = 0; Iter < MaxPassIterations; ++Iter) {
119 if (DeltaPasses.empty()) {
120 runAllDeltaPasses(Tester);
121 } else {
122 StringRef Passes = DeltaPasses;
123 while (!Passes.empty()) {
124 auto Split = Passes.split(",");
125 runDeltaPassName(Tester, Split.first);
126 Passes = Split.second;
129 int NewSize = getIRSize(Tester);
130 if (NewSize >= OldSize)
131 break;
132 OldSize = NewSize;