xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Passes/StandardInstrumentations.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 //===- Standard pass instrumentations handling ----------------*- C++ -*--===//
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 /// \file
9 ///
10 /// This file defines IR-printing pass instrumentation callbacks as well as
11 /// StandardInstrumentations class that manages standard pass instrumentations.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Passes/StandardInstrumentations.h"
16 #include "llvm/ADT/Any.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Analysis/CallGraphSCCPass.h"
20 #include "llvm/Analysis/LazyCallGraph.h"
21 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/LegacyPassManager.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/PassInstrumentation.h"
26 #include "llvm/IR/PassManager.h"
27 #include "llvm/IR/PrintPasses.h"
28 #include "llvm/IR/Verifier.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/FormatVariadic.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/Program.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <unordered_set>
36 #include <vector>
37 
38 using namespace llvm;
39 
40 cl::opt<bool> PreservedCFGCheckerInstrumentation::VerifyPreservedCFG(
41     "verify-cfg-preserved", cl::Hidden,
42 #ifdef NDEBUG
43     cl::init(false));
44 #else
45     cl::init(true));
46 #endif
47 
48 // An option that prints out the IR after passes, similar to
49 // -print-after-all except that it only prints the IR after passes that
50 // change the IR.  Those passes that do not make changes to the IR are
51 // reported as not making any changes.  In addition, the initial IR is
52 // also reported.  Other hidden options affect the output from this
53 // option.  -filter-passes will limit the output to the named passes
54 // that actually change the IR and other passes are reported as filtered out.
55 // The specified passes will either be reported as making no changes (with
56 // no IR reported) or the changed IR will be reported.  Also, the
57 // -filter-print-funcs and -print-module-scope options will do similar
58 // filtering based on function name, reporting changed IRs as functions(or
59 // modules if -print-module-scope is specified) for a particular function
60 // or indicating that the IR has been filtered out.  The extra options
61 // can be combined, allowing only changed IRs for certain passes on certain
62 // functions to be reported in different formats, with the rest being
63 // reported as filtered out.  The -print-before-changed option will print
64 // the IR as it was before each pass that changed it.  The optional
65 // value of quiet will only report when the IR changes, suppressing
66 // all other messages, including the initial IR.  The values "diff" and
67 // "diff-quiet" will present the changes in a form similar to a patch, in
68 // either verbose or quiet mode, respectively.  The lines that are removed
69 // and added are prefixed with '-' and '+', respectively.  The
70 // -filter-print-funcs and -filter-passes can be used to filter the output.
71 // This reporter relies on the linux diff utility to do comparisons and
72 // insert the prefixes.  For systems that do not have the necessary
73 // facilities, the error message will be shown in place of the expected output.
74 //
75 enum class ChangePrinter {
76   NoChangePrinter,
77   PrintChangedVerbose,
78   PrintChangedQuiet,
79   PrintChangedDiffVerbose,
80   PrintChangedDiffQuiet,
81   PrintChangedColourDiffVerbose,
82   PrintChangedColourDiffQuiet
83 };
84 static cl::opt<ChangePrinter> PrintChanged(
85     "print-changed", cl::desc("Print changed IRs"), cl::Hidden,
86     cl::ValueOptional, cl::init(ChangePrinter::NoChangePrinter),
87     cl::values(
88         clEnumValN(ChangePrinter::PrintChangedQuiet, "quiet",
89                    "Run in quiet mode"),
90         clEnumValN(ChangePrinter::PrintChangedDiffVerbose, "diff",
91                    "Display patch-like changes"),
92         clEnumValN(ChangePrinter::PrintChangedDiffQuiet, "diff-quiet",
93                    "Display patch-like changes in quiet mode"),
94         clEnumValN(ChangePrinter::PrintChangedColourDiffVerbose, "cdiff",
95                    "Display patch-like changes with color"),
96         clEnumValN(ChangePrinter::PrintChangedColourDiffQuiet, "cdiff-quiet",
97                    "Display patch-like changes in quiet mode with color"),
98         // Sentinel value for unspecified option.
99         clEnumValN(ChangePrinter::PrintChangedVerbose, "", "")));
100 
101 // An option that supports the -print-changed option.  See
102 // the description for -print-changed for an explanation of the use
103 // of this option.  Note that this option has no effect without -print-changed.
104 static cl::list<std::string>
105     PrintPassesList("filter-passes", cl::value_desc("pass names"),
106                     cl::desc("Only consider IR changes for passes whose names "
107                              "match for the print-changed option"),
108                     cl::CommaSeparated, cl::Hidden);
109 // An option that supports the -print-changed option.  See
110 // the description for -print-changed for an explanation of the use
111 // of this option.  Note that this option has no effect without -print-changed.
112 static cl::opt<bool>
113     PrintChangedBefore("print-before-changed",
114                        cl::desc("Print before passes that change them"),
115                        cl::init(false), cl::Hidden);
116 
117 // An option for specifying the diff used by print-changed=[diff | diff-quiet]
118 static cl::opt<std::string>
119     DiffBinary("print-changed-diff-path", cl::Hidden, cl::init("diff"),
120                cl::desc("system diff used by change reporters"));
121 
122 namespace {
123 
124 // Perform a system based diff between \p Before and \p After, using
125 // \p OldLineFormat, \p NewLineFormat, and \p UnchangedLineFormat
126 // to control the formatting of the output.  Return an error message
127 // for any failures instead of the diff.
doSystemDiff(StringRef Before,StringRef After,StringRef OldLineFormat,StringRef NewLineFormat,StringRef UnchangedLineFormat)128 std::string doSystemDiff(StringRef Before, StringRef After,
129                          StringRef OldLineFormat, StringRef NewLineFormat,
130                          StringRef UnchangedLineFormat) {
131   StringRef SR[2]{Before, After};
132   // Store the 2 bodies into temporary files and call diff on them
133   // to get the body of the node.
134   const unsigned NumFiles = 3;
135   std::string FileName[NumFiles];
136   int FD[NumFiles]{-1, -1, -1};
137   for (unsigned I = 0; I < NumFiles; ++I) {
138     if (FD[I] == -1) {
139       SmallVector<char, 200> SV;
140       std::error_code EC =
141           sys::fs::createTemporaryFile("tmpdiff", "txt", FD[I], SV);
142       if (EC)
143         return "Unable to create temporary file.";
144       FileName[I] = Twine(SV).str();
145     }
146     // The third file is used as the result of the diff.
147     if (I == NumFiles - 1)
148       break;
149 
150     std::error_code EC = sys::fs::openFileForWrite(FileName[I], FD[I]);
151     if (EC)
152       return "Unable to open temporary file for writing.";
153 
154     raw_fd_ostream OutStream(FD[I], /*shouldClose=*/true);
155     if (FD[I] == -1)
156       return "Error opening file for writing.";
157     OutStream << SR[I];
158   }
159 
160   static ErrorOr<std::string> DiffExe = sys::findProgramByName(DiffBinary);
161   if (!DiffExe)
162     return "Unable to find diff executable.";
163 
164   SmallString<128> OLF = formatv("--old-line-format={0}", OldLineFormat);
165   SmallString<128> NLF = formatv("--new-line-format={0}", NewLineFormat);
166   SmallString<128> ULF =
167       formatv("--unchanged-line-format={0}", UnchangedLineFormat);
168 
169   StringRef Args[] = {"-w", "-d", OLF, NLF, ULF, FileName[0], FileName[1]};
170   Optional<StringRef> Redirects[] = {None, StringRef(FileName[2]), None};
171   int Result = sys::ExecuteAndWait(*DiffExe, Args, None, Redirects);
172   if (Result < 0)
173     return "Error executing system diff.";
174   std::string Diff;
175   auto B = MemoryBuffer::getFile(FileName[2]);
176   if (B && *B)
177     Diff = (*B)->getBuffer().str();
178   else
179     return "Unable to read result.";
180 
181   // Clean up.
182   for (unsigned I = 0; I < NumFiles; ++I) {
183     std::error_code EC = sys::fs::remove(FileName[I]);
184     if (EC)
185       return "Unable to remove temporary file.";
186   }
187   return Diff;
188 }
189 
190 /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match
191 /// certain global filters. Will never return nullptr if \p Force is true.
unwrapModule(Any IR,bool Force=false)192 const Module *unwrapModule(Any IR, bool Force = false) {
193   if (any_isa<const Module *>(IR))
194     return any_cast<const Module *>(IR);
195 
196   if (any_isa<const Function *>(IR)) {
197     const Function *F = any_cast<const Function *>(IR);
198     if (!Force && !isFunctionInPrintList(F->getName()))
199       return nullptr;
200 
201     return F->getParent();
202   }
203 
204   if (any_isa<const LazyCallGraph::SCC *>(IR)) {
205     const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
206     for (const LazyCallGraph::Node &N : *C) {
207       const Function &F = N.getFunction();
208       if (Force || (!F.isDeclaration() && isFunctionInPrintList(F.getName()))) {
209         return F.getParent();
210       }
211     }
212     assert(!Force && "Expected a module");
213     return nullptr;
214   }
215 
216   if (any_isa<const Loop *>(IR)) {
217     const Loop *L = any_cast<const Loop *>(IR);
218     const Function *F = L->getHeader()->getParent();
219     if (!Force && !isFunctionInPrintList(F->getName()))
220       return nullptr;
221     return F->getParent();
222   }
223 
224   llvm_unreachable("Unknown IR unit");
225 }
226 
printIR(raw_ostream & OS,const Function * F)227 void printIR(raw_ostream &OS, const Function *F) {
228   if (!isFunctionInPrintList(F->getName()))
229     return;
230   OS << *F;
231 }
232 
printIR(raw_ostream & OS,const Module * M,bool ShouldPreserveUseListOrder=false)233 void printIR(raw_ostream &OS, const Module *M,
234              bool ShouldPreserveUseListOrder = false) {
235   if (isFunctionInPrintList("*") || forcePrintModuleIR()) {
236     M->print(OS, nullptr, ShouldPreserveUseListOrder);
237   } else {
238     for (const auto &F : M->functions()) {
239       printIR(OS, &F);
240     }
241   }
242 }
243 
printIR(raw_ostream & OS,const LazyCallGraph::SCC * C)244 void printIR(raw_ostream &OS, const LazyCallGraph::SCC *C) {
245   for (const LazyCallGraph::Node &N : *C) {
246     const Function &F = N.getFunction();
247     if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) {
248       F.print(OS);
249     }
250   }
251 }
252 
printIR(raw_ostream & OS,const Loop * L)253 void printIR(raw_ostream &OS, const Loop *L) {
254   const Function *F = L->getHeader()->getParent();
255   if (!isFunctionInPrintList(F->getName()))
256     return;
257   printLoop(const_cast<Loop &>(*L), OS);
258 }
259 
getIRName(Any IR)260 std::string getIRName(Any IR) {
261   if (any_isa<const Module *>(IR))
262     return "[module]";
263 
264   if (any_isa<const Function *>(IR)) {
265     const Function *F = any_cast<const Function *>(IR);
266     return F->getName().str();
267   }
268 
269   if (any_isa<const LazyCallGraph::SCC *>(IR)) {
270     const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
271     return C->getName();
272   }
273 
274   if (any_isa<const Loop *>(IR)) {
275     const Loop *L = any_cast<const Loop *>(IR);
276     std::string S;
277     raw_string_ostream OS(S);
278     L->print(OS, /*Verbose*/ false, /*PrintNested*/ false);
279     return OS.str();
280   }
281 
282   llvm_unreachable("Unknown wrapped IR type");
283 }
284 
moduleContainsFilterPrintFunc(const Module & M)285 bool moduleContainsFilterPrintFunc(const Module &M) {
286   return any_of(M.functions(),
287                 [](const Function &F) {
288                   return isFunctionInPrintList(F.getName());
289                 }) ||
290          isFunctionInPrintList("*");
291 }
292 
sccContainsFilterPrintFunc(const LazyCallGraph::SCC & C)293 bool sccContainsFilterPrintFunc(const LazyCallGraph::SCC &C) {
294   return any_of(C,
295                 [](const LazyCallGraph::Node &N) {
296                   return isFunctionInPrintList(N.getName());
297                 }) ||
298          isFunctionInPrintList("*");
299 }
300 
shouldPrintIR(Any IR)301 bool shouldPrintIR(Any IR) {
302   if (any_isa<const Module *>(IR)) {
303     const Module *M = any_cast<const Module *>(IR);
304     return moduleContainsFilterPrintFunc(*M);
305   }
306 
307   if (any_isa<const Function *>(IR)) {
308     const Function *F = any_cast<const Function *>(IR);
309     return isFunctionInPrintList(F->getName());
310   }
311 
312   if (any_isa<const LazyCallGraph::SCC *>(IR)) {
313     const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
314     return sccContainsFilterPrintFunc(*C);
315   }
316 
317   if (any_isa<const Loop *>(IR)) {
318     const Loop *L = any_cast<const Loop *>(IR);
319     return isFunctionInPrintList(L->getHeader()->getParent()->getName());
320   }
321   llvm_unreachable("Unknown wrapped IR type");
322 }
323 
324 /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
325 /// llvm::Any and does actual print job.
unwrapAndPrint(raw_ostream & OS,Any IR,bool ShouldPreserveUseListOrder=false)326 void unwrapAndPrint(raw_ostream &OS, Any IR,
327                     bool ShouldPreserveUseListOrder = false) {
328   if (!shouldPrintIR(IR))
329     return;
330 
331   if (forcePrintModuleIR()) {
332     auto *M = unwrapModule(IR);
333     assert(M && "should have unwrapped module");
334     printIR(OS, M, ShouldPreserveUseListOrder);
335     return;
336   }
337 
338   if (any_isa<const Module *>(IR)) {
339     const Module *M = any_cast<const Module *>(IR);
340     printIR(OS, M, ShouldPreserveUseListOrder);
341     return;
342   }
343 
344   if (any_isa<const Function *>(IR)) {
345     const Function *F = any_cast<const Function *>(IR);
346     printIR(OS, F);
347     return;
348   }
349 
350   if (any_isa<const LazyCallGraph::SCC *>(IR)) {
351     const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
352     printIR(OS, C);
353     return;
354   }
355 
356   if (any_isa<const Loop *>(IR)) {
357     const Loop *L = any_cast<const Loop *>(IR);
358     printIR(OS, L);
359     return;
360   }
361   llvm_unreachable("Unknown wrapped IR type");
362 }
363 
364 // Return true when this is a pass for which changes should be ignored
isIgnored(StringRef PassID)365 bool isIgnored(StringRef PassID) {
366   return isSpecialPass(PassID,
367                        {"PassManager", "PassAdaptor", "AnalysisManagerProxy",
368                         "DevirtSCCRepeatedPass", "ModuleInlinerWrapperPass"});
369 }
370 
371 } // namespace
372 
373 template <typename IRUnitT>
~ChangeReporter()374 ChangeReporter<IRUnitT>::~ChangeReporter<IRUnitT>() {
375   assert(BeforeStack.empty() && "Problem with Change Printer stack.");
376 }
377 
378 template <typename IRUnitT>
isInterestingFunction(const Function & F)379 bool ChangeReporter<IRUnitT>::isInterestingFunction(const Function &F) {
380   return isFunctionInPrintList(F.getName());
381 }
382 
383 template <typename IRUnitT>
isInterestingPass(StringRef PassID)384 bool ChangeReporter<IRUnitT>::isInterestingPass(StringRef PassID) {
385   if (isIgnored(PassID))
386     return false;
387 
388   static std::unordered_set<std::string> PrintPassNames(PrintPassesList.begin(),
389                                                         PrintPassesList.end());
390   return PrintPassNames.empty() || PrintPassNames.count(PassID.str());
391 }
392 
393 // Return true when this is a pass on IR for which printing
394 // of changes is desired.
395 template <typename IRUnitT>
isInteresting(Any IR,StringRef PassID)396 bool ChangeReporter<IRUnitT>::isInteresting(Any IR, StringRef PassID) {
397   if (!isInterestingPass(PassID))
398     return false;
399   if (any_isa<const Function *>(IR))
400     return isInterestingFunction(*any_cast<const Function *>(IR));
401   return true;
402 }
403 
404 template <typename IRUnitT>
saveIRBeforePass(Any IR,StringRef PassID)405 void ChangeReporter<IRUnitT>::saveIRBeforePass(Any IR, StringRef PassID) {
406   // Always need to place something on the stack because invalidated passes
407   // are not given the IR so it cannot be determined whether the pass was for
408   // something that was filtered out.
409   BeforeStack.emplace_back();
410 
411   if (!isInteresting(IR, PassID))
412     return;
413   // Is this the initial IR?
414   if (InitialIR) {
415     InitialIR = false;
416     if (VerboseMode)
417       handleInitialIR(IR);
418   }
419 
420   // Save the IR representation on the stack.
421   IRUnitT &Data = BeforeStack.back();
422   generateIRRepresentation(IR, PassID, Data);
423 }
424 
425 template <typename IRUnitT>
handleIRAfterPass(Any IR,StringRef PassID)426 void ChangeReporter<IRUnitT>::handleIRAfterPass(Any IR, StringRef PassID) {
427   assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");
428 
429   std::string Name = getIRName(IR);
430 
431   if (isIgnored(PassID)) {
432     if (VerboseMode)
433       handleIgnored(PassID, Name);
434   } else if (!isInteresting(IR, PassID)) {
435     if (VerboseMode)
436       handleFiltered(PassID, Name);
437   } else {
438     // Get the before rep from the stack
439     IRUnitT &Before = BeforeStack.back();
440     // Create the after rep
441     IRUnitT After;
442     generateIRRepresentation(IR, PassID, After);
443 
444     // Was there a change in IR?
445     if (same(Before, After)) {
446       if (VerboseMode)
447         omitAfter(PassID, Name);
448     } else
449       handleAfter(PassID, Name, Before, After, IR);
450   }
451   BeforeStack.pop_back();
452 }
453 
454 template <typename IRUnitT>
handleInvalidatedPass(StringRef PassID)455 void ChangeReporter<IRUnitT>::handleInvalidatedPass(StringRef PassID) {
456   assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");
457 
458   // Always flag it as invalidated as we cannot determine when
459   // a pass for a filtered function is invalidated since we do not
460   // get the IR in the call.  Also, the output is just alternate
461   // forms of the banner anyway.
462   if (VerboseMode)
463     handleInvalidated(PassID);
464   BeforeStack.pop_back();
465 }
466 
467 template <typename IRUnitT>
registerRequiredCallbacks(PassInstrumentationCallbacks & PIC)468 void ChangeReporter<IRUnitT>::registerRequiredCallbacks(
469     PassInstrumentationCallbacks &PIC) {
470   PIC.registerBeforeNonSkippedPassCallback(
471       [this](StringRef P, Any IR) { saveIRBeforePass(IR, P); });
472 
473   PIC.registerAfterPassCallback(
474       [this](StringRef P, Any IR, const PreservedAnalyses &) {
475         handleIRAfterPass(IR, P);
476       });
477   PIC.registerAfterPassInvalidatedCallback(
478       [this](StringRef P, const PreservedAnalyses &) {
479         handleInvalidatedPass(P);
480       });
481 }
482 
ChangedBlockData(const BasicBlock & B)483 ChangedBlockData::ChangedBlockData(const BasicBlock &B)
484     : Label(B.getName().str()) {
485   raw_string_ostream SS(Body);
486   B.print(SS, nullptr, true, true);
487 }
488 
489 template <typename IRUnitT>
TextChangeReporter(bool Verbose)490 TextChangeReporter<IRUnitT>::TextChangeReporter(bool Verbose)
491     : ChangeReporter<IRUnitT>(Verbose), Out(dbgs()) {}
492 
493 template <typename IRUnitT>
handleInitialIR(Any IR)494 void TextChangeReporter<IRUnitT>::handleInitialIR(Any IR) {
495   // Always print the module.
496   // Unwrap and print directly to avoid filtering problems in general routines.
497   auto *M = unwrapModule(IR, /*Force=*/true);
498   assert(M && "Expected module to be unwrapped when forced.");
499   Out << "*** IR Dump At Start ***\n";
500   M->print(Out, nullptr,
501            /*ShouldPreserveUseListOrder=*/true);
502 }
503 
504 template <typename IRUnitT>
omitAfter(StringRef PassID,std::string & Name)505 void TextChangeReporter<IRUnitT>::omitAfter(StringRef PassID,
506                                             std::string &Name) {
507   Out << formatv("*** IR Dump After {0} on {1} omitted because no change ***\n",
508                  PassID, Name);
509 }
510 
511 template <typename IRUnitT>
handleInvalidated(StringRef PassID)512 void TextChangeReporter<IRUnitT>::handleInvalidated(StringRef PassID) {
513   Out << formatv("*** IR Pass {0} invalidated ***\n", PassID);
514 }
515 
516 template <typename IRUnitT>
handleFiltered(StringRef PassID,std::string & Name)517 void TextChangeReporter<IRUnitT>::handleFiltered(StringRef PassID,
518                                                  std::string &Name) {
519   SmallString<20> Banner =
520       formatv("*** IR Dump After {0} on {1} filtered out ***\n", PassID, Name);
521   Out << Banner;
522 }
523 
524 template <typename IRUnitT>
handleIgnored(StringRef PassID,std::string & Name)525 void TextChangeReporter<IRUnitT>::handleIgnored(StringRef PassID,
526                                                 std::string &Name) {
527   Out << formatv("*** IR Pass {0} on {1} ignored ***\n", PassID, Name);
528 }
529 
~IRChangedPrinter()530 IRChangedPrinter::~IRChangedPrinter() {}
531 
registerCallbacks(PassInstrumentationCallbacks & PIC)532 void IRChangedPrinter::registerCallbacks(PassInstrumentationCallbacks &PIC) {
533   if (PrintChanged == ChangePrinter::PrintChangedVerbose ||
534       PrintChanged == ChangePrinter::PrintChangedQuiet)
535     TextChangeReporter<std::string>::registerRequiredCallbacks(PIC);
536 }
537 
generateIRRepresentation(Any IR,StringRef PassID,std::string & Output)538 void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID,
539                                                 std::string &Output) {
540   raw_string_ostream OS(Output);
541   unwrapAndPrint(OS, IR,
542                  /*ShouldPreserveUseListOrder=*/true);
543   OS.str();
544 }
545 
handleAfter(StringRef PassID,std::string & Name,const std::string & Before,const std::string & After,Any)546 void IRChangedPrinter::handleAfter(StringRef PassID, std::string &Name,
547                                    const std::string &Before,
548                                    const std::string &After, Any) {
549   // Report the IR before the changes when requested.
550   if (PrintChangedBefore)
551     Out << "*** IR Dump Before " << PassID << " on " << Name << " ***\n"
552         << Before;
553 
554   // We might not get anything to print if we only want to print a specific
555   // function but it gets deleted.
556   if (After.empty()) {
557     Out << "*** IR Deleted After " << PassID << " on " << Name << " ***\n";
558     return;
559   }
560 
561   Out << "*** IR Dump After " << PassID << " on " << Name << " ***\n" << After;
562 }
563 
same(const std::string & S1,const std::string & S2)564 bool IRChangedPrinter::same(const std::string &S1, const std::string &S2) {
565   return S1 == S2;
566 }
567 
568 template <typename IRData>
report(const OrderedChangedData & Before,const OrderedChangedData & After,function_ref<void (const IRData *,const IRData *)> HandlePair)569 void OrderedChangedData<IRData>::report(
570     const OrderedChangedData &Before, const OrderedChangedData &After,
571     function_ref<void(const IRData *, const IRData *)> HandlePair) {
572   const auto &BFD = Before.getData();
573   const auto &AFD = After.getData();
574   std::vector<std::string>::const_iterator BI = Before.getOrder().begin();
575   std::vector<std::string>::const_iterator BE = Before.getOrder().end();
576   std::vector<std::string>::const_iterator AI = After.getOrder().begin();
577   std::vector<std::string>::const_iterator AE = After.getOrder().end();
578 
579   auto handlePotentiallyRemovedIRData = [&](std::string S) {
580     // The order in LLVM may have changed so check if still exists.
581     if (!AFD.count(S)) {
582       // This has been removed.
583       HandlePair(&BFD.find(*BI)->getValue(), nullptr);
584     }
585   };
586   auto handleNewIRData = [&](std::vector<const IRData *> &Q) {
587     // Print out any queued up new sections
588     for (const IRData *NBI : Q)
589       HandlePair(nullptr, NBI);
590     Q.clear();
591   };
592 
593   // Print out the IRData in the after order, with before ones interspersed
594   // appropriately (ie, somewhere near where they were in the before list).
595   // Start at the beginning of both lists.  Loop through the
596   // after list.  If an element is common, then advance in the before list
597   // reporting the removed ones until the common one is reached.  Report any
598   // queued up new ones and then report the common one.  If an element is not
599   // common, then enqueue it for reporting.  When the after list is exhausted,
600   // loop through the before list, reporting any removed ones.  Finally,
601   // report the rest of the enqueued new ones.
602   std::vector<const IRData *> NewIRDataQueue;
603   while (AI != AE) {
604     if (!BFD.count(*AI)) {
605       // This section is new so place it in the queue.  This will cause it
606       // to be reported after deleted sections.
607       NewIRDataQueue.emplace_back(&AFD.find(*AI)->getValue());
608       ++AI;
609       continue;
610     }
611     // This section is in both; advance and print out any before-only
612     // until we get to it.
613     while (*BI != *AI) {
614       handlePotentiallyRemovedIRData(*BI);
615       ++BI;
616     }
617     // Report any new sections that were queued up and waiting.
618     handleNewIRData(NewIRDataQueue);
619 
620     const IRData &AData = AFD.find(*AI)->getValue();
621     const IRData &BData = BFD.find(*AI)->getValue();
622     HandlePair(&BData, &AData);
623     ++BI;
624     ++AI;
625   }
626 
627   // Check any remaining before sections to see if they have been removed
628   while (BI != BE) {
629     handlePotentiallyRemovedIRData(*BI);
630     ++BI;
631   }
632 
633   handleNewIRData(NewIRDataQueue);
634 }
635 
compare(Any IR,StringRef Prefix,StringRef PassID,StringRef Name)636 void ChangedIRComparer::compare(Any IR, StringRef Prefix, StringRef PassID,
637                                 StringRef Name) {
638   if (!getModuleForComparison(IR)) {
639     // Not a module so just handle the single function.
640     assert(Before.getData().size() == 1 && "Expected only one function.");
641     assert(After.getData().size() == 1 && "Expected only one function.");
642     handleFunctionCompare(Name, Prefix, PassID, false,
643                           Before.getData().begin()->getValue(),
644                           After.getData().begin()->getValue());
645     return;
646   }
647 
648   ChangedIRData::report(
649       Before, After, [&](const ChangedFuncData *B, const ChangedFuncData *A) {
650         ChangedFuncData Missing;
651         if (!B)
652           B = &Missing;
653         else if (!A)
654           A = &Missing;
655         assert(B != &Missing && A != &Missing &&
656                "Both functions cannot be missing.");
657         handleFunctionCompare(Name, Prefix, PassID, true, *B, *A);
658       });
659 }
660 
analyzeIR(Any IR,ChangedIRData & Data)661 void ChangedIRComparer::analyzeIR(Any IR, ChangedIRData &Data) {
662   if (const Module *M = getModuleForComparison(IR)) {
663     // Create data for each existing/interesting function in the module.
664     for (const Function &F : *M)
665       generateFunctionData(Data, F);
666     return;
667   }
668 
669   const Function *F = nullptr;
670   if (any_isa<const Function *>(IR))
671     F = any_cast<const Function *>(IR);
672   else {
673     assert(any_isa<const Loop *>(IR) && "Unknown IR unit.");
674     const Loop *L = any_cast<const Loop *>(IR);
675     F = L->getHeader()->getParent();
676   }
677   assert(F && "Unknown IR unit.");
678   generateFunctionData(Data, *F);
679 }
680 
getModuleForComparison(Any IR)681 const Module *ChangedIRComparer::getModuleForComparison(Any IR) {
682   if (any_isa<const Module *>(IR))
683     return any_cast<const Module *>(IR);
684   if (any_isa<const LazyCallGraph::SCC *>(IR))
685     return any_cast<const LazyCallGraph::SCC *>(IR)
686         ->begin()
687         ->getFunction()
688         .getParent();
689   return nullptr;
690 }
691 
generateFunctionData(ChangedIRData & Data,const Function & F)692 bool ChangedIRComparer::generateFunctionData(ChangedIRData &Data,
693                                              const Function &F) {
694   if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) {
695     ChangedFuncData CFD;
696     for (const auto &B : F) {
697       CFD.getOrder().emplace_back(B.getName());
698       CFD.getData().insert({B.getName(), B});
699     }
700     Data.getOrder().emplace_back(F.getName());
701     Data.getData().insert({F.getName(), CFD});
702     return true;
703   }
704   return false;
705 }
706 
~PrintIRInstrumentation()707 PrintIRInstrumentation::~PrintIRInstrumentation() {
708   assert(ModuleDescStack.empty() && "ModuleDescStack is not empty at exit");
709 }
710 
pushModuleDesc(StringRef PassID,Any IR)711 void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, Any IR) {
712   assert(StoreModuleDesc);
713   const Module *M = unwrapModule(IR);
714   ModuleDescStack.emplace_back(M, getIRName(IR), PassID);
715 }
716 
717 PrintIRInstrumentation::PrintModuleDesc
popModuleDesc(StringRef PassID)718 PrintIRInstrumentation::popModuleDesc(StringRef PassID) {
719   assert(!ModuleDescStack.empty() && "empty ModuleDescStack");
720   PrintModuleDesc ModuleDesc = ModuleDescStack.pop_back_val();
721   assert(std::get<2>(ModuleDesc).equals(PassID) && "malformed ModuleDescStack");
722   return ModuleDesc;
723 }
724 
printBeforePass(StringRef PassID,Any IR)725 void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
726   if (isIgnored(PassID))
727     return;
728 
729   // Saving Module for AfterPassInvalidated operations.
730   // Note: here we rely on a fact that we do not change modules while
731   // traversing the pipeline, so the latest captured module is good
732   // for all print operations that has not happen yet.
733   if (StoreModuleDesc && shouldPrintAfterPass(PassID))
734     pushModuleDesc(PassID, IR);
735 
736   if (!shouldPrintBeforePass(PassID))
737     return;
738 
739   if (!shouldPrintIR(IR))
740     return;
741 
742   dbgs() << "*** IR Dump Before " << PassID << " on " << getIRName(IR)
743          << " ***\n";
744   unwrapAndPrint(dbgs(), IR);
745 }
746 
printAfterPass(StringRef PassID,Any IR)747 void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
748   if (isIgnored(PassID))
749     return;
750 
751   if (!shouldPrintAfterPass(PassID))
752     return;
753 
754   if (StoreModuleDesc) {
755     const Module *M;
756     std::string IRName;
757     StringRef StoredPassID;
758     std::tie(M, IRName, StoredPassID) = popModuleDesc(PassID);
759     assert(StoredPassID == PassID && "mismatched PassID");
760   }
761 
762   if (!shouldPrintIR(IR))
763     return;
764 
765   dbgs() << "*** IR Dump After " << PassID << " on " << getIRName(IR)
766          << " ***\n";
767   unwrapAndPrint(dbgs(), IR);
768 }
769 
printAfterPassInvalidated(StringRef PassID)770 void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
771   StringRef PassName = PIC->getPassNameForClassName(PassID);
772   if (!StoreModuleDesc || !shouldPrintAfterPass(PassName))
773     return;
774 
775   if (isIgnored(PassID))
776     return;
777 
778   const Module *M;
779   std::string IRName;
780   StringRef StoredPassID;
781   std::tie(M, IRName, StoredPassID) = popModuleDesc(PassID);
782   assert(StoredPassID == PassID && "mismatched PassID");
783   // Additional filtering (e.g. -filter-print-func) can lead to module
784   // printing being skipped.
785   if (!M)
786     return;
787 
788   SmallString<20> Banner =
789       formatv("*** IR Dump After {0} on {1} (invalidated) ***", PassID, IRName);
790   dbgs() << Banner << "\n";
791   printIR(dbgs(), M);
792 }
793 
shouldPrintBeforePass(StringRef PassID)794 bool PrintIRInstrumentation::shouldPrintBeforePass(StringRef PassID) {
795   if (shouldPrintBeforeAll())
796     return true;
797 
798   StringRef PassName = PIC->getPassNameForClassName(PassID);
799   return llvm::is_contained(printBeforePasses(), PassName);
800 }
801 
shouldPrintAfterPass(StringRef PassID)802 bool PrintIRInstrumentation::shouldPrintAfterPass(StringRef PassID) {
803   if (shouldPrintAfterAll())
804     return true;
805 
806   StringRef PassName = PIC->getPassNameForClassName(PassID);
807   return llvm::is_contained(printAfterPasses(), PassName);
808 }
809 
registerCallbacks(PassInstrumentationCallbacks & PIC)810 void PrintIRInstrumentation::registerCallbacks(
811     PassInstrumentationCallbacks &PIC) {
812   this->PIC = &PIC;
813 
814   // BeforePass callback is not just for printing, it also saves a Module
815   // for later use in AfterPassInvalidated.
816   StoreModuleDesc = forcePrintModuleIR() && shouldPrintAfterSomePass();
817   if (shouldPrintBeforeSomePass() || StoreModuleDesc)
818     PIC.registerBeforeNonSkippedPassCallback(
819         [this](StringRef P, Any IR) { this->printBeforePass(P, IR); });
820 
821   if (shouldPrintAfterSomePass()) {
822     PIC.registerAfterPassCallback(
823         [this](StringRef P, Any IR, const PreservedAnalyses &) {
824           this->printAfterPass(P, IR);
825         });
826     PIC.registerAfterPassInvalidatedCallback(
827         [this](StringRef P, const PreservedAnalyses &) {
828           this->printAfterPassInvalidated(P);
829         });
830   }
831 }
832 
registerCallbacks(PassInstrumentationCallbacks & PIC)833 void OptNoneInstrumentation::registerCallbacks(
834     PassInstrumentationCallbacks &PIC) {
835   PIC.registerShouldRunOptionalPassCallback(
836       [this](StringRef P, Any IR) { return this->shouldRun(P, IR); });
837 }
838 
shouldRun(StringRef PassID,Any IR)839 bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) {
840   const Function *F = nullptr;
841   if (any_isa<const Function *>(IR)) {
842     F = any_cast<const Function *>(IR);
843   } else if (any_isa<const Loop *>(IR)) {
844     F = any_cast<const Loop *>(IR)->getHeader()->getParent();
845   }
846   bool ShouldRun = !(F && F->hasOptNone());
847   if (!ShouldRun && DebugLogging) {
848     errs() << "Skipping pass " << PassID << " on " << F->getName()
849            << " due to optnone attribute\n";
850   }
851   return ShouldRun;
852 }
853 
registerCallbacks(PassInstrumentationCallbacks & PIC)854 void OptBisectInstrumentation::registerCallbacks(
855     PassInstrumentationCallbacks &PIC) {
856   if (!OptBisector->isEnabled())
857     return;
858   PIC.registerShouldRunOptionalPassCallback([](StringRef PassID, Any IR) {
859     return isIgnored(PassID) || OptBisector->checkPass(PassID, getIRName(IR));
860   });
861 }
862 
print()863 raw_ostream &PrintPassInstrumentation::print() {
864   if (Opts.Indent) {
865     assert(Indent >= 0);
866     dbgs().indent(Indent);
867   }
868   return dbgs();
869 }
870 
registerCallbacks(PassInstrumentationCallbacks & PIC)871 void PrintPassInstrumentation::registerCallbacks(
872     PassInstrumentationCallbacks &PIC) {
873   if (!Enabled)
874     return;
875 
876   std::vector<StringRef> SpecialPasses;
877   if (!Opts.Verbose) {
878     SpecialPasses.emplace_back("PassManager");
879     SpecialPasses.emplace_back("PassAdaptor");
880   }
881 
882   PIC.registerBeforeSkippedPassCallback(
883       [this, SpecialPasses](StringRef PassID, Any IR) {
884         assert(!isSpecialPass(PassID, SpecialPasses) &&
885                "Unexpectedly skipping special pass");
886 
887         print() << "Skipping pass: " << PassID << " on " << getIRName(IR)
888                 << "\n";
889       });
890   PIC.registerBeforeNonSkippedPassCallback([this, SpecialPasses](
891                                                StringRef PassID, Any IR) {
892     if (isSpecialPass(PassID, SpecialPasses))
893       return;
894 
895     print() << "Running pass: " << PassID << " on " << getIRName(IR) << "\n";
896     Indent += 2;
897   });
898   PIC.registerAfterPassCallback(
899       [this, SpecialPasses](StringRef PassID, Any IR,
900                             const PreservedAnalyses &) {
901         if (isSpecialPass(PassID, SpecialPasses))
902           return;
903 
904         Indent -= 2;
905       });
906   PIC.registerAfterPassInvalidatedCallback(
907       [this, SpecialPasses](StringRef PassID, Any IR) {
908         if (isSpecialPass(PassID, SpecialPasses))
909           return;
910 
911         Indent -= 2;
912       });
913 
914   if (!Opts.SkipAnalyses) {
915     PIC.registerBeforeAnalysisCallback([this](StringRef PassID, Any IR) {
916       print() << "Running analysis: " << PassID << " on " << getIRName(IR)
917               << "\n";
918       Indent += 2;
919     });
920     PIC.registerAfterAnalysisCallback(
921         [this](StringRef PassID, Any IR) { Indent -= 2; });
922     PIC.registerAnalysisInvalidatedCallback([this](StringRef PassID, Any IR) {
923       print() << "Invalidating analysis: " << PassID << " on " << getIRName(IR)
924               << "\n";
925     });
926     PIC.registerAnalysesClearedCallback([this](StringRef IRName) {
927       print() << "Clearing all analysis results for: " << IRName << "\n";
928     });
929   }
930 }
931 
CFG(const Function * F,bool TrackBBLifetime)932 PreservedCFGCheckerInstrumentation::CFG::CFG(const Function *F,
933                                              bool TrackBBLifetime) {
934   if (TrackBBLifetime)
935     BBGuards = DenseMap<intptr_t, BBGuard>(F->size());
936   for (const auto &BB : *F) {
937     if (BBGuards)
938       BBGuards->try_emplace(intptr_t(&BB), &BB);
939     for (auto *Succ : successors(&BB)) {
940       Graph[&BB][Succ]++;
941       if (BBGuards)
942         BBGuards->try_emplace(intptr_t(Succ), Succ);
943     }
944   }
945 }
946 
printBBName(raw_ostream & out,const BasicBlock * BB)947 static void printBBName(raw_ostream &out, const BasicBlock *BB) {
948   if (BB->hasName()) {
949     out << BB->getName() << "<" << BB << ">";
950     return;
951   }
952 
953   if (!BB->getParent()) {
954     out << "unnamed_removed<" << BB << ">";
955     return;
956   }
957 
958   if (BB->isEntryBlock()) {
959     out << "entry"
960         << "<" << BB << ">";
961     return;
962   }
963 
964   unsigned FuncOrderBlockNum = 0;
965   for (auto &FuncBB : *BB->getParent()) {
966     if (&FuncBB == BB)
967       break;
968     FuncOrderBlockNum++;
969   }
970   out << "unnamed_" << FuncOrderBlockNum << "<" << BB << ">";
971 }
972 
printDiff(raw_ostream & out,const CFG & Before,const CFG & After)973 void PreservedCFGCheckerInstrumentation::CFG::printDiff(raw_ostream &out,
974                                                         const CFG &Before,
975                                                         const CFG &After) {
976   assert(!After.isPoisoned());
977   if (Before.isPoisoned()) {
978     out << "Some blocks were deleted\n";
979     return;
980   }
981 
982   // Find and print graph differences.
983   if (Before.Graph.size() != After.Graph.size())
984     out << "Different number of non-leaf basic blocks: before="
985         << Before.Graph.size() << ", after=" << After.Graph.size() << "\n";
986 
987   for (auto &BB : Before.Graph) {
988     auto BA = After.Graph.find(BB.first);
989     if (BA == After.Graph.end()) {
990       out << "Non-leaf block ";
991       printBBName(out, BB.first);
992       out << " is removed (" << BB.second.size() << " successors)\n";
993     }
994   }
995 
996   for (auto &BA : After.Graph) {
997     auto BB = Before.Graph.find(BA.first);
998     if (BB == Before.Graph.end()) {
999       out << "Non-leaf block ";
1000       printBBName(out, BA.first);
1001       out << " is added (" << BA.second.size() << " successors)\n";
1002       continue;
1003     }
1004 
1005     if (BB->second == BA.second)
1006       continue;
1007 
1008     out << "Different successors of block ";
1009     printBBName(out, BA.first);
1010     out << " (unordered):\n";
1011     out << "- before (" << BB->second.size() << "): ";
1012     for (auto &SuccB : BB->second) {
1013       printBBName(out, SuccB.first);
1014       if (SuccB.second != 1)
1015         out << "(" << SuccB.second << "), ";
1016       else
1017         out << ", ";
1018     }
1019     out << "\n";
1020     out << "- after (" << BA.second.size() << "): ";
1021     for (auto &SuccA : BA.second) {
1022       printBBName(out, SuccA.first);
1023       if (SuccA.second != 1)
1024         out << "(" << SuccA.second << "), ";
1025       else
1026         out << ", ";
1027     }
1028     out << "\n";
1029   }
1030 }
1031 
1032 // PreservedCFGCheckerInstrumentation uses PreservedCFGCheckerAnalysis to check
1033 // passes, that reported they kept CFG analyses up-to-date, did not actually
1034 // change CFG. This check is done as follows. Before every functional pass in
1035 // BeforeNonSkippedPassCallback a CFG snapshot (an instance of
1036 // PreservedCFGCheckerInstrumentation::CFG) is requested from
1037 // FunctionAnalysisManager as a result of PreservedCFGCheckerAnalysis. When the
1038 // functional pass finishes and reports that CFGAnalyses or AllAnalyses are
1039 // up-to-date then the cached result of PreservedCFGCheckerAnalysis (if
1040 // available) is checked to be equal to a freshly created CFG snapshot.
1041 struct PreservedCFGCheckerAnalysis
1042     : public AnalysisInfoMixin<PreservedCFGCheckerAnalysis> {
1043   friend AnalysisInfoMixin<PreservedCFGCheckerAnalysis>;
1044 
1045   static AnalysisKey Key;
1046 
1047 public:
1048   /// Provide the result type for this analysis pass.
1049   using Result = PreservedCFGCheckerInstrumentation::CFG;
1050 
1051   /// Run the analysis pass over a function and produce CFG.
runPreservedCFGCheckerAnalysis1052   Result run(Function &F, FunctionAnalysisManager &FAM) {
1053     return Result(&F, /* TrackBBLifetime */ true);
1054   }
1055 };
1056 
1057 AnalysisKey PreservedCFGCheckerAnalysis::Key;
1058 
invalidate(Function & F,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator &)1059 bool PreservedCFGCheckerInstrumentation::CFG::invalidate(
1060     Function &F, const PreservedAnalyses &PA,
1061     FunctionAnalysisManager::Invalidator &) {
1062   auto PAC = PA.getChecker<PreservedCFGCheckerAnalysis>();
1063   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
1064            PAC.preservedSet<CFGAnalyses>());
1065 }
1066 
registerCallbacks(PassInstrumentationCallbacks & PIC,FunctionAnalysisManager & FAM)1067 void PreservedCFGCheckerInstrumentation::registerCallbacks(
1068     PassInstrumentationCallbacks &PIC, FunctionAnalysisManager &FAM) {
1069   if (!VerifyPreservedCFG)
1070     return;
1071 
1072   FAM.registerPass([&] { return PreservedCFGCheckerAnalysis(); });
1073 
1074   auto checkCFG = [](StringRef Pass, StringRef FuncName, const CFG &GraphBefore,
1075                      const CFG &GraphAfter) {
1076     if (GraphAfter == GraphBefore)
1077       return;
1078 
1079     dbgs() << "Error: " << Pass
1080            << " does not invalidate CFG analyses but CFG changes detected in "
1081               "function @"
1082            << FuncName << ":\n";
1083     CFG::printDiff(dbgs(), GraphBefore, GraphAfter);
1084     report_fatal_error(Twine("CFG unexpectedly changed by ", Pass));
1085   };
1086 
1087   PIC.registerBeforeNonSkippedPassCallback(
1088       [this, &FAM](StringRef P, Any IR) {
1089         assert(&PassStack.emplace_back(P));
1090         (void)this;
1091         if (!any_isa<const Function *>(IR))
1092           return;
1093 
1094         const auto *F = any_cast<const Function *>(IR);
1095         // Make sure a fresh CFG snapshot is available before the pass.
1096         FAM.getResult<PreservedCFGCheckerAnalysis>(*const_cast<Function *>(F));
1097       });
1098 
1099   PIC.registerAfterPassInvalidatedCallback(
1100       [this](StringRef P, const PreservedAnalyses &PassPA) {
1101         assert(PassStack.pop_back_val() == P &&
1102                "Before and After callbacks must correspond");
1103         (void)this;
1104       });
1105 
1106   PIC.registerAfterPassCallback([this, &FAM,
1107                                  checkCFG](StringRef P, Any IR,
1108                                            const PreservedAnalyses &PassPA) {
1109     assert(PassStack.pop_back_val() == P &&
1110            "Before and After callbacks must correspond");
1111     (void)this;
1112 
1113     if (!any_isa<const Function *>(IR))
1114       return;
1115 
1116     if (!PassPA.allAnalysesInSetPreserved<CFGAnalyses>() &&
1117         !PassPA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>())
1118       return;
1119 
1120     const auto *F = any_cast<const Function *>(IR);
1121     if (auto *GraphBefore = FAM.getCachedResult<PreservedCFGCheckerAnalysis>(
1122             *const_cast<Function *>(F)))
1123       checkCFG(P, F->getName(), *GraphBefore,
1124                CFG(F, /* TrackBBLifetime */ false));
1125   });
1126 }
1127 
registerCallbacks(PassInstrumentationCallbacks & PIC)1128 void VerifyInstrumentation::registerCallbacks(
1129     PassInstrumentationCallbacks &PIC) {
1130   PIC.registerAfterPassCallback(
1131       [this](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
1132         if (isIgnored(P) || P == "VerifierPass")
1133           return;
1134         if (any_isa<const Function *>(IR) || any_isa<const Loop *>(IR)) {
1135           const Function *F;
1136           if (any_isa<const Loop *>(IR))
1137             F = any_cast<const Loop *>(IR)->getHeader()->getParent();
1138           else
1139             F = any_cast<const Function *>(IR);
1140           if (DebugLogging)
1141             dbgs() << "Verifying function " << F->getName() << "\n";
1142 
1143           if (verifyFunction(*F))
1144             report_fatal_error("Broken function found, compilation aborted!");
1145         } else if (any_isa<const Module *>(IR) ||
1146                    any_isa<const LazyCallGraph::SCC *>(IR)) {
1147           const Module *M;
1148           if (any_isa<const LazyCallGraph::SCC *>(IR))
1149             M = any_cast<const LazyCallGraph::SCC *>(IR)
1150                     ->begin()
1151                     ->getFunction()
1152                     .getParent();
1153           else
1154             M = any_cast<const Module *>(IR);
1155           if (DebugLogging)
1156             dbgs() << "Verifying module " << M->getName() << "\n";
1157 
1158           if (verifyModule(*M))
1159             report_fatal_error("Broken module found, compilation aborted!");
1160         }
1161       });
1162 }
1163 
~InLineChangePrinter()1164 InLineChangePrinter::~InLineChangePrinter() {}
1165 
generateIRRepresentation(Any IR,StringRef PassID,ChangedIRData & D)1166 void InLineChangePrinter::generateIRRepresentation(Any IR, StringRef PassID,
1167                                                    ChangedIRData &D) {
1168   ChangedIRComparer::analyzeIR(IR, D);
1169 }
1170 
handleAfter(StringRef PassID,std::string & Name,const ChangedIRData & Before,const ChangedIRData & After,Any IR)1171 void InLineChangePrinter::handleAfter(StringRef PassID, std::string &Name,
1172                                       const ChangedIRData &Before,
1173                                       const ChangedIRData &After, Any IR) {
1174   SmallString<20> Banner =
1175       formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name);
1176   Out << Banner;
1177   ChangedIRComparer(Out, Before, After, UseColour)
1178       .compare(IR, "", PassID, Name);
1179   Out << "\n";
1180 }
1181 
same(const ChangedIRData & D1,const ChangedIRData & D2)1182 bool InLineChangePrinter::same(const ChangedIRData &D1,
1183                                const ChangedIRData &D2) {
1184   return D1 == D2;
1185 }
1186 
handleFunctionCompare(StringRef Name,StringRef Prefix,StringRef PassID,bool InModule,const ChangedFuncData & Before,const ChangedFuncData & After)1187 void ChangedIRComparer::handleFunctionCompare(StringRef Name, StringRef Prefix,
1188                                               StringRef PassID, bool InModule,
1189                                               const ChangedFuncData &Before,
1190                                               const ChangedFuncData &After) {
1191   // Print a banner when this is being shown in the context of a module
1192   if (InModule)
1193     Out << "\n*** IR for function " << Name << " ***\n";
1194 
1195   ChangedFuncData::report(
1196       Before, After, [&](const ChangedBlockData *B, const ChangedBlockData *A) {
1197         StringRef BStr = B ? B->getBody() : "\n";
1198         StringRef AStr = A ? A->getBody() : "\n";
1199         const std::string Removed =
1200             UseColour ? "\033[31m-%l\033[0m\n" : "-%l\n";
1201         const std::string Added = UseColour ? "\033[32m+%l\033[0m\n" : "+%l\n";
1202         const std::string NoChange = " %l\n";
1203         Out << doSystemDiff(BStr, AStr, Removed, Added, NoChange);
1204       });
1205 }
1206 
registerCallbacks(PassInstrumentationCallbacks & PIC)1207 void InLineChangePrinter::registerCallbacks(PassInstrumentationCallbacks &PIC) {
1208   if (PrintChanged == ChangePrinter::PrintChangedDiffVerbose ||
1209       PrintChanged == ChangePrinter::PrintChangedDiffQuiet ||
1210       PrintChanged == ChangePrinter::PrintChangedColourDiffVerbose ||
1211       PrintChanged == ChangePrinter::PrintChangedColourDiffQuiet)
1212     TextChangeReporter<ChangedIRData>::registerRequiredCallbacks(PIC);
1213 }
1214 
StandardInstrumentations(bool DebugLogging,bool VerifyEach,PrintPassOptions PrintPassOpts)1215 StandardInstrumentations::StandardInstrumentations(
1216     bool DebugLogging, bool VerifyEach, PrintPassOptions PrintPassOpts)
1217     : PrintPass(DebugLogging, PrintPassOpts), OptNone(DebugLogging),
1218       PrintChangedIR(PrintChanged == ChangePrinter::PrintChangedVerbose),
1219       PrintChangedDiff(
1220           PrintChanged == ChangePrinter::PrintChangedDiffVerbose ||
1221               PrintChanged == ChangePrinter::PrintChangedColourDiffVerbose,
1222           PrintChanged == ChangePrinter::PrintChangedColourDiffVerbose ||
1223               PrintChanged == ChangePrinter::PrintChangedColourDiffQuiet),
1224       Verify(DebugLogging), VerifyEach(VerifyEach) {}
1225 
registerCallbacks(PassInstrumentationCallbacks & PIC,FunctionAnalysisManager * FAM)1226 void StandardInstrumentations::registerCallbacks(
1227     PassInstrumentationCallbacks &PIC, FunctionAnalysisManager *FAM) {
1228   PrintIR.registerCallbacks(PIC);
1229   PrintPass.registerCallbacks(PIC);
1230   TimePasses.registerCallbacks(PIC);
1231   OptNone.registerCallbacks(PIC);
1232   OptBisect.registerCallbacks(PIC);
1233   if (FAM)
1234     PreservedCFGChecker.registerCallbacks(PIC, *FAM);
1235   PrintChangedIR.registerCallbacks(PIC);
1236   PseudoProbeVerification.registerCallbacks(PIC);
1237   if (VerifyEach)
1238     Verify.registerCallbacks(PIC);
1239   PrintChangedDiff.registerCallbacks(PIC);
1240 }
1241 
1242 namespace llvm {
1243 
1244 template class ChangeReporter<std::string>;
1245 template class TextChangeReporter<std::string>;
1246 
1247 template class ChangeReporter<ChangedIRData>;
1248 template class TextChangeReporter<ChangedIRData>;
1249 
1250 } // namespace llvm
1251